Bash script to remove old files

  • MasterZ
  • Expert
  • Expert
  • User avatar
  • Posts: 699
  • Loc: Colorado Springs

Post 3+ Months Ago

Hey guys,

I'm working on a backup script and part of it I want to delete files older than x days old. I found some good tutorials but none of it seems to work and I'm not getting any error messages. Here is the relevant code.

Code: [ Select ]
 
#!/bin/bash -e
 
# Keep backups for how many days
KEEP_FOR=14
 
# Backup Directory
BACKUP_DIR="/home/zssites/backups"
 
# Delete files older than x days
find $BACKUP_DIR/archived/*.sql.gz -mtime +1 -exec rm {} \;
 
 
  1.  
  2. #!/bin/bash -e
  3.  
  4. # Keep backups for how many days
  5. KEEP_FOR=14
  6.  
  7. # Backup Directory
  8. BACKUP_DIR="/home/zssites/backups"
  9.  
  10. # Delete files older than x days
  11. find $BACKUP_DIR/archived/*.sql.gz -mtime +1 -exec rm {} \;
  12.  
  13.  


I have files in the directory, up to 3 days old, but after running the script the files are still there. Is there anything wrong with the code here?
  • MasterZ
  • Expert
  • Expert
  • User avatar
  • Posts: 699
  • Loc: Colorado Springs

Post 3+ Months Ago

Doing some testing, here is what I came up with:

Code: [ Select ]
zssites@zssites.net [~/backups]# find * -exec ls -l {} \;
total 372
-rw-r--r-- 1 zssites zssites 120969 Dec 23 23:59 zssites_whmc_2008-12-23.sql.gz
-rw-r--r-- 1 zssites zssites 120969 Dec 24 00:10 zssites_whmc_2008-12-24.sql.gz
-rw-r--r-- 1 zssites zssites 122225 Dec 25 08:41 zssites_whmc_2008-12-25.sql.gz
-rw-r--r-- 1 zssites zssites 120969 Dec 23 23:59 archived/zssites_whmc_2008-12-23.sql.gz
-rw-r--r-- 1 zssites zssites 120969 Dec 24 00:10 archived/zssites_whmc_2008-12-24.sql.gz
-rw-r--r-- 1 zssites zssites 122225 Dec 25 08:41 archived/zssites_whmc_2008-12-25.sql.gz
-rwx------ 1 zssites zssites 1665 Dec 25 08:41 backup.sh
-rwx------ 1 zssites zssites 1158 Dec 23 17:16 sqlemail.pl
zssites@zssites.net [~/backups]# find * -mtime +1 -exec ls -l {} \;
zssites@zssites.net [~/backups]#
  1. zssites@zssites.net [~/backups]# find * -exec ls -l {} \;
  2. total 372
  3. -rw-r--r-- 1 zssites zssites 120969 Dec 23 23:59 zssites_whmc_2008-12-23.sql.gz
  4. -rw-r--r-- 1 zssites zssites 120969 Dec 24 00:10 zssites_whmc_2008-12-24.sql.gz
  5. -rw-r--r-- 1 zssites zssites 122225 Dec 25 08:41 zssites_whmc_2008-12-25.sql.gz
  6. -rw-r--r-- 1 zssites zssites 120969 Dec 23 23:59 archived/zssites_whmc_2008-12-23.sql.gz
  7. -rw-r--r-- 1 zssites zssites 120969 Dec 24 00:10 archived/zssites_whmc_2008-12-24.sql.gz
  8. -rw-r--r-- 1 zssites zssites 122225 Dec 25 08:41 archived/zssites_whmc_2008-12-25.sql.gz
  9. -rwx------ 1 zssites zssites 1665 Dec 25 08:41 backup.sh
  10. -rwx------ 1 zssites zssites 1158 Dec 23 17:16 sqlemail.pl
  11. zssites@zssites.net [~/backups]# find * -mtime +1 -exec ls -l {} \;
  12. zssites@zssites.net [~/backups]#


So why is -mtime +1 not working?
  • MasterZ
  • Expert
  • Expert
  • User avatar
  • Posts: 699
  • Loc: Colorado Springs

Post 3+ Months Ago

Nevermind, I guess it is right but the files just are not old enough. If I do -mtime +0 then it shows the files, or if I do -mmin +1440 it works fine for 1 day.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 46 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.