Quite often it is possible to end up with large files, especially large log files on your system. Sometimes, you want to start over by emptying the file especially if you are trying to minimize disk space. This is especially true about log files, which often contains a large amount of outdated data. This is not just relegated to log files, but could also happen to output files or any other file as well.
The truncation process basically removes all the contents of the file. It does not however remove the file itself, but it leaves it on the disk as a zero byte file. This allows the file to re-used or be continually used by other programs while keeping the overall size in check. This process is also referred to as “zero out a file” or to “empty a file“.
You can always delete the file and recreate the file, which might work in most cases. The truncation process however reduces the need for the two different steps and also will preserve the inode of the file.
We will only deal with the case where you want to completely empty the file or zero it out and not the case where you might want to remove parts of the file or truncate files by certain number of lines or byte size.
truncate is a command line utility that can be found in most Linux distros. It is used to shrink the size of a file to a desired size. We will use the size 0 (zero) to empty the file.
bash$ truncate -s 0 myfile.txt
-s or –size: specifies the size to which the file needs to be truncated, in bytes.
One of the easiest and quickest way to empty a file is to use I/O redirection. This will create a new empty file, if one does not exist. If the file already exist, it will delete all its content and save it as a zero byte file.
Although it is not precisely the same as the truncate system call mentioned above, it will work for almost all practical use cases. Also this is the fastest of all the method mentioned here. If you have a really large file, then the I/O redirection could be substantially faster.
bash$ > myfile.txt
You can also use the output of various different commands, that produce an empty output with redirection. For example, the echo command can be used as shown below. Other no-op commands such as “:” or true can be used as well. All of the commands below will truncate files to zero bytes.
bash$ echo > myfile.txt
bash$ :> myfile.txt
bash$ true > myfile.txt
Most modern versions of Linux has an implementation of the /dev/null device. If you have older version, it is possible that it may not be implemented. You can choose to use cp command to truncate the file or use redirection as mentioned above.
bash$ cp /dev/null myfile.txt
As mentioned in the previous section, you can use redirection as well…
bash$ cat /dev/null > myfile.txt
noclobber is a feature in Linux which is used to prevent accidental overwriting of files. If noclobber is set or enabled on your system, the above I/O redirection methods will throw an error. You will need to modify the command to over ride the noclobber option.
Depending on your command shell, you can try the following. In csh and its derivative shell, you could use the >! instead of > to override the noclobber configuration. In ksh and its derivates, the >| is used. Some shells supports both the syntax.
bash$ >! myfile.txt
bash$ >| myfile.txt
You can try both of the above commands one after the other to see which one works, if you are unsure about the shell you are using.