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?

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.

0

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

  • I don't think xargs has a -d option. – erythraios Oct 17 '17 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. – Eliah Kagan Oct 17 '17 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) – Franklin Piat Oct 17 '17 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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