![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| get latest file | inquirer | Shell Programming and Scripting | 4 | 04-02-2009 12:15 PM |
| How to find the latest file on Unix or Linux | duke0001 | UNIX for Dummies Questions & Answers | 10 | 11-04-2008 04:31 AM |
| Getting latest file from ftp | arunavlp | UNIX for Dummies Questions & Answers | 3 | 02-08-2008 10:19 AM |
| script to find latest executable in particular directory and start that particular ex | kvineeth | Shell Programming and Scripting | 6 | 09-24-2007 08:19 AM |
| Unix shell scripting to find latest file having timestamp embedded... | kaushik25 | AIX | 2 | 08-06-2007 11:42 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Find and remove all but the latest file
Hi,
Would appreciate if someone could help me with the following requirement. Say I have a directory where a file called abc_$timestamp.txt is created couple of times in a day. So this directory would have files like abc_2007-03-28-4-5-7.txt abc_2007-03-28-3-5-7.txt abc_2007-03-28-6-5-7.txt abc_2007-03-28-5-5-7.txt I need a script to search abc_*.txt and delete all but the latest file. I know i could use a find command find . type -f name -"abc_*" -exec rm{} but , how would i keep the latest file |
|
|||||
|
In the first command, I am taking the files with name "abc_*.txt". Since i have used -t in the ls command, the file which is created recently will always come first. Then awk will pass the filenames other than the first record(this is the file we need to retain) to the xargs command to delete.
Second command is also quite similar. ls will take all the files starting with "abc_*.txt" and they are sorted by filename in descending order. The file with the latest timestamp will come first and the tail command will filter the first file(which is the latest) and pass the remaining filenames to the xargs to delete. Hope my explanation is clear |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|