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
To list only the very latest files venkatesht UNIX for Dummies Questions & Answers 3 02-23-2009 12:26 PM
Pick the latest set of files w020637 Shell Programming and Scripting 5 02-03-2009 04:50 PM
Need to delete the latest two files..Help needed satyajit007 Shell Programming and Scripting 1 01-13-2009 02:37 AM
Retain 3 latest files kamathg Shell Programming and Scripting 6 08-24-2007 06:51 AM
when I try to run rm on multiple files I have problem to delete files with space umen UNIX for Dummies Questions & Answers 1 09-20-2005 04:20 AM

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 04-03-2009
spkandy spkandy is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 13
Smile Delete all Files except the Latest Files

Hi,
In my scenario, i just want to delete all the files except the latest one.
How can I do?
Please reply me.
  #2 (permalink)  
Old 04-03-2009
nephros's Avatar
nephros nephros is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 8
Several possibilities.

'find -mtime' or 'find -ctime' will find old files - but it only has ~ one day resolution.
See man find.

Or cou could do a 'ls -lrt' which will list files sorted by date in reverse (-r) order. Combine it with head, tail, awk, xargs, rm to get your desired result.

If it's about log files you could also have a look at the logrotate package, which gives you lots of possibilities of rotating, deleting, compressing files in an automated fashion.

Hope this helps,
n.
  #3 (permalink)  
Old 04-03-2009
amitranjansahu's Avatar
amitranjansahu amitranjansahu is offline
Registered User
  
 

Join Date: Jan 2009
Location: Gurgaon,INDIA
Posts: 239
you can use -amin instead of atime or mtime ( its not supported in some OS).

or find -newer

see the man page of find.
  #4 (permalink)  
Old 04-04-2009
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 596
Quote:
In my scenario, i just want to delete all the files except the latest one.
Try:

ls -1t |awk 'NR>=2 { print $ 0; }' | xargs rm
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 02:20 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