linux - Delete all except the most recent n files of each prefix (based on file date)? -


i'm running command in script, , it's deleting 5 of each file, it's not keeping last 5 files based on date:

ls -rv | awk -f_ '++n[$1]>5' | xargs rm 

**underscore delimiter between prefix (hostname) , filenames.

i've tried other stuff too, didn't seem trick.

i'm running delete last n files in folder , works correctly:

rm -f $(ls -1t -i "*.sh" | tail -n +21) 

this applies though.

i have folder full of backups various devices have hostname prefixes. format is: hostname_config.backup-timestamp.

@rici solved it. dumping -rv flags , using -t created behavior looking for.

ls -t | awk -f_ '++n[$1]>5' | xargs rm 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -