了解管道和重定向命令 [英] Understand pipe and redirection command

查看:87
本文介绍了了解管道和重定向命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道按我的理解管和重定向command.As,的实权| 需要一个命令结果的输出作为自身的输入。而且,> 是输出重定向帮助。如果是这样,

 找到。 -name* .SWP| RM
找 。 -name* .SWP> RM

为什么预期。对于我上面的命令表示此命令不工作


  1. 找到的所有文件,递归扩展名是.SWP在当前目录中。

  2. 采取的输出 1 并删除所有的结果文件。

仅供参考,是的,我知道如何去完成这个任务。它可以通过传递 -exec 标志来完成。

 找到。 -name* .SWP-exec RM -rf {} \\;

但正如我前面提到的,我想与&GT来完成它。 | 命令

如果我错了,在错误的方向走,请大家指正,并解释重定向和管道命令。在这里我们使用的命令?请不要提简单的例子本书我读了所有的事情。试图解释我一些复杂的事情。


解决方案

我会支持你已经证明了三种方法打破这


  • > 将重定向从所有输出找到到一个名为 RM (将无法工作,因为你只是附加到文件)。

  • | 将从输出管道找到 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

  1. Find the all files recursively whose extension is .swp in current directory .
  2. 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 from find into a file named rm (will not work, because you're just appending to a file).
  • | will pipe output from find into the rm command (will not work, because rm does not read on stdin)
  • -exec rm -rf {} \; will run rm -rf on each item ({}) that find finds (will work, because it passes the files as argument to rm).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆