Re: How to keep only 5 most recent files in certain folder Scott McMillan <smcm@usa.net> wrote in message news:<a6t6kvcsqnap44h71plkpu31kccn1hu9ug@4ax.com>. ..
> On Wed, 20 Aug 2003 01:37:52 GMT, scriptOmatic
> <ScriptOmatic@ChironComputing.Com> wrote:
>
> >Sinisa wrote:
> >>
> >> Hi,
> >>
> >> I am trying to make a script that would run every couple hours and purge all
> >> files in certain folder except last five files (most recent).
> >> Files are being added to that folder constantly.
> >>
> >> Can you help me ?
> >>
> >> Thanks
> >
> >use
> >/bin/ls -tr "-t" for sort by time
> > "-r" for sort reverse
> >
> >the top 5 lines will be the 5 NEWEST files, e.g:
>
> Actually, using the -r option on SCO Openserver would place the OLDEST
> files first. Just using -t will place the NEWEST files first. Since
> the OP didn't even hint at the flavor/version of his/her system but
> posted to the c.u.s.m NG I'm assuming he/she is using either SCO
> Openserver or Unixware.
>
> >
> >
> >/bin/ls -tr | head -5
> >
> >should do it
>
>
> Scott McMillan
head -5 will give you the first or newest 5.
I'd suggest
rm `ls -t * | tail +6`
you can pretty it up and handle the null set and figure out what to
do with directories if they exist.
Regards…Dan. |