Delete directory conditioned


 
Thread Tools Search this Thread
# 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

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

Last edited by Hammerhand; 04-17-2012 at 06:23 AM..
# 2  
Any specifications on what criteria to pick those 2 sub directories? Top level I guess and age? Name?.. ?
# 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/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.
This User Gave Thanks to zaxxon For This Post:
# 5  
Wow, it's more complex that I thought. I will give a try. Thanks.
# 6  
No, you are right, sorry. I was drifting away while testing out and thought about having additional levels etc.Smilie 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 10:02 AM..
This User Gave Thanks to zaxxon For This Post:
# 7  
Smilie Happens to many of us... Smilie
This User Gave Thanks to vbe For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete directory

Can we force delete the directories despite having files in it? Because I need to delete multiple directories which has multiple files. As of now, I'm deleting the files before deleting the directory. Can't we delete the directory with files? (3 Replies)
Discussion started by: Ram Kumar_BE
3 Replies

2. UNIX for Dummies Questions & Answers

Why can't I delete this directory ?

Oracle Linux 6.4 bash shell I am trying to remove the directory named OPatch. I am trying to run rm and rmdir commands while logged in as grid user, the owner of the directory. But I keep getting "Permission denied" error. It is an empty directory. Any idea why I am getting 'Permission denied'... (5 Replies)
Discussion started by: kraljic
5 Replies

3. UNIX for Dummies Questions & Answers

awk file subtracted by a fix value - conditioned

Hi all... i have been trying to make this work but I have been failing for 6 hours .. I know it should be something simple that I am missing to it would be great if you can help me ... I want to subtract a fixed value (lets set 1) from any value >=1 from the whole file my file looks like ... (4 Replies)
Discussion started by: A-V
4 Replies

4. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

5. UNIX for Dummies Questions & Answers

Cannot Delete Directory

Hello, I am trying to delete a directory but cannot. Can anyone assist? See attached. Many thanks. (6 Replies)
Discussion started by: dixer
6 Replies

6. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

7. Solaris

Directory cannot delete

Dear All, I have problem at T5220 server, when I enter at my server as oracle user and make directory ex. directory test then I will delete directory test with command : rm -rf, what happenned...?? that directory cannot deleted. What's wrong with my server...? Note: my server T5220 with... (3 Replies)
Discussion started by: mbah_jiman
3 Replies

8. AIX

Can not delete Directory

I've run into a issue where I can not delete directories under a user directory. There are several of these dir now. I can do a 'mv' of the dir to another dir. ie 'mv sp2456 old/' and it will move the directory, but I can not delete them. Here is all the information I hope you will need to... (9 Replies)
Discussion started by: Clevelaw
9 Replies

9. Solaris

Can't delete directory

Hi, I have a problem on Solaris 10 regarding deletion of files when I try to recursively delete the directory /opt/Tivoli as root, I get the following error message: rm: Unable to remove directory Tivoli/lcf/dat/1/no-del/bin/solaris2/TME: File exists rm: Unable to remove directory... (4 Replies)
Discussion started by: funksen
4 Replies

10. UNIX for Dummies Questions & Answers

delete a directory

Hi all, I have an empty directory "-ltr". How to delete it ? Thanks (4 Replies)
Discussion started by: madmat
4 Replies

Featured Tech Videos