Remove all folders within another folder except the lastest 1, 2 or 3.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove all folders within another folder except the lastest 1, 2 or 3.
# 1  
Old 01-20-2011
Remove all folders within another folder except the lastest 1, 2 or 3.

I am using this command to remove all files and folders older than 24 hours;
Code:
find /databackup/dbs/* -maxdepth 0 -type d -mtime +0 -exec rm -rf {} \;

However, if/when backups to this folder fail and are not updated, it will completely destroy any backups we have in that folder.

What can I add to that line so it leaves the latest two folders (for example)
that were created.
So if backups don't run for a week I'm not stuck with an empty backup folder.

can someone help?

TIA

Last edited by damang111; 01-24-2011 at 12:29 AM.. Reason: Added code tags
# 2  
Old 01-20-2011
Post withdrawn.
# 3  
Old 01-24-2011
Question .

I'm not very good at bash but I've been searching google for this and so far nothing without adding 10 lines of code and I still couldn't make heads or tails out of it.
Can anyone help?
# 4  
Old 01-24-2011
So how is the backup folder structure, can we have the folder named as:

Code:
backup_20110122
backup_20110123
backup_20110124
backup_20110125

With that, you can list the folder name without the first 3 folders by sed '4,p' or awk 'NR>3'
# 5  
Old 01-24-2011
Question .

Thanks for the reply.
the folder structure is like this;
Code:
180111
190111
200111
210111
240111

only dates.

is this how I would wrap it?:
Code:
find /databackup/dbs/* -maxdepth 0 -type d -mtime +0 | awk 'NR>3'

?

when I do this I only get 210111 showing up from the above folder list. Which means it displayed the folder that is 24+ hours old and ignored the oldest 3 folders from the listing.

without the NR>3 i get:
Code:
180111
190111
200111
210111

So it seems to just ignore the 3 oldest with the awk and only shows the latest one. (ignoring the previous/oldest 3)

I would want the script to ignore the latest 1,2 or 3 folders no matter what the time stamp is so it doesn't accidently remove all folders if backups don't run in 2 days.

Am I doing something wrong?

Last edited by Franklin52; 02-03-2011 at 04:11 AM.. Reason: Please use code tags, thank you
# 6  
Old 01-24-2011
Your current date format is hard to sort, think to update in your backup script/cronjob and export folder format as below.
(I know there are some ways to resolve it, but can we have date format as: 110118 ?)

Code:
110118
110119
110120
110123
110125

find /databackup/dbs/* -maxdepth 0 -type d |sort -rn |awk 'NR>3'

# 7  
Old 01-24-2011
... adding to rdcwayx...

Code:
find /databackup/dbs -maxdepth 0 -type d |sort -rn |awk 'NR>3' |
xargs rm -rf 

I looked at your original post, and you said:
Quote:
What can I add to that line so it leaves the latest two folders (for example)
that were created.
In the case you want to do it by date/time modification of the directory, you use:
Code:
ls -1tcp | grep "/$" | tail +3 | xargs echo rm -rf

When you are sure it's what you want, remove the "echo" from the xargs command.

Last edited by otheus; 01-24-2011 at 09:32 PM.. Reason: had reversed order in ls
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping through all folders in a folder.

I am trying to write a script that loops through all the folders within a given folder. My intention is to access each folder and rename each file ending with fna.gz with the name of the folder it resides in. #!/bin/bash cd /p/w/d/ for f in /p/w/d/*; do echo $f done I'm... (13 Replies)
Discussion started by: Mr_Keystrokes
13 Replies

2. Shell Programming and Scripting

Moving files and folders to another folder

I recently bought Synology server and realised it can run scripts. I would need fairly simple script which moves all files and folders from ARCHIVE folder to WORKING folder. I would also need to maintain folder structure as each of the folders may contain subfolders. How would I go about it as I am... (1 Reply)
Discussion started by: ###
1 Replies

3. UNIX for Dummies Questions & Answers

Creating a sub-folder in multiple folders

Hi I've been trying to find an answer to this question and was hoping someone would be able to help me. I want to add a sub-folder to to an existing structure: for example /toys/toy_1/new /toys/toy_2/new /toys/toy_3/new There are humdreds of theses folders - what i want to do is add a... (2 Replies)
Discussion started by: LouSan
2 Replies

4. Shell Programming and Scripting

How to delete other's folders which are in our own folder?

Hi All, I need a solution for the following scenario. I am the owner for the particular folder and I have given 777 permissions for some specific reasons. So others can able to create folders and files. But when I am done with the work, I need to delete the folders which are created by... (4 Replies)
Discussion started by: manoj_thorali
4 Replies

5. Shell Programming and Scripting

Symlink all files from one folder into all found folders

Hi. I have a folder which contains my application. I then have a flexible number of folders in another directory, called “sites”. It looks like this: -Application -- Test.html -- CSS --- Style.css -Sites --Site1 --Site2 I want to symlink all the files in the application folder... (1 Reply)
Discussion started by: Spadez
1 Replies

6. UNIX for Dummies Questions & Answers

Tar-ing folders in a folder

How do I create individual tars of a all the directories in a directory? I have a directory called 'patients', each patient has a directory in the patients directory. I want to create tars such that each patient has their own tar file. Thanks! (5 Replies)
Discussion started by: HappyPhysicist
5 Replies

7. Shell Programming and Scripting

moving files from one folder to many folders

I have a more than 10 K files in a folder. They are accumulated in a period of more than an year (Say from 13th July 2010 to 4th June 2011). I need to perform housekeeping on this. The requirement is to create a folder like 13Jul2010,14July2010,......3June2011,4June2010 and then from the main... (2 Replies)
Discussion started by: realspirituals
2 Replies

8. Shell Programming and Scripting

Move all files but not folders to a new folder

Hi, I have a sub directory with a number of files and folders. What i want is a subdirectory with just folders and not files for cleanliness sake. So I want to move the files into the new folder but keep the folders in the same place. Move all files (but not folders) to new folder. I am... (4 Replies)
Discussion started by: Hopper_no1
4 Replies

9. UNIX for Dummies Questions & Answers

Zip a folder including its sub-folders.

Hi, I have a folder that contains a few sub-folders. I would like to zip that folder and KEEP the subfolders. What it does at the moment is taking all the files within the subfolders and zipping them into one big file (zip -r ...). Does anyone know the UNIX command to keep the subfolders in the... (3 Replies)
Discussion started by: gdog
3 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question