How to Truncate Files in Linux: Empty Files Without Deleting

By 

Updated on

5 min read

Terminal showing a file being truncated to zero bytes in Linux

Truncating a file means removing its contents without deleting the file itself. This is faster than deleting the file , recreating it, and setting the correct permissions and ownership . It also preserves the file’s inode, which means any process that has the file open will continue to work correctly.

This guide explains how to truncate files in Linux using shell redirection and the truncate command.

Quick Reference

MethodCommand
Shell redirection> filename
Colon redirection: > filename
With catcat /dev/null > filename
With echoecho -n > filename
truncate commandtruncate -s 0 filename
With sudo (subshell)sudo sh -c '> filename'
With sudo (tee): | sudo tee filename
Truncate to specific sizetruncate -s 100K filename
Empty all logssudo truncate -s 0 /var/log/*.log

Shell Redirection

The simplest method to truncate a file is to use the > shell redirection operator.

On most modern shells such as Bash or Zsh, you can use:

Terminal
> filename

You can also use the : (colon) builtin, which is equivalent to true and produces no output:

Terminal
: > filename

If the file exists , it is truncated to zero. If it does not exist, the file is created.

Instead of :, you can use any command that produces no output. Here is an example using cat to output the contents of /dev/null:

Terminal
cat /dev/null > filename

Another option is echo with the -n flag, which tells echo not to append a newline:

Terminal
echo -n > filename

Redirection with sudo

To truncate a file you need write permissions on it. When using sudo , the elevated privileges do not apply to the redirection operator because the shell handles the redirect before sudo runs. For example, this will fail:

Terminal
sudo : > /var/log/syslog
output
bash: /var/log/syslog: Permission denied

To work around this, run a new shell with sudo and execute the command inside it using the -c flag:

Terminal
sudo sh -c '> /var/log/syslog'

Another option is to pipe empty output to the tee command with elevated privileges:

Terminal
: | sudo tee /var/log/syslog > /dev/null

truncate Command

The truncate command allows you to shrink or extend the size of a file to a given size. The general syntax for truncating a file to zero is:

txt
truncate -s 0 filename

The -s 0 option sets the file size to zero bytes.

For example, to empty the Nginx access log:

Terminal
sudo truncate -s 0 /var/log/nginx/access.log

Truncating to a Specific Size

The truncate command can also set a file to a specific size. This is useful for creating fixed-size files or trimming a file to a certain length:

Terminal
truncate -s 100M largefile.img

The size suffixes are K (kilobytes), M (megabytes), G (gigabytes), and T (terabytes). If the file is smaller than the specified size, it is extended with null bytes. If it is larger, the extra data is lost.

Empty All Log Files

Over time, your disk drive may get cluttered with large log files taking up large amounts of disk space. The following command empties all .log files in the /var/log directory:

Terminal
sudo truncate -s 0 /var/log/*.log

To also truncate log files in subdirectories, enable the globstar option in Bash and use **:

Terminal
shopt -s globstar
sudo truncate -s 0 /var/log/**/*.log

For long-term log management, consider configuring logrotate to automatically rotate, compress, and remove old log files.

Troubleshooting

Permission denied when using redirection with sudo The shell processes the > redirect before sudo executes. Use sudo sh -c '> file' or pipe to sudo tee instead. See Redirection with sudo above.

File size does not change after truncation If a process still has the file open and is writing to it, the file will grow again immediately. Check which process is using the file with lsof filename and restart or reload it after truncating.

truncate: cannot open 'file' for writing: No such file or directory The file path does not exist. Double-check the path and ensure all parent directories exist.

Truncated a file by mistake Truncation is irreversible — the data is gone. There is no undo. Always verify the file name and consider making a backup before truncating important files.

FAQ

What is the difference between truncating and deleting a file? Deleting removes the file entry from the filesystem. Truncating keeps the file (with its permissions, ownership, and inode) but removes all its contents, setting the size to zero.

Is it safe to truncate a log file while a service is running? Yes. Truncating a file that a process has open is safe because the file descriptor remains valid. The process will continue writing to the now-empty file. This is safer than deleting and recreating the file, which can leave the process writing to a deleted inode.

What is the difference between > file and truncate -s 0 file? Both produce the same result. Shell redirection (> file) is shorter for interactive use. The truncate command is more readable in scripts and supports setting files to arbitrary sizes, not just zero.

Can I truncate multiple files at once? Yes. The truncate command accepts multiple file arguments or shell globs: truncate -s 0 file1.log file2.log or truncate -s 0 *.log.

Conclusion

To truncate a file in Linux, use the redirection operator > filename for a quick one-liner or truncate -s 0 filename for scripts and non-zero sizes. Always verify the target file before truncating, as the operation is irreversible.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page