Join my Bash scripting course on Udemy 👀

Clear the contents of a file from the command line

April 1, 2021  ‐ 1 min read

From the command-line you can empty a file without opening an editor and removing all the lines. This can be useful when you have a bulky log file you don't need to keep for example.

With the truncate command you can shrink the file to a specified size. If you specify a size of 0 you effectively end up with an empty file. This even works when the file is currently being written to.

$ truncate -s 0 huge.log

A second option is to use the output redirection operator(>). You use the output redirection operator to write standard output to a file, and overwrite the file if it already exists.

$ > huge.log