LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-11-2013, 08:31 AM   #1
kauuttt
Member
 
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 -
Code:
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, 09:09 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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, 10:28 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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:
Code:
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 10:43 AM.
 
Old 07-12-2013, 01:37 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

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
Code:
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 02:26 PM.
 
Old 07-12-2013, 03:23 PM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,602

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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, 02:05 AM   #6
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Yea sub directories wouldnt be a one liner.
 
Old 07-14-2013, 06:11 AM   #7
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 502

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

Code:
$ 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 08:21 AM.
 
  


Reply


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 04:39 AM
Creating a script that removes the oldest files on /tmp directory danndp Linux - Newbie 6 11-18-2010 10:47 AM
script for deleting .dat files which > 5 mb from a directory and its subdirectory anindyabhattacharjee Linux - Enterprise 2 01-16-2007 12:38 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM


All times are GMT -5. The time now is 10:17 AM.

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