Sometimes you need to truncate a file to zero bytes. For example you have a big file that contains various logs and its size is too big to open it easily. Of course, you can revome this file and touch it again. But this way you’ll have to deal with permissions and file ownership. There is a simple command that will truncate the file to zero length without affecting any permissions. Here it is:
:>filename
If the file already exists, it will be truncated to zero bytes, else this command will create an empty file. This is great for truncating big log files, e,g. Apache logs.
Tags: apache truncating log files, empty file, how to truncate a file, how to truncate large files in linux, linux empty file, linux truncate file, linux truncate file command -library, linux truncate file quick, linux truncate file to 0, linux truncate log file, linux truncate log file 0 length, linux zero a file, truncate a file linux, truncate file, truncate file command linux, truncate file length 0 linux, truncate file linux, zero a file in linux
4 Responses
Vlatko Šurlan
09|Dec|2008 1For those who find this syntax too cryptic here is one that is much more memorable and fluent:
cat /dev/null > file_to_be_truncated
admin
09|Dec|2008 2Another excellent solution, didn’t think about it
g
21|Jul|2009 3Awesome! I have no idea whats happening in the :> step, but this is much more elegant than the /dev/null approach also mentioned ;D
bucabay
25|Feb|2010 4What is the : at the front. What about just:
> filename
Leave a reply
Search
Categories
Links