Managing log files are a pain the rear. I do a lot of processing of log files which means pulling log files from various servers, processing them then deleting them. Automating this task is pretty straight forward, but I’ve always manually deleted the files once they have been processed.

Being a “lazy” programmer, I decided to automate the deleting of the log files after 7 days. The unix find command makes this really simple:

find logfiles -ntime +7 -type f -delete

The command above simply does the following:

  1. Specify the log files you want to delete. Simply replace logfiles with the filename (with wildcards).
  2. Specify the age of the files in days using the -ntime parameter. In the example above I used +7 days.
  3. Specify the file type via the -type parameter. Since I’m only interested in deleting files I use -type f. If you want to delete folders you can use -type d. If you want to delete both files and folders just omit the -type parameter.
  4. Finally, I use the -delete parameter to tell find to delete the files that match the search criteria.

Of course, I use the command in a script so a lot of the parameter values are set via variables. This is a simple thing to do, but I’ve seen a lot of people writing elaborate shell scripts that does what this single command does. Not sure what the Windows command line equivalent is, but luckily I have Windows BASH installed on my Win10 computer. Unix and and MacOS systems should all have find already installed.

Published On: May 8th, 2018 / Categories: Programming / Tags: , , /

Subscribe To Receive The Latest News

Curabitur ac leo nunc. Vestibulum et mauris vel ante finibus maximus.

[contact-form-7 id=”1362″ /]

Add notice about your Privacy Policy here.