|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
Last edited by Hammerhand; 04-17-2012 at 05:23 AM.. |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
Any specifications on what criteria to pick those 2 sub directories? Top level I guess and age? Name?.. ?
|
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I would want to erase the oldest, doesn't mean the name.
|
|
#4
|
||||
|
||||
|
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/bFinally 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
|
|||
|
|||
|
Wow, it's more complex that I thought. I will give a try. Thanks.
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
No, you are right, sorry. I was drifting away while testing out and thought about having additional levels etc. 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 | |
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|