intelli78 intelli78 - 3 months ago 5x
Bash Question

Why doesn't the shell command find . -name "*.txt" | rm remove all .txt files in the current directory?

I am new to the shell. I tried to use the following command to remove all .txt files in the current directory.

find . -name "*.txt" | rm

I realize this isn't the most straightforward way to remove all .txt files -- it was just an experiment with pipes. But it just returns usage information for rm... doesn't do what I expected.

Wherein lies my misunderstanding? Thank you!


You should be using xargs to treat the piped-in data as arguments:

find . -name "*.txt" | xargs rm

Of course, there are easier ways to do this:

find . -name '*.txt' -exec rm {} \;

rm *.txt

Note that the latter only deletes files in the current directory, whereas using find does a recursive search.