Mailinglist Archive: opensuse (1476 mails)

< Previous Next >
Re: [opensuse] How to delete everything but the newest 5 files?
On 03/31/2012 09:06 PM, Andreas wrote:
Hi,

I'd like to let cron clean up a directory that holds hourly backups.
So there are 24 new files every day.

I'd need some scripting that sorts the content of this directory and
then removes everything but the 5 newest files.

Is there a howto or even sample code that does this?
If your directory contains only files, something like this should work:

rm `ls -St | tail +6`

ls -St sorts the directory contents by time and tail +6 prints the file
starting from 6th line

Regards,
-- Mark


--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
References