If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Delete all except latest 3 files in a folder

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-25-04, 06:26
aashish_bg aashish_bg is offline
Registered User
 
Join Date: Apr 2004
Location: Bangalore
Posts: 1
Delete all except latest 3 files in a folder

Hi ,
I'm a guy new to Unix.
I need help to delete files in a folder on a certain criteria :-

The problem is that i donna know the names of the files.
they are generated auto. by a diff program.
the target is just to retain the latest 3 files.
like its a sought of daily proces -- so if a script can be written for that.

like if i have files in my folder (in acsending order of timestamp -- this is imp).
a1 : created 10:45
a2 : created 10:46
a3 : created 10:47
a4 : created 10:48
a5 : created 10:49
-- i just need to retain the last 3 files (a3 , a4 , a5)
i.e. -- only the latest 3 files and delete rest all the files.
it should not do anything if there are 3 or less than 3 files in the folder.

Can anyone please help me out ..
Regards,
Reply With Quote
  #2 (permalink)  
Old 06-25-04, 07:38
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
Hi aashish,

Try this command .

First move to the particular directory.
Otherwise

rm [I]-f[/I] `ls -tr |tr -s " " |cut -d " " -f1- |tail -3`

bye
Jc
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On