Retain Only specific number of file in Directory


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Retain Only specific number of file in Directory
# 1  
Old 01-05-2009
Retain Only specific number of file in Directory

I want to retain specific number of backup files in a directory.for example i want to retain only two latest backup file in backup directory. If number of backup files is greater than this policy that it will delete oldest file.Please Tell me whether this is possible or not.
# 2  
Old 01-05-2009
Possible. Sort the directory by date-time ("ls -1t") and remove the first two lines ("tail -n +3") and then remove every file in the remaining list.

If you're feeling lucky:
Code:
ls -1t | tail -n +3 | xargs rm -f

Just make sure there are no non-backup files in the folder.
# 3  
Old 01-05-2009
Great.it works....Thanks it solve my problem....Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check for a specific number of columns in a file

Hi All I have a file which has five columns in each rows. cat file.txt a|b|c|d|e 1|2|3|4|5 a1|a2|a3|a4|a5 . . . I need to make sure that there are no less than five or more than five columns (in all the rows) by mistake. I tried this : cat file.txt | awk 'BEGIN{FS="|"};{print... (3 Replies)
Discussion started by: chatwithsaurav
3 Replies

2. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

write specific line number in file

dear all, i need your advice i have sample script like this: testing.sh for i in {1..10} do echo testing $i done but i forgot create "#!/bin/bash" in above "for" so i want output will like this testing.sh #!/bin/bash for i in {1..10} do echo testing $i done (2 Replies)
Discussion started by: zvtral
2 Replies

5. Shell Programming and Scripting

How to change a number on a specific lines in a file with shell?

Hello My problem is that I want to change some specific numbers in a file. It is like, 2009 10 3 2349 21.3 L 40.719 27.388 10.8 FRO 7 0.8 1.1LFRO 2.6CFRO 1.1LMAM1 GAP=157 1.69 5.7 5.9 5.8 0.5405E+01 0.4455E+00 0.1653E+02E STAT SP IPHASW D HRMM SECON CODA AMPLIT... (11 Replies)
Discussion started by: miriammiriam
11 Replies

6. UNIX for Advanced & Expert Users

allow user to use sudo cp on a specific directory and only a specific file

Is there a way to allow a user to use sudo cp on a specific directory and only a specific file? (6 Replies)
Discussion started by: cokedude
6 Replies

7. UNIX for Dummies Questions & Answers

Comparing a number in a text file with a specific value

My project is to get a temperature reading from a refridgerator every 2 minutes and check to see if the door has been left open. I don't yet have the mastery of Linux, being a complete noob, but I reckon I need a text file with the latest temperature reading in it. This I've managed to do by... (2 Replies)
Discussion started by: Fitch
2 Replies

8. UNIX for Dummies Questions & Answers

How to replace specific number in a file

Hi, i'm trying to find a way to replace some numbers in a file, but with no luck sofar. Let's say i have file with three columns, i would like to find specific line, and replace third column with different number. search_string="oa11 /home/testfile1.log" new_number=600 test file... (4 Replies)
Discussion started by: dusoo
4 Replies

9. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

10. Shell Programming and Scripting

search for any number, and retain them in replacement

Hello! Here is what I am trying to do: I am taking a class at school that is using a lot of literature that is in the public domain, and available for free on the web. Rather than read them online in HTML, I have been pulling them down and quickly and dirtily tagging them up and typesetting... (4 Replies)
Discussion started by: ccox85
4 Replies
Login or Register to Ask a Question