Need a script to delete previous versions of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a script to delete previous versions of files
# 1  
Old 03-19-2009
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" command) that deletes all but the last 1 or 2, such that when the script runs tonight, if the limit is 2, I end up with only the following two directories.

build5_dev_1.23
build5_dev_1.24

if a new build is created during the day the directory ill be named

build5_dev_1.25

and after the script runs I will have directories

build5_dev_1.24
build5_dev_1.25

left.

Ideally I only need to save the "latest" build (the one with the highest "1.xx" number), but i would like the option to save "n" builds, so that I can always have the last 2 or even 3 around if I want.

I am not well versed in script building, my main job is software development, and maintaining the builds is a side task that I write scripts for when i get tired of doing the repetitive tasks by hand. As a consequence I do the scripting so seldom that I have to re-learn alot each time I start a new script. that's why I'm asking for any pointers anyone would be willing to give.

thanks !
Joe Simon
# 2  
Old 03-19-2009
If your tail program supports the +n notation
Code:
$ ls -1t | tail +2 | xargs rm -rf

Otherwise
Code:
$ ls -1t | sed -ne '3,$p' | xargs rm -rf

Test those first using "echo" instead of "rm -rf". Wouldn't want you to delete your root fs just because of a typo.
# 3  
Old 03-19-2009
Quote:
Originally Posted by pludi
If your tail program supports the +n notation
Code:
$ ls -1t | tail +2 | xargs rm -rf

Otherwise
Code:
$ ls -1t | sed -ne '3,$p' | xargs rm -rf

Test those first using "echo" instead of "rm -rf". Wouldn't want you to delete your root fs just because of a typo.

Thanks ! Great start.

there are other files in the directory other than the ones I want to delete, so I need to do some further work, but it is a good start, and much easier than I envisioned.

Thanks Lots !

Joe
# 4  
Old 03-19-2009
After further thought that didn't quite work.
The actual directory listing looks like this ( I "bolded" the directories of interest):

$ls -1t
suite6_dev_1.127/
latest_unit_test_log_dev_unit_tests.html@
suite6_dev_1.126/
ld@
lm@
lw@
latest_suite6_dev@
build_suite6_dev_1.126.log
suite6_dev_1.125/
latest_suite5.4@
build_suite5.4_1.53.log
suite5.4_1.53/
build_suite6_dev_1.125.log
build_suite5.4_1.52.log
suite5.4_1.52/
latest_suite5_dev@
build_suite5_dev_1.625.log
suite5_dev_1.625/

After the command
$ ls -1t | grep suite6 | sort | tail -2 | xargs echo
suite6_dev_1.126/ suite6_dev_1.127/


What i want is suite6_dev_1.125/ (and any else before, for instance suite6_dev_1.124 if it exists also), I can then use xargs rm -rf instead of xargs echo.

Any ideas of how to get the files that I want ?
The output of
$ls -1t | grep suite6 | sort

build_suite6_dev_1.125.log
build_suite6_dev_1.126.log
build_suite6_dev_1.127.log
latest_suite6_dev@
suite6_dev_1.125/
suite6_dev_1.126/
suite6_dev_1.127/

I need to modify the grep to match the file names starting with "suite6" that will get just the directories that i want. Then I need to get the names of all but the last 2.

Thanks again
Joe
# 5  
Old 03-19-2009
OK,

$ls -1t | grep ^suite6 | sort
suite6_dev_1.125/
suite6_dev_1.126/
suite6_dev_1.127/

This gets me the list of files I am interested in.
I want to now remove all but the last 2 directories in this list.

So, I can get that list into a shell variable, and iterate over that list, removing all but the last two. Is that the best way to finish this off ?

Thanks
Joe
# 6  
Old 03-19-2009
Why exactly did you put that grep and sort in there. What you want to do can be easily accomplished by ls alone.
Code:
$ ls -1td suite6_dev_* | sed -ne '3,$p' | xargs echo

# 7  
Old 03-19-2009
Quote:
Originally Posted by pludi
Why exactly did you put that grep and sort in there. What you want to do can be easily accomplished by ls alone.
Code:
$ ls -1td suite6_dev_* | sed -ne '3,$p' | xargs echo

Thanks !
that is perfect.

(sorry about the grep and sort, It didn't click off hand of how to get ls to do this.).

the line you gave gives me exactly what I want. I just have to get add it to another script that already runs every night at midnight and that will take care of that manual process.

Thanks Again !
Joe
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy the previous month files using shell script?

could you please assist the below query. i had written the below piece of code to copy the files from one directory to another. For current month files had been copied ,unfortunately the previous month files not copied. Please find the below directory structure:- ls -lrt total 1824... (2 Replies)
Discussion started by: venkat918
2 Replies

2. 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

3. Shell Programming and Scripting

making code compatible to previous bash versions

First let me explain the scenario I have tywo files as usual file1.txt (it has n rows and 8 columns) $1 $2 $3 $4 $5 $6 $7 $8 Code: 1234567|iufgt|iuoy|iout|white |black |red |90879 1234567|iufgt|iuoy|iout|green |pink |blue |90879... (3 Replies)
Discussion started by: s.deepak
3 Replies

4. Shell Programming and Scripting

Need to delete previous lines

Need to delete the line which is directly above any line which has 3 fields in it. one two three one two three four five six four five six seven eight nine seven eight nine one two three should output: one two three (7 Replies)
Discussion started by: linuxkid
7 Replies

5. UNIX for Dummies Questions & Answers

sample script to archive & move previous day syslog files

hi all. Please help me with archiving previous day syslog files. the files have no extension and have the format YYYY-MM-DD. I want to archive the file then move it to some other machine. thanks. (2 Replies)
Discussion started by: coolatt
2 Replies

6. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: zzstore
5 Replies

7. Shell Programming and Scripting

shell script preserving last 2 versions of files

I need some help with the logic and syntax for a shell script (ksh) that will search a directory and look for similar files and save only the last two versions. The version number is in the file name. However, the files are of varying name lengths and may have 1 or many files, with no limit to... (6 Replies)
Discussion started by: synergy_texas
6 Replies
Login or Register to Ask a Question