Skip to content

Leave N amount of files | Delete fiels | Clean up

Leave 6 files

Bash
ls -tp | grep -v '/$' | tail -n +6 | xargs -I {} rm -- {}
Bash
ls -tp | grep -v '/$' | tail -n +6 | xargs -d '\n' -r rm --

Note: Option -r (--no-run-if-empty) ensures that rm is not invoked if there's no input.

Bash
ls -tp | grep -v '/$' | tail -n +6 | tr '\n' '\0' | xargs -0 rm --

Simple

Remove all but last recent

Bash
rm `ls -t | awk 'NR>5'`