了解管道和重定向命令 [英] Understand pipe and redirection command
问题描述
我想知道按我的理解管和重定向command.As,的实权|
需要一个命令结果的输出作为自身的输入。而且,>
是输出重定向帮助。如果是这样,
找到。 -name* .SWP| RM
找 。 -name* .SWP> RM
为什么预期。对于我上面的命令表示此命令不工作
- 找到的所有文件,递归扩展名是.SWP在当前目录中。
- 采取的输出
1
并删除所有的结果文件。
仅供参考,是的,我知道如何去完成这个任务。它可以通过传递 -exec
标志来完成。
找到。 -name* .SWP-exec RM -rf {} \\;
但正如我前面提到的,我想与&GT来完成它。
或 |
命令
如果我错了,在错误的方向走,请大家指正,并解释重定向和管道命令。在这里我们使用的命令?请不要提简单的例子本书我读了所有的事情。试图解释我一些复杂的事情。
我会支持你已经证明了三种方法打破这
-
>
将重定向从所有输出找到
到一个名为RM 文件code>(将无法工作,因为你只是附加到文件)。
-
|
将从输出管道找到
到RM
命令(不会起作用,因为RM
不上阅读标准输入
) -
-exec RM -rf {} \\;
将运行RM -rf
,在每个项目({}
)的找到
认定(将工作,因为它把文件作为参数传递给RM
)。
您将需要使用 -exec
标志或者管道进入的xargs
命令(男人xargs的
),而不是 |
或>
,以达到所期望的行为
编辑:为@dmckee说,你也可以使用 $()
运营商串插,即:室射频$(找到。-name* .SWP)
(如果你有大量的文件,由于参数长度的限制,这将失败)。
I want to understand the real power of pipe and redirection command.As per my understanding,|
takes the output of one command result as the input of itself. And, >
is helps in output redirecting .If it is so,
find . -name "*.swp" | rm
find . -name "*.swp" > rm
why this command is not working as expected .For me above command means
- Find the all files recursively whose extension is .swp in current directory .
- take the output of
1.
and remove all whose resulted files .
FYI,yes i know how to accomplish this task . it can be done by passing -exec
flag .
find . -name "*.swp"-exec rm -rf {} \;
But as I already mentioned,i want to accomplish it with >
or |
command.
If i was wrong and going in wrong direction,please correct me and explain redirection and pipe command. Where we use whose commands ? please dont mention simple book examples i read all whose thing . try to explain me some complicated thing .
I'll break this down by the three methods you have shown:
>
will redirect all output fromfind
into a file namedrm
(will not work, because you're just appending to a file).|
will pipe output fromfind
into therm
command (will not work, becauserm
does not read onstdin
)-exec rm -rf {} \;
will runrm -rf
on each item ({}
) thatfind
finds (will work, because it passes the files as argument torm
).
You will want to use -exec
flag, or pipe into the xargs
command (man xargs
), not |
or >
in order to achieve the desired behavior.
EDIT: as @dmckee said, you can also use the $()
operator for string interpolation, ie: rm -rf $(find . -name "*.swp")
(this will fail if you have a large number of files, due to argument length limits).
这篇关于了解管道和重定向命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!