• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recycle Folder names  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a cron job that runs evrery day to back up files. Script it runs creates a folder with system date ie. Cron runs today April 21, 2004 folder name will be "BackupFor04212004".
I need a bash script that will recycle these folder names so that at any given time, there are only 7 folders.
Thank You.
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could play with this:

I haven't tested that. Be careful with ls listing stuff like . and .. which would ruin the count of '7'. I think adding the -A option to ls will help. Be careful

-Tim
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to say, the code above uses the creation time to sort I think. You could also name your folders more nicely (like Backup20041223 or similar), so that by alphabetising them you get a good ordering. Then you can use `ls | sort | tail $numToDel` or whatever.
-Tim
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, last post now, why don't you just add a new cron job which deletes the oldest file in the directory?
Something like adding a final line: rm -rf `ls -1c | tail -1` would do it (again, haven't tested at all). That way if it runs every day, it'll clean up after itself so to speak.
-Tim
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or you use find:

But test it with 'ls -la' instead of 'rm' first
It will look for files modified at least 7*24 ago.
You will not have to rename your dirs.
It will delete 2 directories if it is ommited to run yesterday.
But it will have deleted every directory if there wasn't a backup for the last 7 days.
[ April 21, 2004: Message edited by: Stefan Wagner ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, that's much nicer. I didn't realise 'find' was so powerful or arcane...then again, it's a GNU utility, so it can probably read my mail also

-Tim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!