2

I accumulate sequentially named files as the result of an automated process, and I would like to keep only the last 10 around. I tried this, which had the effect of deleting everything:

ls -r | more +11 | xargs rm

Apparently piping more +n causes the entire result to be piped, without regard to the +n argument.

What would be the correct approach?

nw.
  • 123

1 Answers1

2

I'll admit, nothing in the more documentation jumps out at me as saying that this will happen.  But, since you want to start displaying (outputting / processing) the input (list of files) at a specified line number (specifically, 11), the logical command to use is tail:

ls -r | tail -n +11 | …

A word to the wise: test things like this by piping into cat or xargs (with no command) before you do something dangerous like xargs rm.