Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-17-2012
Registered User
 
Join Date: Apr 2012
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
Delete directory conditioned

Hi, I am trying to make a script that finds if inside a folder there are more than 2 subfolders; if so, I want to erase the older subfolder.

I thought this would be useful:

Code:
find /sdcard/backup -mtime +1 -exec rm-r {} /;

But it only erases subfolders older than one day, and I would want that subfolders get erased if there are more than two, even if none of them is older than one day.

How could I modify the command?

Thanks for your help.

Edit: Sorry, didn't use code tags

Moderator's Comments:
Please use next time code tags for your code and data

Last edited by Hammerhand; 04-17-2012 at 05:23 AM..
Sponsored Links
    #2  
Old 04-17-2012
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Location: St. Gallen, Switzerland
Posts: 5,873
Thanks: 80
Thanked 372 Times in 351 Posts
Any specifications on what criteria to pick those 2 sub directories? Top level I guess and age? Name?.. ?
Sponsored Links
    #3  
Old 04-17-2012
Registered User
 
Join Date: Apr 2012
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
I would want to erase the oldest, doesn't mean the name.
    #4  
Old 04-17-2012
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Location: St. Gallen, Switzerland
Posts: 5,873
Thanks: 80
Thanked 372 Times in 351 Posts
Assuming all directories are on the same level and you are using Linux or a Linux like OS, you can try this:

Content:

Code:
$ for a in /sdcard/backup/*; do stat --format="%n %y" $a; done
/sdcard/backup/a 2012-04-17 18:00:00.000000000 +0200
/sdcard/backup/b 2012-04-17 01:00:00.000000000 +0200
/sdcard/backup/c 2012-04-17 11:00:00.000000000 +0200
/sdcard/backup/d 2012-04-17 23:00:00.000000000 +0200

Sorted:

Code:
# for a in /sdcard/backup/*; do stat --format="%n %y" $a; done| sort -t" " -k2rn -k3rn -k4rn
/sdcard/backup/d 2012-04-17 23:00:00.000000000 +0200
/sdcard/backup/a 2012-04-17 18:00:00.000000000 +0200
/sdcard/backup/c 2012-04-17 11:00:00.000000000 +0200
/sdcard/backup/b 2012-04-17 01:00:00.000000000 +0200

Sorted content but the latest, dir "d":

Code:
for a in /sdcard/backup/*; do stat --format="%n %y" $a; done| sort -t" " -k2rn -k3rn -k4rn| awk 'NR>1 {print $1}'
/sdcard/backup/a
/sdcard/backup/c
/sdcard/backup/b

Finally deleting those 3:

Code:
# for a in /sdcard/backup/*; do stat --format="%n %y" $a; done| sort -t" " -k2rn -k3rn -k4rn| awk 'NR>1 {system("rm -r "$1)}'

Trying the steps you can dry-run before deleting too much.
The Following User Says Thank You to zaxxon For This Useful Post:
Hammerhand (04-17-2012)
Sponsored Links
    #5  
Old 04-17-2012
Registered User
 
Join Date: Apr 2012
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
Wow, it's more complex that I thought. I will give a try. Thanks.
Sponsored Links
    #6  
Old 04-17-2012
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Location: St. Gallen, Switzerland
Posts: 5,873
Thanks: 80
Thanked 372 Times in 351 Posts
No, you are right, sorry. I was drifting away while testing out and thought about having additional levels etc. not seeing the wood for the trees...
Didn't think about much easier ls -t which does it all for us:

Code:
cd /sdcard/backup; ls -t1 | awk 'NR>1 {system("rm -r "$1)}'

Keep in mind that both solutions are looking for files in that directory, not only directories. So with rm -r you will get an error when encountering non-directory files.

Last edited by zaxxon; 04-17-2012 at 09:02 AM..
The Following User Says Thank You to zaxxon For This Useful Post:
Hammerhand (04-17-2012)
Sponsored Links
    #7  
Old 04-17-2012
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
 
Join Date: Sep 2005
Location: Switzerland - GE
Posts: 4,636
Thanks: 118
Thanked 256 Times in 245 Posts
Happens to many of us...
The Following User Says Thank You to vbe For This Useful Post:
zaxxon (04-17-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to delete some of the files in the directory, if the directory size limits the specified size shaal89 Shell Programming and Scripting 0 02-10-2012 08:22 AM
Directory cannot delete mbah_jiman Solaris 3 04-22-2010 11:07 AM
Can not delete Directory Clevelaw AIX 9 05-21-2008 10:53 AM
Can't delete directory funksen Solaris 4 03-09-2007 01:14 PM
Can't delete a directory on HP-UX oradbus HP-UX 2 05-31-2005 03:32 PM



All times are GMT -4. The time now is 02:15 AM.