为什么 rm 不能按我的预期工作?
我只想做一件简单的事。我在目录中得到了以下文件:
AppInterface.h baa PEMsg.h PluginInterface.h
然后我发出命令:
ls | grep -v ".h" | rm -rf
令我沮丧的是,baa
没有被删除。但是,这:
ls | grep -v ".h"
正如我所期望的那样给出了 baa 。所以我猜问题在于 rm 如何接受输入,但我不知道为什么。在 csh 和 bash 中都尝试过这个。
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
Much to my dismay, baa
does not get deleted. But, this:
ls | grep -v ".h"
gives baa
as I expect. So I guess the problem is with how rm
takes input but I don't know why. Tried this both in csh and bash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
rm 不从标准输入获取输入,因此您无法将文件列表通过管道传递给它。
你需要
或
rm doesn't take input from stdin so you can't pipe the list of files to it.
You need
or
您想使用 xargs:
You want to use xargs:
rm
不会从标准输入读取文件列表,这就是它不起作用的原因。您无法通过管道将文件名列表传递给rm
。正确的方法是
使用多功能的 find 实用程序。如果您认为这看起来工作量很大:这就是正确性的代价。但是,实际上,一旦您熟悉了 find,情况也不会更糟。
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 torm
.The correct way to do this would be
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
不从标准输入获取参数。您可能正在寻找的是命令替换(此处由反引号表示),其中一个命令的结果可以插入到另一个命令的参数中: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: