Log files are important, for audit purposes, to check what's going on and a file to consult when something doesn't work as expected.
However, log files can fill up disk space easily especially if the application or server being monitored is quite buya, thus it will also record quite a few lines on the log file.
Of course the easy way, is always to do an mv command and just move the file somewhere. This is not practical if you need to move hundreds of files.
To make things easy and efficient is to use a group or series of commands.
For example, if you need to move files other than the log files for the month of November, then ls and egrep can do it.
Example:
ls | egrep -iv "*2025-11*" | egrep -iv "2025-11-template"
Above will list all files except files for the month of November and the template for the month of November.
-iv tells egrep insensitive case, and don't include the pattern specified and list all files
Double check the output if the ls and egrep command once it shows the desired output. The it's time time to move the files since the output of LS is confirmed.
Here's how to move to the files.
ls | egrep -iv "*2025-11*" | egrep -iv "2025-11-template" | xargs -I {} mv {} /backup-moved-files
Then yes /backup-moved-files will contain all the log files.
Then zipping the files after moving is also a good idea, xz command does a good job in zipping log files.
That's it till next time, enjoy keep yourself busy on the command line :)
Trust in God, Pray and don't worry.
God is in Control, Be still.
Comments
Post a Comment