0

I have a group of files I'd like to remove at once.

ls | egrep \^New

The output is as expected,

New 1
New 2
New 3

but continuing the pipe with

| xargs -L rm

attempts to remove the input as space-delimited:

rm: New: No such file or directory
rm: 1: No such file or directory

What am I missing?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

2 Answers2

4

Don’t parse ls. This should do the trick:

rm New*

Your approach is failing because xargs splits arguments up on whitespace by default, so it runs rm on New, 1, New, 2 etc. You could work around that by splitting on newlines, but that won’t work with filenames containing newlines.

Stephen Kitt
  • 434,908
1

Yes you're right, xargs is breaking up the file names at the spaces. If you're using GNU xargs you can have it use a newline as the delimiter with the -d option. Example:

ls | egrep \^New | xargs -d '\n' rm

Satō Katsura
  • 13,368
  • 2
  • 31
  • 50
David Birks
  • 490
  • 6
  • 6