The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-28-2007
hyennah hyennah is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 14
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
  #2 (permalink)  
Old 03-28-2007
mona's Avatar
mona mona is offline
Registered User
  
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
To retain the latest file created with name abc*.txt and to remove the remaining,

Code:
 ls -lt abc_*.txt | awk '{if(NR!=1) print $9}' | xargs -i rm {}
Another approach. This is based on the timestamp in the filename

Code:
ls -1 abc_*.txt | sort -r | tail +2 |  xargs -i rm {}
HTH,
Mona
  #3 (permalink)  
Old 03-28-2007
hyennah hyennah is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 14
Thank You Mona...could you please explain how it ensures that the latest file is not deleted?
  #4 (permalink)  
Old 03-28-2007
mona's Avatar
mona mona is offline
Registered User
  
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
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
  #5 (permalink)  
Old 03-29-2007
hyennah hyennah is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 14
thanks! tail +2 was throwing me off...its clear now...thanks again for your time
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:50 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0