15th September 2016 at 10:14pm
Keywords: Linux
Abstract
I had a process that would clean-up a file system prior to a process that was going to heavily use the file system. This was of course a cold backup of a database. So I had a script that used the FIND command to delete anything over N days old. To my surprise, there were times when the delete did not work causing the backup to fail forcing me to deal with manual work. When I looked at the file age using **ls -l**, the files appeared to be old enough to purge. But after researching the issue I found that the FIND command uses an integer based age when determining whether to select a file. So if you wanted to delete a file over 5 days old, you might try something like:
find . -name "*.gz" -type f -mtime +5 -exec rm -f {} \;
But what the command says is the file must be over 5 days old … like 6 or more … but not 5.1 … or the file will not be selected. So I found these alternatives to determine whether FIND would select the file.
Process 1
This command returns an integer age of a file. So remember the age must be one greater than specified in the mtime parameter to trigger the selection.
filename="filename_to_be_deleted" echo $((($(date +%s) - $(date +%s -r "$filename")) / 86400 days
Process 2
This Perl script returns a non-integer representation of the date of a file. Remember you would need the result to be 1 greater than the mtime parameter.
filename="filename_to_be_deleted"; perl -l -e 'print -M $ARGV[0], " days"' $filename;