Question

I want to delete *.zip or *.log files but keep last created one(newest).

For example:

text-ids.zip(12.10.2013)
text-ids.zip(13.10.2013)
..
.
text-ids.zip(19.10.2013) <-- keep this one
Was it helpful?

Solution

ls -ct *.zip | tail -n +2 | xargs rm
  • ls -ct show files, sorted by ctime
  • tail -n +2 remove first line
  • xargs rm call rm on the remaining
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top