How to empty a file in linux?

In many cases we may need to empty a file content. This usually happens for the log files. But it could be any that we may need to empty the content. In linux it is really easy to empty the content from command line. By using different commands we can do this. Following are some commands to empty a file content in linux:

Using echo command

echo basically writes string to file. If we write an empty string to file it will simply remove the existing content and writes the empty string.

$ echo "" > access.log
OR 
$ echo  > access.log

By using true and :>

$ true > access.log
OR 
$ :>  access.log

Using truncate command

If we use truncate command with size flag to 0 then it also empty the file content.

$ truncate -s 0 access.log
Share