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?

0

2 Answers 2

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.

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

3
  • I don't think xargs has a -d option.
    – erythraios
    Commented Oct 17, 2017 at 16:42
  • @erythraios xargs -d is not POSIX. But the xargs provided by GNU Findutils, which is present in GNU/Linux systems, does support it. Commented Oct 17, 2017 at 16:47
  • Either used rm New* or use find . -type f -name "New*" -print0 |xargs -0 -r rm .. both handle special char correctly (the later is recursive and it's very powerful) Commented Oct 17, 2017 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .