Results 1 to 8 of 8
Hi guys as part of my script I need to need to delete backups if they are older than 5 days
at the moment the file name goes as backup-"$(date ...
- 02-21-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
How to get my script to delete files if they are certian date
Hi guys as part of my script I need to need to delete backups if they are older than 5 days
at the moment the file name goes as backup-"$(date +%d-%b-%y)" so something like backup-13-Feb-11.
I have already setup in my script to make sure the backup was successful and is not corrupt what I need to do next if for example
if I backup for 21st feburary
backup-21-Feb-11
if it finds anything older than five days for example
backup-13-Feb-11 it will delete this. I want the old delete part to work on the filename not a system time stamp
Thanks for any help
Tom
- 02-22-2011 #2Linux Newbie
- Join Date
- Dec 2010
- Posts
- 146
You need to use find with -atime (access time) option --
find <location> -atime 5
This will list on basis of the access times. Also --
-mtime n - File's data was last modified n*24 hours ago.
Then use -delete at the end to delete all matching files.
- 02-22-2011 #3Just Joined!
- Join Date
- Jan 2011
- Posts
- 19
Thank you for your help so far is there no way to do this based on my filename rather than system time stamps.
The way I’m thinking is if today’s date is the 22nd February the backup filename will be backup-22-Feb-11 so anything older than backup-17-feb-11 will be deleted. If I making any sense.
- 02-22-2011 #4Just Joined!
- Join Date
- Feb 2007
- Posts
- 14
How about a loop, like:
for a in {1..17}
do
rm "backup-"$a"-feb-11";
done
in a script to run once, then use the mtime function to run every day to keep on top of it
- 02-22-2011 #5Just Joined!
- Join Date
- Feb 2011
- Location
- Patagonia argentina
- Posts
- 9
Perhaps the simplest way would be to use Unix time instead of conventional time format.
But it would certainly add some garbage to the filename :-s
- 02-22-2011 #6Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
delete files by date
Is there a particular reason for preferring your file naming convention to the operating system date stamp? If you use the system date, you do not have to worry about details like changing from one month to another, changing from one year to another, leap years, etc. Someone else has already done that for you.
By the way, you can use the find command with the +n flag to specify a range of date stamps.
will delete files of age greater than 5 days. Maybe you wantCode:$ find /directory_name -mtime +5 -delete
Until you are sure of what you really want to do, be safe and use -print instead of -delete. That way you can look at what the command would do without accidentally destroying any data.Code:-mtime +4
- 02-22-2011 #7Linux Newbie
- Join Date
- Dec 2010
- Posts
- 146
- 02-22-2011 #8Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
You're on a hiding to nothing trying to do it by filename - even if you chose a sensible way to name your files, like using the week number and the year. It's possible to script the deletion of specific dates but, as others have pointed out, coping with a range of filenames based on dates and coping with month and year ends will certainly hone your scripting skills - if you manage to do it. I've been scripting for over 30 years, and I wouldn't bother to attempt what you're trying to do in the way you're trying to do it. find is the right tool for this job.


Reply With Quote
