Solved

Delete oldest files/directories - but not all!

Posted on 2003-12-10
3
380 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux daemon 11 356
Xubuntu Remove Open Terminal Here Option from Desktop Right Click 7 320
Linux Search 3 67
Why  my code (program) build with old compiler? 11 76
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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