I am trying to delete some .
class
file from a directory. So first I have tired to count the available.class
file using the following command (after going to the directory) –$ find . -name *.class | wc -l
Here I can understand the role of pipelining (|) – the output of the
find
command/process works as a input ofwc
command (please correct me If I am wrong). The above command works fine for me and produce the correct output. But when I am trying to delete the all .class
files using the following command with pipelining –$ find . -name *.class | rm * # case-1
then it doesn’t works. It shows the following error –
rm: cannot remove `<a_directory_name>': Is a directory rm: cannot remove `<an_another_directory_name>': Is a directory
But when I use
xargs
then it works fine –$ find . -name *.class | xargs rm * # case-2
Now my question is can anyone tell me why
case-1
doesn’t works while thecase-2
works fine?.Thanks in advance.
Answer
The reason that Case-1 does not work is that rm
does not take the arguments via STDIN, it takes the arguments or the files to remove as:
rm file_1 file_2
Whereas in Case-2 xargs
takes the output of the find
command via STDIN and converts the filenames as arguments for the rm
command.
Attribution
Source : Link , Question Author : Razib , Answer Author : heemayl