Why doesn't it work, as I expect here?

I just want to do a simple thing. I got the following files in a directory:

AppInterface.h  baa  PEMsg.h  PluginInterface.h

Then I issue the command:

ls | grep -v ".h" | rm -rf

To my horror, baait is not removed. But this:

ls | grep -v ".h"

gives baaas I expect. So I think the problem is with how it rmaccepts input, but I don’t know why. Tried this in both csh and bash.

+3
source share
4 answers

rm does not accept input from stdin, so you cannot pass a list of files to it.

You need

 rm `ls | grep -v ".h"`

or

ls | grep -v ".h" | xargs rm -rf
+8
source

Do you want to use xargs :

ls | grep -v ".h" | xargs rm -rf
+4
source

rm stdin, . rm.

:

find . -maxdepth 1 -not -name '*.h' -exec rm -rf {} +

Using the universal find utility . If you think this looks like a lot of work: this is the price of correctness. But, really, this is not much worse if you are familiar with the search.

+2
source

rmdoes not accept its arguments from standard input. What you're probably looking for is command substitution (backlinks are listed here) where the result of one command can be inserted into the arguments of another:

 rm -rf `ls | grep -v ".h"`
+1
source

Source: https://habr.com/ru/post/1778695/


All Articles