Linux is a great operating system for servers. Coincidentally, I run a local linux server and as everyone should know… disk space matters! That’s where today’s quick fix comes in! As a server owner, you will quickly realize how important every little kilobit is when it comes to keeping your server in order.
If you are curious how large your log space is you may run du -hs /var/log
Heres how to compress and backup all your logs to /root/logs-(date).tar.bz2 and wipe all the data out of all the files in /var/log.
- Open up a CLI (terminal)
- tar -cjf /root/logs-`date +%d%m%y`.tar.bz2 /var/log
- find /var/log -type f | xargs sed ‘/^/d’ -i
- Red: This part of the command will send the path of all the files in /var/log to stdout(the screen)
- Blue: The xargs command allows us to perform an operation on everything in stdout, as from the previous command displays all the file names, this will let us “merge” a command with the filenames.
- Green: sed is a streamline editor, basically it will allow us to perform a regular expression. In this case we are deleting all the lines from a file