vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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 |
| |||
| 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 ? Here's one solution: cd /whereever n=5 # save $n most recent set -- `/bin/ls -t` if test $# -gt $n then shift $n /bin/rm -f "$@" fi -- Roger Cornelius racpop@tenzing.org |
| |||
| 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. Adapted from the standard way in .procmailrc to keep the latest n files in a backup directory where n is 5 to satisfy your criteria: rm -f dummy `ls -t |sed -e 1,5d` The reference to dummy is to ensure rm always has at least one filename as a parameter. -- Richard Howlett newsgroups@howie.org.uk Mail to "newsgroups" will be rejected. Mail to my forename will not. |
| ||||
| 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. |