58

I'm trying to pipe grep output to rm, but it outputs useless stuff. Is any switch required for rm? Or can rm can be provided a regexp directly?

ls | grep '^\[Daruchini'| rm

rm: missing operand Try `rm --help' for more information.

1
  • 1
    Always copy-paste error messages. (I've inserted the actual error message for the benefit of future readers. But I could only do this because there's only one possible error message in this case. Most of the time, it's vital to have the exact error message to understand the problem. So always copy-paste all error messages in full.) Commented Mar 18, 2011 at 21:53

6 Answers 6

81

You need to use xargs to turn standard input into arguments for rm.

$ ls | grep '^Dar' | xargs rm

(Beware of special characters in filenames; with GNU grep, you might prefer

$ ls | grep -Z '^Dar' | xargs -0 rm

)

Also, while the shell doesn't use regexps, that's a simple pattern:

$ rm Dar*

(meanwhile, I think I need more sleep.)

3
  • 2
    Assuming the asker didn't make a mistake when (s)he posted the question, (s)he's after rm Dar*, not rm !(Dar*). Commented Mar 18, 2011 at 21:55
  • ...right, yes, I was stuck in zsh ^ mode (similar to the basg ! thing).
    – geekosaur
    Commented Mar 18, 2011 at 21:56
  • 2
    +1 Great answer, I had to add | tr "\n" "\0" before the xargs bit. Seen in this post: stackoverflow.com/questions/20307299/… Commented Jan 29, 2016 at 0:32
8

Do not parse the output of ls.

Here, it's very simple to get the shell to filter the files you want. Note that it's the shell that's expanding the pattern Dar*, not the rm command. The pattern expansion performed by the shell is called globbing.

rm Dar*

In more complex cases, look up the find command.

1
  • Late addition, but to add on to this answer, including the -i option will ask you to confirm before removing as a sanity check. Commented Oct 31, 2021 at 12:17
5

For passing output as an argument, I tend to use a while loop since I'm not familiar with xargs.

ls | grep '^Dar' | while read line; do rm "$line";done;
2
  • 1
    this is the only one that worked in my case.
    – marbel
    Commented Jun 16, 2018 at 1:31
  • Yes, this is the only one actually working properly.
    – gyurix
    Commented Sep 8, 2021 at 23:38
4

If you want to delimit the output of grep by newlines rather than whitespace, you can specify that to xargs via the -d option.

$ ls | grep '^Dar' | xargs -d '\n' rm

(as described in this question)

2

On Catalina 10.15.3, with zsh, the way I found to deal with newlines and white spaces was:

 sudo find / | grep -i 'yourString' | tr '\n' '\0' | xargs -0 -n1 rm -r 

In other words, by translating new lines with no space (tr \n to \0) before removing it with "rm -0".

Also, I always use 'find'+'grep -i' because 'find -iname' sometimes doesn't output anything.

One last note: it finds and remove files and folders that matches 'yourString'. For files only, specify "find -type f", and then the option "-r" for rm is useless.

0

The best command for me is:

find ./Daruchini* -exec rm {}\;

You must log in to answer this question.

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