Solved

Delete oldest files/directories - but not all!

Posted on 2003-12-10
3
375 Views
Last Modified: 2010-04-21
I have an automated process that deposits generated files in subdirs (that it creates) of a fixed directory.  So it looks like

/mydir/20031201/file
/mydir/20031203/file
etc.

What I want to do is setup a cron job that will clean up the mydir directory.  Here's how I want it to work:

I want to delete all the directories (the 20031203-type, not mydir), and their files, EXCEPT for the newest 10.  So if there's only 10 dirs or less when the cleanup script runs, it does nothing.  If there are 33 dirs, it deletes the 23 oldest.

I'm trying to do this with ls -c, and then pipe through head and/or tail, but I just can't see a way to do it that way.

How can I write a script that will do this?
0
Comment
Question by:kwd
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
Comment Utility
The command "ls -t /mydir/*" lists all directories ordered by their creation time. The newest is first, and the oldest last. All you now have to do is strip off the first 10 names: The command "tail +11" does exactly this: It lists all lines starting with the 11th. If you now combine these commands, and call rm -rf on all lines, you are done:

ls -t /mydir/* | tail +11 | xargs /bin/rm -rf
You should however test this first with the echo command instead of /bin/rm:
ls -t /mydir/* | tail +11 | xargs echo


0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
BTW: I used -t (modification time) instead of -c (creation time) because I would want to keep a directory that was recently modified around for a little while
0
 

Author Comment

by:kwd
Comment Utility
Brilliant, thanks.  I missed the "+" option in man tail, and I didn't know about xargs.  Your script works perfectly!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now