Computing.Net > Forums > Solaris > Delete files excluding latest file

Delete files excluding latest file

Reply to Message Icon

Original Message
Name: raja76
Date: June 24, 2003 at 14:21:02 Pacific
Subject: Delete files excluding latest file
OS: Sun OS 5.6
CPU/Ram: Sun OS 5.6
Comment:

Delete files in a certain directory except the latest created file.

for example:
payroll_12102003.pdf
payroll_10102003.pdf
payroll_9102003.pdf
payroll_8202003.pdf

Here payroll_12102003.pdf is the latest file created among all files in the directory.
Now, I have to delete all files in the directory except payroll_12102003.pdf.

Any one suggest me how to skip the latest file in the directory not to delete and delete remaining files.

Thanks in advance,
Raja


Report Offensive Message For Removal


Response Number 1
Name: WilliamRobertson
Date: June 25, 2003 at 15:31:03 Pacific
Reply: (edit)

Maybe something like the following (take out the "print" to enable actual deletion):

#!/bin/ksh

function delete_all_but_first
{
shift

for f in $@
do
[[ -f $f ]] && print rm $f
done
}

delete_all_but_first $(ls -C1 -t)


Report Offensive Follow Up For Removal

Response Number 2
Name: Raja
Date: June 30, 2003 at 08:01:45 Pacific
Reply: (edit)

Thank you for the reply. It helped me alot. I was trying with awk command and not get through. Could you suggest me any books that helps to learn shell scripts. once again, thanks for the response.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Delete files excluding latest file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge