4

Why does the following command aiming to remove recursively all .svn folders

   find . -name ".svn" | rm -rfv

doesn't work ?

I know the find command provides the -exec option to solve this issue but I just want to understand what is happening there.

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134

5 Answers5

12

In your example, the results from find are passed to rm's STDIN. rm doesn't expect its arguments in STDIN, though.

Here is an example how input redirecting works.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
2

rm does not read file names from standard input, so any data piped to it is ignored.

The only thing it uses standard input for is checking whether it's a terminal, so it can determine whether to prompt.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
1

It doesn't work because rm does not accept a list of file names on its standard input stream.

Just for reference, the safest way to handle this in the case of directories that might contain spaces is:

find . -name .svn -exec rm -frv {} \;

Or, if you are shooting for speed:

find . -name .svn -print0 | xargs -0 rm -frv
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 1
    And if you read the **rest** of the question: *"I know the `find` command provides the `-exec` option to solve this issue but I just want to understand what is happening there."*. **Edit**: Okay, now you've edited enough to deal with it. – T.J. Crowder Jan 03 '11 at 09:20
  • 1
    I would expect (without testing) that `-exec rm -frv {} +` is fastest, since it has the fewest processes. – Matthew Flaschen Jan 03 '11 at 09:23
1

find do works with | ( for example find ~ -name .svn | grep "a") but the problem is with rm

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
  • Exactly. That's because `grep` *expects* input from STDIN, whereas `rm` ignores anything piped to it and wants parameters. – Linus Kleen Jan 03 '11 at 09:27
0

This question is similar to this other answered question. Hope this helps.

How do I include a pipe | in my linux find -exec command?

Community
  • 1
  • 1
dave
  • 559
  • 3
  • 12