Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Delete Huge Files in Linux: 7 Safe Methods

· 5 min read · Home Tech

In this article
  1. Why Deleting Large Files Is Different
  2. Basic Deletion with rm
  3. Using find to Locate and Delete Large Files
  4. Deleting Files That Are “In Use”
  5. Securely Deleting with shred
  6. Deleting Files with Special Characters in Names
  7. Alternatives: Emptying Files Instead of Deleting
  8. Frequently Asked Questions

To delete huge files in Linux, use the rm command with the full path to the file, such as rm /path/to/hugefile. For files larger than a few gigabytes, this is usually instant, but you should check for open file handles and use shred or truncate if you need to securely erase or quickly free disk space.

Why Deleting Large Files Is Different

On most Linux filesystems, deleting a file is a metadata operation—the system removes the directory entry and marks the disk blocks as free. The actual data remains on the disk until overwritten. This means rm is typically just as fast for a 100 GB file as for a 1 KB file. The real challenges are:

  • Disk space not freed because a process still holds the file open.
  • Accidental deletion of the wrong file, which is harder to recover when it is huge.
  • Secure deletion requirements, where you need to overwrite the data to prevent recovery.

Basic Deletion with rm

The rm command is the standard tool. Its syntax is simple:

rm /path/to/hugefile

To delete multiple files at once, list them all:

rm /path/to/hugefile1 /path/to/hugefile2

To delete a directory and everything inside it, use the recursive flag:

rm -r /path/to/hugedirectory

Be careful: rm does not move files to a trash folder. Once deleted, the file is gone from the filesystem, though the raw data may remain on disk until overwritten.

Using find to Locate and Delete Large Files

If you need to find large files before deleting them, the find command is your best ally. To find files larger than 1 GB in the current directory and its subdirectories:

find . -type f -size +1G

To delete them directly, add -delete:

find . -type f -size +1G -delete

This is a powerful one-liner, but test it first without -delete to see exactly which files match. You can also use -size +500M for megabytes or -size +1G for gigabytes.

Deleting Files That Are “In Use”

A common problem: you delete a huge file, but df -h shows no free space gained. This happens when a process still has the file open. On Linux, the file is not truly removed until all file descriptors are closed.

To find which process is holding a file, use lsof:

lsof /path/to/hugefile

If the output lists a process, you can either kill that process (if safe) or use truncate to shrink the file to zero bytes, which frees the disk space immediately while the process keeps writing to a zero-length file:

truncate -s 0 /path/to/hugefile

This is often used for log files that are still being written to. After truncating, the space is released, and the process can continue writing.

Securely Deleting with shred

If the file contains sensitive data, a simple rm leaves the data recoverable with forensic tools. The shred command overwrites the file with random data before deleting it:

shred -u /path/to/hugefile

The -u flag tells shred to remove the file after overwriting. By default, it overwrites three times, which is usually sufficient for modern hard drives. For SSDs, shred is less effective because the drive’s wear-leveling may prevent overwriting all physical blocks. In that case, full-disk encryption is a better safeguard.

Note: shred is not available on all filesystems, such as some network filesystems. Check with man shred for details.

Deleting Files with Special Characters in Names

Large files often have spaces, quotes, or other special characters in their names. To delete them safely, always quote the filename:

rm "my huge file.bin"

If the filename starts with a hyphen, use -- to signal the end of options:

rm -- -hugefile

Alternatively, use a relative or absolute path that does not start with a hyphen, such as rm ./-hugefile.

Alternatives: Emptying Files Instead of Deleting

Sometimes you do not need to delete the file, just empty its contents. This is useful for log files or temporary files that should remain for a process to write to. The quickest way is:

> /path/to/hugefile

This truncates the file to zero bytes without changing its permissions or ownership. The truncate command does the same thing and is more explicit:

truncate -s 0 /path/to/hugefile

Both methods free the disk space instantly. This is a common technique for managing application logs that grow without bound.

Frequently Asked Questions

Why does df still show no free space after I deleted a huge file?

This usually means a process still has the file open. Use lsof /path/to/file to find the process, then either restart it or use truncate -s 0 /path/to/file to free the space without closing the file.

Is rm permanent? Can I recover a deleted file?

Yes, rm is permanent—it does not move the file to a trash folder. The data may still be recoverable with specialized tools if the disk blocks have not been overwritten, but this is not guaranteed and is much harder for large files. For sensitive data, use shred.

How do I delete all files larger than 1 GB in a directory?

Use find /path/to/dir -type f -size +1G -delete. Test it without -delete first to see what will be removed.

What is the difference between rm and shred?

rm simply unlinks the file, leaving the data on disk. shred overwrites the file’s contents with random data before unlinking, making recovery much more difficult. Use shred for sensitive files.

Can I delete a file that is being written to by a process?

Yes, you can run rm on it, but the space will not be freed until the process closes the file. If you want to free space immediately, use truncate -s 0 to empty the file instead.

What does the -delete option in find do?

The -delete option removes the files that match the preceding criteria. It is equivalent to running rm on each file, but it is faster and handles special characters in filenames more safely.