Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
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.) –  Gilles Mar 18 '11 at 21:53
add comment

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
1  
Assuming the asker didn't make a mistake when (s)he posted the question, (s)he's after rm Dar*, not rm !(Dar*). –  Gilles Mar 18 '11 at 21:55
    
...right, yes, I was stuck in zsh ^ mode (similar to the basg ! thing). –  geekosaur Mar 18 '11 at 21:56
    
thanks geekosaur –  CodeBladeRunner Mar 18 '11 at 22:09
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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