Why doesn't rm work as I expect here?

血红的双手。 提交于 2019-12-04 14:03:41

rm doesn't take input from stdin so you can't pipe the list of files to it.

You need

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

or

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

You want to use xargs:

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

rm doesn't read a list of files from stdin, which is why it's not working. You cannot pipe a list of file names to rm.

The correct way to do this would be

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

Using the versatile find utility. If you think this seems like a lot of work: that is the price of correctness. But, really, it isn't much worse once you're familiar with find.

rm does not take its arguments from standard input. What you're probably looking for is command substitution (indicated by the backticks here), where the result of one command can be inserted into the arguments of another:

 rm -rf `ls | grep -v ".h"`
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!