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?

1
  • by 'last' do you mean oldest? Newest? – Ray Andrews Mar 11 '15 at 2:49
3

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.

1
  • That worked! Must be a quirk of more. – nw. Mar 11 '15 at 2:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.