?
Solved

Delete oldest files/directories - but not all!

Posted on 2003-12-10
3
Medium Priority
?
388 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

762 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