giving grep output to rm

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.)


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.


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;

Tags:

Bash

Grep

Rm