Script to delete older versions of unique files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete older versions of unique files
# 1  
Old 03-27-2009
Script to delete older versions of unique files

I have directory where new sub directories and files being created every few minutes. The directories are like abc_date, def_date, ghi_date. I am looking to keep the latest 2 unique directories and delete everything else.
Here is what I have so far

This gives me unique names excluding the date.
ls -lt | awk '{ print $9 }' | cut -d_ -f1 | sort | uniq


This will give me the files that I want to delete for the uniq word abc
ls -lt |grep abc | sort -r | tail +3

My question is, how do i pass the output of first command to the second one and delete what is needed? Any help is greatly appreciated.
# 2  
Old 03-28-2009
Clarify your question, what is the format of the names of created subdirectories and wich directories should be delete? Give some examples.

Regards
# 3  
Old 03-28-2009
How's this? You'll still need logic to decide what to remove.

ls -lt | awk '{ print $9 }' | cut -d_ -f1 | sort | uniq | while read prefix x
do
ls -t ${prefix}* | sort -r | tail +3
done
# 4  
Old 03-28-2009
Quote:
Originally Posted by Franklin52
Clarify your question, what is the format of the names of created subdirectories and wich directories should be delete? Give some examples.

Regards

I have directories being created by some automated scripts like this
Broker_01022009.15.23
Broker_03042009.12.15
Broker_03062009.16.15
Dealer_02172009.14.20
Dealer_01222009.17.45
Dealer_02272009.16.40


What I want is to leave the last 2 and delete the rest directories and their contents. In this ex. what should be left is
Broker_03062009.16.15
Broker_03042009.12.15
Dealer_02272009.16.40
Dealer_02172009.14.20


Thanks in advance.
# 5  
Old 03-28-2009
Be careful when writing this type of script.
If the directories are being created by multiple concurrent processes rather than consecutive single processes you will need to be sure that a the process has finished before deleting the directory.
BTW: The while construct proposed by aixylinux is sound and works when there are no directories to delete. IMHO it needs a minor tweak to extract the directory name rather than the files in the directory.
Note the use of ls - one rather than ls - ell because we just want the name.
For example:


Code:
for PREFIX in "Broker_" "Dealer_"
do
       ls -1d ${PREFIX}* 2>/dev/null | sort -r | tail +3 | while read DIR
       do
             if [ -d "${DIR}" ]
             then
                     echo "Old directory: ${DIR}"
             fi
       done
done


Last edited by methyl; 03-28-2009 at 06:47 PM.. Reason: Code tags
# 6  
Old 03-28-2009
Try this:

Code:
ls -lt | awk '/^d/{split($8,f,"_");a[f[1]]++}a[f[1]] > 2{system("ls -l "$8)}'

If the command outputs the directories you want to remove, replace ls -l with rm -r:

Code:
ls -lt | awk '/^d/{split($8,f,"_");a[f[1]]++}a[f[1]] > 2{system("rm -r "$8)}'

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to delete all the files older than a date?

Hi, I need a command for deleting all the compress files *.Z that are older than the current date - 5 days. Basically I have a directory where daily I meet some back up files and I want to remove automatically the ones 5 days (or more) older than the current date. How can I write a 'rm' command... (1 Reply)
Discussion started by: Francy
1 Replies

2. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

3. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

4. Shell Programming and Scripting

Delete files older than X days.

Hi All, I am using below code to delete files older than 2 days. In case if there are no files, I should log an error saying no files to delete. Please let me know, How I can achive this. find /path/*.xml -mtime +2 Thanks and Regards Nagaraja. (3 Replies)
Discussion started by: Nagaraja Akkiva
3 Replies

5. Shell Programming and Scripting

Delete files older than today

is it -mtime +1 as i need all files older than today to be deleted (6 Replies)
Discussion started by: dinjo_jo
6 Replies

6. Shell Programming and Scripting

Script for delete tmp files older than 15 days and owned by "xxx" id

Hi All , I want to delete files from /tmp directory created by "xxxx" id. because i got the list says more than 60 thousand files were created by "xxxx" id since 2002. The /tmp directory has lot of files created by different user ids like root,system etc.. But, i need a script to... (2 Replies)
Discussion started by: vparunkumar
2 Replies

7. Solaris

Delete files older than 30 days

Hi all, I want to delete log files with extension .log which are older than 30 days. How to delete those files? Operating system -- Sun solaris 10 Your input is highly appreciated. Thanks in advance. Regards, Williams (2 Replies)
Discussion started by: William1482
2 Replies

8. Shell Programming and Scripting

Need a script to delete previous versions of files

Hi. I need a script (either bash or perl) that can delete previous versions of files. For instance, from our continuous build process I get directories such as build5_dev_1.21 build5_dev_1.22 build5_dev_1.23 build5_dev_1.24 I need a script that I can run every night (using "at"... (6 Replies)
Discussion started by: jbsimon000
6 Replies

9. Shell Programming and Scripting

Delete files older than 2 days using shell script in Unix

I'm new to shell script.... can any one help... What is the shell script to delete the files older than 2 days ? (3 Replies)
Discussion started by: satishpabba
3 Replies

10. UNIX for Dummies Questions & Answers

How can I delete files older than 7 days?

I will like to write a script that delete all files that are older than 7 days in a directory and it's subdirectories. Can any one help me out witht the magic command or script? Thanks in advance, Odogboly98:confused: (3 Replies)
Discussion started by: odogbolu98
3 Replies
Login or Register to Ask a Question