Linux Truncate (Log)File

|
| By Webner

Commands to empty logfile :

1. :>logfile – If logfile does not exist, this will create an empty log file, otherwise it will empty the existing file.

2. cat /dev/null > logfile – This is equivalent to above command.

Command to truncate logfile to specific size :

Sometimes we want to bring the size of a file down to specific bytes instead of removing all contents. Following command will keep initial 1000 bytes of data in the file and remove everything after that (hence truncating the file to 1000 bytes).

3. dd if=/dev/null of=logfile seek=1 bs=1000.

Here dd is equivalent to copy command, ‘if’ stands for ‘input file’, means read from given file (in above example reading from /dev/null means read ‘nothing’), ‘of’ stands for output file (write into this file), ‘seek’ means skip given number of blocks before starting to write (in above command it will skip 1 block of data), where block size is given with ‘bs’. So in summary above command says, write ‘NOTHING’ into ‘logfile’ BUT skip 1 block of 1000 bytes before starting to write. Hence we are left with a file of 1000 bytes…

Free Online Courses with Certificate
Free Online Courses With Personalized Certificate
Study online, from the comfort of your home
Study online, from the comfort of your home

Leave a Reply

Your email address will not be published. Required fields are marked *