Need a PERL script to delete backup files dated exactly  7 days ago.

Posted on 2013-05-24
Last Modified: 2013-05-24
Hi Team,

   Just need help on a cronjob task i need to do for housekeeping my Centos file system.   I have an application that backups an app database twice a day and these backup files have started accummulating in our /root/backup directory like so.....


Open in new window

I just need help with a PERL script that I can include in a cron job so that it will automatically delete files with file names that have the date of exactly 7 days ago.   In the case of the example above, if today were 24 May 2013, I want the cron job to delete only files MYAPP.db.backup.17-May-2013*   but leave the other dates untouched.

Any help would be appreciated.
Question by:rleyba828
LVL 68

Accepted Solution

woolmilkporc earned 450 total points
ID: 39193974
Why perl?

rm /root/backup/MYAPP.db.backup.$(date -d "7 days ago" +"%d-%b-%Y")*.sql

When adding this line to crontab you must escape the % signs (cron restriction)

0 7 * * * /bin/rm /root/backup/MYAPP.db.backup.$(/bin/date -d "7 days ago" +"\%d-\%b-\%Y")*.sql

Assisted Solution

by:Robert Saylor
Robert Saylor earned 50 total points
ID: 39194075
Here is a shell script I use for this purpose:



echo Cleaning backup files older then 4 days

# change the path below to where you want to auto delete
$FIND /backup/extended/* -mtime +4 -exec $RM {} \;

Open in new window


Author Comment

ID: 39195905
Hi woolmilkporc,

   Thanks for the solution.  It worked excellently and is the exact thing I was looking for.  My sysadmin just preferred to standardize on perl but bash will do in this case.

Hi rsaylor, your solution was helpful, but in our case, the date was part of the filename, so we had to work within those constraints, but thanks as well.

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question