User Tools

Site Tools


linux:bash:delete-every-file-except-the-newest-ones

Delete every file except the newest ones

Delete all but the most recent X files in bash is quite simple and is particularly useful for logs and backups scripts.

Delete old log files except the latest 10

rm `ls -t *.log|awk 'NR >10'`

Delete all the zip files except the newest 2.

rm `ls -t *.zip|awk 'NR >2'`
linux/bash/delete-every-file-except-the-newest-ones.txt · Last modified: 2014/09/12 17:15 by rik