Solved

Delete oldest files/directories - but not all!

Posted on 2003-12-10
3
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 125 total points
ID: 9917703
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
ID: 9917718
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
ID: 9918135
Brilliant, thanks.  I missed the "+" option in man tail, and I didn't know about xargs.  Your script works perfectly!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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