Thursday, October 31, 2019

Find and clear log files in Linux


Find and clear the files with extension of *.log, usually log files.


find . -type f -iname *.log -exec tee {} \; </dev/null

This command will find the files with extension of 'log' and empty that file. Which means file will be there and data of that file will be cleared.

If you are using cPanel, there will be a directory called "virtfs", in that case you can use following command.

find /home -type f -iname *.log -not -path "./virtfs/*" -exec tee {} \; </dev/null

Please note, in WHM / cPanel server, this will clear logs including created by server.

So, If you need to do this manually for specific file, you can simply use any of following commands,

# >

# :>

# true >

# echo "" >

# echo  >

# dd if=/dev/null of=logfile

# dd if=/dev/null > logfile

Still there are several methods to clear it.