Try this command:
Code:
ls -t | \
grep -v '^ *total' | \
perl -n -e 'print if ($. > 5);'
If it prints the correct list of files, you can use this to remove them:
Code:
ls -t | \
grep -v '^ *total' | \
perl -n -e 'chomp; if ($. > 5) { unlink($_) || warn "could not delete $_: $!\n"; }'
Note that this won't delete directories - you will get a warning if you try to "unlink" one.
Be careful, there is no way to undelete files removed in this way (there may be
some very hackish way to undelete depending on what filesystem you use, but success is not guaranteed).
If you want to be safer, here's a similar way which will move the files to the trash directory:
Code:
ls -t | \
grep -v '^ *total' | \
perl -n -e 'print if ($. > 5);' | \
while read f; do mv "$f" ~/.Trash; done
Note that you should make sure ~/.Trash exists before starting this, or the first file will be renamed to be a
file called ~/.Trash, and the other files will be moved
over it one at a time... at the end of the process you would end up with the last file moved called ~/.Trash!