Thursday, October 31, 2019

Find and clear log files in Linux

Objective:

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

Commands:

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,

# > file.name

# :> file.name

# true > file.name

# echo "" > file.name

# echo  > file.name

# dd if=/dev/null of=logfile

# dd if=/dev/null > logfile

Still there are several methods to clear it.

Find and clear log files in Linux

Objective: Find and clear the files with extension of *.log, usually log files. Commands: find . -type f -iname *.log -exec tee {} \;...