Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep latest 5 files and delete the rest..

Status
Not open for further replies.

scriptfinder

Technical User
Apr 17, 2006
18
US
Hi!

Please help me find a command or a script to keep, for ex., 5 latest files and delete the rest in the directory. The order should be file creation date.

Your help is greatly appreciated.

Thank you.
 
And what have you tried so far ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Take a look at the man page for find. Look at the

-atime
-ctime
-mtime

options - choose the 0ne that is most appropriate for you.

I have a script that I run from cron that deletes files that have not been modified in 7+ days. The script looks something like this:

Code:
cd /some/dir
find ./ -mtime +7 -exec rm {} \;

If it finds any file in /some/dir that has not been modified in 7 (or more days), delete it.
 
How about? (note the ls with a one not an 'l')

ls -1t | sed '1,5d' | xargs rm 2>/dev/null


Cheers,
ND [smile]
 
Sorry about that...I should have searched the existing answers... anyway thanks everyone..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top