Retaining file versions based on input parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retaining file versions based on input parameter
# 1  
Old 07-15-2013
Retaining file versions based on input parameter

Hello Forum.

I have the following files in one directory:

Code:
abc_july01_2013.txt
abc_july02_2013.txt
abc_july03_2013.txt
abc_july04_2013.txt
abc_july05_2013.txt
abc_july06_2013.txt
abc_july07_2013.txt
abc_july08_2013.txt

If I want to be able to keep the last 5 versions of the file and delete the rest so that all that remains is:

Code:
abc_july04_2013.txt
abc_july05_2013.txt
abc_july06_2013.txt
abc_july07_2013.txt
abc_july08_2013.txt

Then the following code will accomplish this:
Code:
rm -f `ls -tp abc* | grep -v "/$" | awk '{ if (NR > 5) print; }'`

But I don't want to hard-code the # of versions (which is 5 in this case). I want to be able to pass in a parameter value to the command so that it's more flexible. Is there an easy way that it can be done?

Thanks

Last edited by Scott; 07-15-2013 at 04:51 PM.. Reason: More code tags
# 2  
Old 07-15-2013
Code:
if [ -z "$1" ] 
then
        echo "Must pass in a number" >&2
        exit 1
fi

ls -tp abc* | grep -v "/$" | -v MAX="$1" awk 'NR > MAX' | xargs echo rm

Remove the echo once you've tested and are sure it does what you want.
# 3  
Old 07-15-2013
Thank you Corona688 for your quick response.

I tried your code but I'm getting the following error:

Code:
ksh: -v:  not found.

Is that -v just before the MAX variable intentional?
# 4  
Old 07-15-2013
Whoops, typo.

Code:
ls -tp abc* | grep -v "/$" | awk -v MAX="$1" 'NR > MAX' | xargs echo rm

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 07-15-2013
Thank you Fellow Canadian - it works beautifully. Smilie
This User Gave Thanks to pchang For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

3. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Get the ipaddress and align based on the input file

Hi All, I have a file contains below contents, "interfacename/subnet: public (or) interfacename/subnet:cluster_interconnect" "en2"/10.185.81.0:cluster_interconnect,"en5"/10.185.81.0:cluster_interconnect,"en6"/169.181.146.0:public... (6 Replies)
Discussion started by: kamauv234
6 Replies

5. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

6. Shell Programming and Scripting

split the file based on the 2nd column passing as a parameter

I am unable to spit the file based on the 2nd column passing as a parameter with awk command. Source file: “100”,”customer information”,”10000” “200”,”customer information”,”50000” “300”,”product information”,”40000” script: the command is not allowing to pass the parameters with the awk... (7 Replies)
Discussion started by: number10
7 Replies

7. UNIX for Dummies Questions & Answers

redirecting output retaining the input

Hi Guys, I am trying to redirect output from a file to another file while retaining the contents of the input file. For ex: cat /tmp/Out1.cfg > test.txt Now, test.txt would contain the value of Out1.cfg.,ie say for ex "12" What i require is to have the value as "/tmp/Out1.cfg 12" in the... (2 Replies)
Discussion started by: kiran1112
2 Replies

8. UNIX for Dummies Questions & Answers

Reading from a file(passing the file as input parameter)

hi I have a shell script say primary.sh . There is a file called params my scenario is primary.sh should read all the values and echo it for example i should pass like $primary.sh params output would be Abc ... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

9. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

10. Shell Programming and Scripting

How to Get the File Input from Parameter

pdir=`pwd` if ; then echo current directory $pdir ls -altr echo fi for f in $* do # directory if ; then echo current directory $f cd $f ls -latr echo fi # but you can test file/dir # regular file only if ; then echo... (4 Replies)
Discussion started by: wtolentino
4 Replies
Login or Register to Ask a Question