Reputation: 371
I have a folder with some normal files. I want to sort them by their modified date, keeps the latest 20 entries, and from what's left, erases anything older than 20 days.
Here is my code to delete files those are > 20 days.
#!/bin/bash
cleanup="../some path/"
find $cleanup/*gz -mtime +20 -exec rm {} \;
However, I don't know how to keep the last modified 20 files, then doing the delete. How to fix it please?
Upvotes: 1
Views: 748
Reputation: 2376
You can filter 20 filenames out with
awk 'NR > 20'
Full command:
find ...... | awk 'NR > 20' | xargs -r rm
For example
seq 30 | xargs -i echo 'file{}' | awk 'NR > 20' | xargs -r rm
Upvotes: 1