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.
# 8  
Old 01-25-2011
.

Thanks for the help guys.
I will change the script so the date format is a little more standard.

When do i a ls -1tcp | grep "/$" | tail +3
I get the following error:
tail: cannot open `+3' for reading: No such file or directory

and when I change it to
ls -1tcp | grep "/$" | tail -3
the oldest 3 files folders get displayed.

this is what I need I think?
Then I will just xargs rm -rf ?

What does the grep "/$' do?

thanks.
# 9  
Old 01-25-2011
I think you want ti delete all but the three newest. Try
Code:
tail -n +3

. If that does nit work, use
Code:
sed '1,3d'

.

The -p option in ls tells it to print directory names followed by a slash /. So the grep makes the distinction between file and directory. In case you had files in your backup folder.
# 10  
Old 01-26-2011
Quote:
ls -1tcp | grep "/$" | tail +3 | xargs echo rm -rf
The "-c" switch to "ls" is not the file creation timestamp. It is the timestamp the inode was last modified. Some backup software changes that timestamp. Be careful.

---------- Post updated at 17:25 ---------- Previous update was at 16:31 ----------

Here is an experimental script to display folders to delete but ensuring that we always have two folders left.
Script has an awkward sort on the directory names to sort ddmmyy to yymmdd order.
Assumes that directory only contains relevant folders.

Code:
# Exit if not at least 3 folders
counter=`ls -1d ?????? 2>/dev/null | wc -l`
if [ $counter -lt 3 ]
then
     exit
fi
# Calculate how many folders to delete
counter2=`expr $counter - 2`
ls -1d ?????? | sort -k1.5n,1.6n -k1.3n,1.4n -k1.1n,1.2n | head -${counter2}

# 11  
Old 01-26-2011
@methyl - That's a conundrum. The mtime will change anytime
A directory entry is modified.

Why would backup software touch the ctime? ( looking for a reasonable
Answer )
# 12  
Old 01-26-2011
Commercial backup software would touch the "ctime" timestamp to mark the file as "backed up". Then use that timestamp as a reference for subsequent incremental backups. By using the unix filesystem this is vastly quicker than referencing an "all files" database. Not all backup packages do it.
Obviously they still need to refer to the "mtime" timestamp to find out what has changed. I can see a performance improvement on systems where the vast majority of changes are new files.
During the backup they then only need to update their own database with what has changed. In an incremental backup they must at some point detect deleted files.

IMHO. They are treating this timestamp as being similar to the archive flag on M$ platforms.

I have used the "ctime" timestamp to check that every file is actually backed up and uncovered some scary backup configurations.

Personally I object to backup software changing anything - especially when I invariably schedule a "full backup" not an "incremental" backup.

If nothing else it can be a PIA when diagnosing faults on a server where there are no proper records. I used to find out when a server was installed with "ls -lcd /".

Footnote: A backup can take several hours. An incremental backup needs to know exactly when the file was backed up. Personally I quiesce filesystems prior to a backup using every available technique to achieve this state.

Last edited by methyl; 01-26-2011 at 05:42 PM..
# 13  
Old 02-02-2011
thanks for all your help guys.
I managed it with the following:
change the format to yyyymmdd .
then I have this cronjob:
find /dbbackups/ -maxdepth 1 | egrep [0-9]{8} | sort -r | sed 1,3d | xargs rm -rf

ls -t would work but it dosn't output a full path so cron didn't know what to do with it.
find was the way to go output a full path so bash and/or cron knew where exactly the outputted folders were.
so even if backups will not occur, and as long as nothing else writes to that folder, it will find all the folders except the latest 3 and remove them.

I will be testing this for the next few days.
# 14  
Old 02-02-2011
Code:
find /dbbackups/ -maxdepth 1 -type d -name "20[0-9][0-9]*" | sort -r | sed 1,3d | xargs rm -rf

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