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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Delete files excluding latest file

Reply to Message Icon

Name: raja76
Date: June 24, 2003 at 14:21:02 Pacific
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



Sponsored Link
Ads by Google

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

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)


0

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

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


IP printing jumpstart - add_install_c...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Solaris Forum Home


Sponsored links

Ads by Google


Results for: Delete files excluding latest file

Recover space used by deleted files www.computing.net/answers/solaris/recover-space-used-by-deleted-files/1336.html

delete file created before 2Hrs bac www.computing.net/answers/solaris/delete-file-created-before-2hrs-bac/3991.html

Deleted file www.computing.net/answers/solaris/deleted-file/3689.html