We often need command to truncate large log files while working on production server. I have listed few of the commands which are really handy in such case. The command below is the smallest one and most handy.
Some people also use the following command. Both of the commands do the same thing, it just depends on the personal preference.
cat /dev/null > LogFileName.log
Sometimes above command can give you
"File already exists" error, if
noclobber is set. In that case you can run the either of the following commands.
cat /dev/null >| file #Or >| file
noclobberoption prevents accidental overwriting of file. This option is set by putting
"set -o noclobber"command in .bashrc file.
cp command can also does the trick.
cp /dev/null LogFileName.log
There is another esoteric command which is hardly recognized by the developer. This command basically set the file to specific size and truncate rest of the file.
truncate --size=0 LogFileName.log
If you came across or use different command please leave them in the comment section below. Hope this blog helped you in some way. If you like this blog then please share it.