Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-11-2013, 07:31 AM   #1
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
Shell script for deleting the oldest directory within a directory

Hi all,

My scenario is -
Suppose we have a directory (dir1), which in turn contains "N" number of hidden directories. From this point of time my script is going to take the control. The job of my script will be, remove the oldest directory (a hidden directory), amoung the list of directories (the number can be anything) which are presnt in the 'dir1'.

I have doing this by -
ls -1 -alt | awk ' /^d/ {print $9}' | tail -2 | head -1 | xargs rm -rf
Is there any better/graceful way of doing the same.?

Thank you.
Old 07-11-2013, 08:09 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,570

Rep: Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811
sure, there is a better way:
rm -rf `ls -altr | awk ' /^d/ {print $9; exit}'`
but it will not work if dirname contains spaces. Actually in that case you should avoid ls or need a more complicated perl/awk/whatever script.
Old 07-11-2013, 09:28 AM   #3
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Even though the community always rants about not using ls as input (which, in general I agree with), it does have a few nice options that can be used to your advantage.

Have a look at this:
rm -rf $(ls -1dAtr --quoting-style=shell --group-directories-first .[a-zA-Z0-9]* | head -1)
I've used A instead of a to get rid of the . and .. (just in case), the l option is replaced by 1 (no need for the long listing, 1 column works).

The quoting-style= options can has a few ways of quoting text, I'm using shell to put the output in single quotes (do have a look at the ls manual page). No need to worry about spaces and other "weird" characters.

The group-directories-first makes sure directories are listed first, this together with the r option makes sure that the first dot directory shown is the oldest.

The d option is needed to keep ls from outputting contents of the directories itself.

EDIT: My initial version didn't work, the above is tested (minimal) and seems to do the job.

Last edited by druuna; 07-11-2013 at 09:43 AM.
Old 07-12-2013, 12:37 PM   #4
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
this seemed like a very interesting little project, so here is my one liner using process substitution is the best way IMHO
IFS= read -d $\0 line < <(find . -maxdepth 1 -type d -printf '%T@ %p\0' 2>/dev/null | sort -z -n) ;echo ${line}
You can then do what ever you wanted with the file the command returns.
Using the find command I found type directory, not going into sub folders with maxdepth,then the print command to display the time stamp T, @ for the seconds, %p to display the path and then give the command a noticeable ending character "\0"=null character. Pipe any errors because of permissions to /dev/null and then sort numerically and by the null character being the end of a file.Pipe the process substitution into the read command using the IFS= so it reads folders with spaces and the -d on the read as a delimiter being the null character.

Last edited by cbtshare; 07-12-2013 at 01:26 PM.
Old 07-12-2013, 02:23 PM   #5
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,704

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
And note - if a subdirectory IS recent, it will not be reflected in the date associated with the parent directory (nothing changed there).

Take the .mozilla directory. The dates reflected are only when the directory itself is modified (or read, depending on which date you are looking at). If the file isin a subdirectory (such as firefox/<something>.default/OfflineCache/...) then the modification date of the .mozilla directory will not change.

Also something like the plugins directory, which can contain symbolic links to things outside the directory. The things outside can be completely replaced without changing ANY date associated with file/directory in the tree.
Old 07-14-2013, 01:05 AM   #6
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Yea sub directories wouldnt be a one liner.
Old 07-14-2013, 05:11 AM   #7
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
I'm learning/experimenting xargs, so my solution is based on xargs... so, how about this:

$ ls -1trd /dir1/.[^.]*/ | head -n1 | xargs -0 rm -rf
Off topic, is spell checker has been added to editor?

Last edited by mddnix; 07-14-2013 at 07:21 AM.


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
[SOLVED] Writing First Script Unix - Deleting Directory Contents IamNoob_Help_ Programming 15 09-20-2011 03:39 AM
Creating a script that removes the oldest files on /tmp directory danndp Linux - Newbie 6 11-18-2010 09:47 AM
script for deleting .dat files which > 5 mb from a directory and its subdirectory anindyabhattacharjee Linux - Enterprise 2 01-15-2007 11:38 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:13 PM.

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