How do I delete all but 10 newest files in Linux?
Problem
I'm trying to keep a directory full of log files manageable. Nightly, I want to delete all but the 10 most recent. How can I do this in a single command?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix for: How do I delete all but 10 newest files in Linux?
For a portable and reliable solution, try this: The syntax in one of the other answers doesn't seem to work everywhere (i.e., not on my RHEL5 systems). And using or on the command line of runs the risk of splitting file names with whitespace, and exceeding the maximum commandline character limit. fixes both of these problems because it'll automatically figure out how many args it can pass within the character limit, and with the it will only split at the line boundary of the input. Technically …
Awaiting Verification
Be the first to verify this fix
Sign in to verify this fix