LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-22-2009, 04:57 PM   #1
kennc
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Rep: Reputation: 0
BASH script that removes sub-directories (not just files) but keeps the two newest.


Hello. This is my first posting and I'm stuck trying to script this logic together.

The task is to write a script that can be run from cron (preferably bash) that will recursively delete directories as a cleanup task. That part I can do without too much difficulty thanks to the many examples on this forum. However, an added piece is that I need the script to leave the two *newest* created directories intact. The objective is to always have the two newest directories available, but prune older ones as time goes on.

If the script is run against a directory that only has two sub-directories, it should exit and do nothing.

Any help would be appreciated. I'm working on FC8.
 
Old 04-22-2009, 07:01 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,988
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Hi, welcome to LQ!

To be able to assist you with this will depend on whether or not all
directories will be written in an ongoing manner; since Linux doesn't have
a "creation time stamp" it's quite important to know the above. If
they are you probably want to integrate the creation time/date into
the directories names - in which case all you need to do to keep
the last two is to "ls | head -n -2" which will give you all but the
last two entries (if the names of your directories sort ASCII, that
is, in other words if you name them YYYYMMDDHHMM).


Cheers,
Tink
 
Old 04-23-2009, 01:09 PM   #3
kennc
LQ Newbie
 
Registered: Apr 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the help

That's exactly what I needed. When I get this written up today I'll add it to this post, might actually help someone else too!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to create sequential, numbered directories ericcarlson Linux - Software 4 01-08-2010 09:43 AM
Bash script to search through directories. mcdrr Programming 5 05-11-2007 05:41 PM
Automated FTP script to grab ONLY newest dated files kaiserbeto Linux - Newbie 4 10-20-2006 02:46 AM
bash ls not coloring files, directories etc mr_smith Red Hat 2 08-30-2006 07:05 AM
Bash script about directories tarja Programming 3 11-23-2005 01:19 PM


All times are GMT -5. The time now is 04:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration