mini-script to list all files except the 'n' newest

Translation of the original in Catalan. Read it in: CatalàCastellano

The idea: we want to keep a directory with the n newest files, the rest should be deleted as we go... the simplest script of all: (let's assume n=10)

rm -f `ls -t | sed -e '1,10d'`

If the list of files to delete is too long we could also do:

for F in `ls -t | sed -e '1,10d'`; do rm -f $F; done

Example use: to save the last n received emails to the mail repository as a backup and keep deleting the rest.

A small, fast and good sed manual you can find at Selectorweb.com. (local )

2 comments

Comments from the old blog, as they were written. Comments are closed.

  1. —

    Compte amb els "for f" tractant fitxers, que si els noms de fitxers tenen espais no funcionen ;)

  2. —

    [1]

    tens raó gràcies per recordar-ho... sempre se m'oblida aquesta parida i depsrés em dona pel sac.