Empty Delete File Content Linux: 5 Ways
In this article
To empty or delete a file's content in Linux without removing the file itself, use the shell redirection command : > filename or truncate -s 0 filename. These commands instantly set the file size to zero bytes while preserving the file's permissions, ownership, and inode.
Why Empty a File Instead of Deleting It?
Many Linux processes—like log daemons, application servers, and databases—hold an open file descriptor to their log files. If you delete the file with rm, the process continues writing to the now-unlinked inode, and the disk space is not freed until the process closes the file. Emptying the file keeps the same inode and file descriptor valid, so the process can keep writing to a fresh, zero-length file. This is the standard way to rotate logs without restarting services.
5 Methods to Empty a File
All of the following commands achieve the same result: a zero-byte file. Choose based on your shell, available tools, and how explicit you want the command to be.
: > file— The colon is a shell built-in that does nothing, and>truncates the file. Works in Bash, Zsh, and POSIX shells.truncate -s 0 file— Thetruncatecommand is specifically designed to resize files. Using-s 0sets the size to zero.cat /dev/null > file— Reads from the null device (which returns nothing) and redirects that empty output to the file, truncating it.echo -n > file—echo -nprints no newline, so the redirection creates an empty file. Note that plainecho > filewould write a single newline character (1 byte).> file— A bare redirection with no command. In most shells this truncates the file, but it is less portable and some shells may refuse it without a command.
How Truncation Works Under the Hood
When you run any of these commands, the shell or utility calls the truncate() system call (or ftruncate() on an already-open file descriptor). The kernel updates the file's size to zero and marks the data blocks as free. The file's metadata—permissions, owner, timestamps, and inode number—remain unchanged. Because the inode is the same, any process that already has the file open keeps writing at offset 0, which is exactly what you want for log rotation.
Common Use Cases and Examples
Emptying a file is most often done for log files, temporary data, or before reusing a file for a new process. Here are concrete examples:
- Clear a web server access log:
: > /var/log/nginx/access.log - Reset a database error log before debugging:
truncate -s 0 /var/log/mysql/error.log - Remove the contents of a temporary data file while keeping it for a script:
cat /dev/null > /tmp/output.dat
In each case, the file remains in the same directory with the same name, and any process that had it open continues to work.
Choosing the Right Method
Which method you pick depends on your environment:
- Portability:
: > fileworks in any POSIX-compliant shell, making it the safest choice for scripts. - Clarity:
truncate -s 0 fileis self-documenting and is the easiest for other people to understand when reading your script. - Availability:
truncateis part of GNU coreutils, so it is present on virtually all Linux distributions, but may be missing on minimal embedded systems or some BSD systems. - Verbosity:
cat /dev/null > fileis a classic idiom but requires spawning acatprocess, making it slightly slower.
Common Mistakes and Pitfalls
Several errors are easy to make when emptying files:
- Using
echo > file— This writes a newline, making the file 1 byte, not 0. Useecho -nor one of the other methods. - Truncating the wrong file — Double-check the path. There is no undo; the data is gone.
- Permission errors — You need write permission on the file (and execute permission on the directory) to truncate it. Use
sudoonly when you are sure it is safe. - Symlinks — Redirection follows symlinks and truncates the target file, not the symlink itself. If you want to empty the symlink (which is rare), you would need to delete and recreate it.
- Files in use — While truncating an open file is safe, truncating a file that a database is actively writing to can cause data corruption if the database does not expect the size change. Always follow the application's own log rotation procedure.
Alternatives: Deleting vs. Truncating
If you actually want to remove the file entirely, use rm file. This frees the directory entry and, if no process holds it open, frees the disk space immediately. For log rotation, the common alternative is logrotate, which can rename and compress old logs before creating a new file. However, logrotate often uses copytruncate to copy the log and then truncate the original, which is exactly the operation described here.
Frequently Asked Questions
Does emptying a file free disk space immediately?
Yes, the space is released as soon as the truncate operation completes, unless the file is still open and the process has written data beyond the new size. In practice, for a normal file, the freed blocks are returned to the filesystem immediately.
What is the difference between : > file and rm file?
: > file keeps the file in place, preserving its inode and permissions, and simply sets its length to zero. rm file removes the directory entry entirely; if any process has the file open, the data remains until that process closes it, but the file is no longer accessible by name.
Can I truncate a file without write permission?
No, you need write permission on the file. If you own the file but lack write permission, you can change permissions with chmod or use sudo if you have administrative rights. However, you cannot truncate a file you do not own unless you are root.
Does truncating a file affect its timestamps?
Yes, the modification time is updated to the current time, and the change time is also updated. The access time may or may not change depending on the filesystem and mount options. If you need to preserve timestamps, you can save and restore them with touch -r.
Is truncate available on macOS?
macOS does not include truncate by default. Use : > file or cat /dev/null > file instead, which work on any Unix-like system.