当前位置: 首页 > article >正文

Linux学习 重定向 管道 流

  1. 重定向 管道 流

    在 Linux 中一个命令的去向可以有 3 个地方:终端、文件、作为另外一个命令的入参。

    而命令一般都是通过键盘输入,然后输出到终端、文件等地方,它的标准用语是 stdin 、 stdout 以及 stderr 。

    标准输入stdin,终端接收键盘输入的命令,会产生两种输出;

    标准输出stdout,终端输出的信息(不包含错误信息);

    标准错误输出stderr ,终端输出的错误信息。

    1. 重定向

      把本来要显示在终端的命令结果,输送到别的地方(到文件中或者作为其他命令的输入)。

    2. 输出重定向 >
      >表示重定向到新的文件,root@hcss-ecs-c2b8:~# ll > /var/test/root_files.txt,它表示将ll命令列出的根目录下所有文件夹信息重定向到/var/test/root_files.txt文件。

      root@hcss-ecs-c2b8:~# ll
      total 124
      drwx------ 10 root root  4096 Sep 24 21:15 ./
      drwxr-xr-x 24 root root  4096 Sep 20 00:12 ../
      drwx------  3 root root  4096 Jul 22  2023 .ansible/
      drwxr-xr-x  2 root root  4096 Jul 22  2023 .ansible_async/
      -rw-r--r--  1 root root  8925 Sep 23 23:45 .bash_history
      -rw-r--r--  1 root root  3144 Jul 22  2023 .bashrc
      drwx------  2 root root  4096 Feb 10  2023 .cache/
      drwxr-xr-x  7 root root  4096 Jul 22  2023 docker-library/
      -rw-r--r--  1 root root 21926 Jul 22  2023 get-docker.sh
      -rw-r--r--  1 root root  1326 Jul 21  2023 githubclone.sh
      -rw-------  1 root root     0 Feb 10  2023 .history
      -rwxr-xr-x  1 root root 15353 Jul 22  2023 HSSInstall*
      drwxr-xr-x  8 root root  4096 Jul 22  2023 library/
      -rw-r--r--  1 root root   161 Jul  9  2019 .profile
      drwx------  4 root root  4096 Jul 22  2023 snap/
      drwx------  2 root root  4096 Sep 19 23:43 .ssh/
      drwxr-xr-x  9 root root  4096 Jul 22  2023 stackhub/
      -rw-------  1 root root 10517 Sep 23 23:23 .viminfo
      -rw-r--r--  1 root root   173 Jul 22  2023 .wget-hsts
      -rw-------  1 root root   118 Sep 24 21:15 .Xauthority
      # 将上述目录重定向到一个文本文件中
      root@hcss-ecs-c2b8:~# ll > /var/test/root_files.txt
      root@hcss-ecs-c2b8:~# cat /var/test/root_files.txt 
      total 124
      drwx------ 10 root root  4096 Sep 24 21:15 ./
      drwxr-xr-x 24 root root  4096 Sep 20 00:12 ../
      drwx------  3 root root  4096 Jul 22  2023 .ansible/
      drwxr-xr-x  2 root root  4096 Jul 22  2023 .ansible_async/
      -rw-r--r--  1 root root  8925 Sep 23 23:45 .bash_history
      -rw-r--r--  1 root root  3144 Jul 22  2023 .bashrc
      drwx------  2 root root  4096 Feb 10  2023 .cache/
      drwxr-xr-x  7 root root  4096 Jul 22  2023 docker-library/
      -rw-r--r--  1 root root 21926 Jul 22  2023 get-docker.sh
      -rw-r--r--  1 root root  1326 Jul 21  2023 githubclone.sh
      -rw-------  1 root root     0 Feb 10  2023 .history
      -rwxr-xr-x  1 root root 15353 Jul 22  2023 HSSInstall*
      drwxr-xr-x  8 root root  4096 Jul 22  2023 library/
      -rw-r--r--  1 root root   161 Jul  9  2019 .profile
      drwx------  4 root root  4096 Jul 22  2023 snap/
      drwx------  2 root root  4096 Sep 19 23:43 .ssh/
      drwxr-xr-x  9 root root  4096 Jul 22  2023 stackhub/
      -rw-------  1 root root 10517 Sep 23 23:23 .viminfo
      -rw-r--r--  1 root root   173 Jul 22  2023 .wget-hsts
      -rw-------  1 root root   118 Sep 24 21:15 .Xauthority
      
      

      这样做的好处是,当我们在启动某项服务或者查询大量信息室,在终端中展示起来很麻烦我们就可以使用重定向到文件的方法,来记录这样大量的信息.

      【注意】使用>要注意,如果输出的文件不存在它会新建一个,如果输出的文件已经存在,则会覆盖。因此执行这个操作要非常小心,以免覆盖其它重要文件。

    3. 输出重定向 >>

      表示重定向到文件末尾,因此它不会像 > 命令这么危险,它是追加到文件的末尾(当然如果文件不存在,也会被创建)。

      # 这里也可以看出 这个重定向命令是会先去创建文件夹,再去输出
      root@hcss-ecs-c2b8:/var/test# ll > files.txt
      root@hcss-ecs-c2b8:/var/test# cat files.txt 
      total 28
      drwxr-xr-x  3 root root 4096 Sep 24 21:24 ./
      drwxr-xr-x 14 root root 4096 Sep 21 08:28 ../
      -rwxr-xr-x  1 root root  628 Sep 23 23:23 compose_file.sh*
      drwxr-xr-x  2 root root 4096 Sep 23 23:25 dir1/
      -rwxr-xr-x  1 root root  473 Sep 22 10:07 file_move.sh*
      -rw-r--r--  1 root root    0 Sep 24 21:24 files.txt
      -rw-r--r--  1 root root 1071 Sep 24 21:16 root_files.txt
      -rw-r--r--  1 root root  987 Sep 23 23:24 test.tar.gz
      # 这里当我们使用>>时,就是进行追加写入了,没有将之前的覆盖掉
      root@hcss-ecs-c2b8:/var/test# ll >> files.txt
      root@hcss-ecs-c2b8:/var/test# cat files.txt 
      total 28
      drwxr-xr-x  3 root root 4096 Sep 24 21:24 ./
      drwxr-xr-x 14 root root 4096 Sep 21 08:28 ../
      -rwxr-xr-x  1 root root  628 Sep 23 23:23 compose_file.sh*
      drwxr-xr-x  2 root root 4096 Sep 23 23:25 dir1/
      -rwxr-xr-x  1 root root  473 Sep 22 10:07 file_move.sh*
      -rw-r--r--  1 root root    0 Sep 24 21:24 files.txt
      -rw-r--r--  1 root root 1071 Sep 24 21:16 root_files.txt
      -rw-r--r--  1 root root  987 Sep 23 23:24 test.tar.gz
      total 32
      drwxr-xr-x  3 root root 4096 Sep 24 21:24 ./
      drwxr-xr-x 14 root root 4096 Sep 21 08:28 ../
      -rwxr-xr-x  1 root root  628 Sep 23 23:23 compose_file.sh*
      drwxr-xr-x  2 root root 4096 Sep 23 23:25 dir1/
      -rwxr-xr-x  1 root root  473 Sep 22 10:07 file_move.sh*
      -rw-r--r--  1 root root  426 Sep 24 21:24 files.txt
      -rw-r--r--  1 root root 1071 Sep 24 21:16 root_files.txt
      -rw-r--r--  1 root root  987 Sep 23 23:24 test.tar.gz
      
      

      当我们每次启动一个服务时,不希望之前服务运行的日志被覆盖掉就可以使用这个重定向到文件尾的方式.

    4. 输出重定向 2> 和 2>>

      刚刚提到过,有两种输出方式,这里介绍第二种,标准错误输出

      当我们输入的命令有无,或者报错的时候,如下

      root@hcss-ecs-c2b8:/var/test# 1
      1: command not found
      root@hcss-ecs-c2b8:/var/test# not_exist_command
      not_exist_command: command not found
      root@hcss-ecs-c2b8:/var/test# ll -asda asdasd
      ls: cannot access 'asdasd': No such file or directory
      root@hcss-ecs-c2b8:/var/test# 
      

      输入错误命令的下一行都会有对应的报错,这个就是标准错误输出

      当我们编写的脚本或者服务出现问题时,我们希望能苟将其记录进一个错误日志error.log的文件之中,我们这样这样来做

      # 查看一个不存在的目录
      root@hcss-ecs-c2b8:/var/test# ll not_exist_dir 2>error.log 
      root@hcss-ecs-c2b8:/var/test# cat error.log 
      ls: cannot access 'qweq': No such file or directory
      root@hcss-ecs-c2b8:/var/test# ll not_exist_dir 2>>error.log 
      root@hcss-ecs-c2b8:/var/test# ll not_exist_dir 2>>error.log 
      root@hcss-ecs-c2b8:/var/test# cat error.log 
      ls: cannot access 'not_exist_dir': No such file or directory
      ls: cannot access 'not_exist_dir': No such file or directory
      
    5. 输出重定向 2>&1

      标准输出和标准错误输出都重定向都一个地方,注意这个无论是追加还是覆盖都只有一个>符号,由标准输出的符号决定输出方式

      # 用cat命令打开一个不存在的文件
      root@hcss-ecs-c2b8:/var/test# cat not_exit_file > res.txt 2>&1
      root@hcss-ecs-c2b8:/var/test# cat res.txt 
      cat: not_exit_file: No such file or directory
      root@hcss-ecs-c2b8:/var/test# cat not_exit_file > res.txt 2>&1
      root@hcss-ecs-c2b8:/var/test# cat res.txt 
      cat: not_exit_file: No such file or directory
      root@hcss-ecs-c2b8:/var/test# cat not_exit_file >> res.txt 2>&1
      root@hcss-ecs-c2b8:/var/test# cat res.txt 
      cat: not_exit_file: No such file or directory
      cat: not_exit_file: No such file or directory
      

      目前为止,我们接触的命令的输入都来自命令的参数,其实命令的输入还可以来自文件或者键盘的输入。

    6. 输入重定向 <

      <符号用于指定命令的输入。
      
      cat < res.txt # 指定命令的输入为 res.txt 
      

      虽然它的运行结果与cat name.csv一样,但是它们的原理却完全不同。

      cat res.txt表示cat命令接收的输入是res.txt文件名,那么要先打开这个文件,然后打印出文件内容。

      cat < res.txt 表示cat命令接收的输入直接是res.txt这个文件的内容,cat命令只负责将其内容打印,打开文件并将文件内容传递给 cat命令的工作则交给终端完成。

    7. 输入重定向 <<

      将键盘的输入重定向为某个命令的输入。

    8. 管道|

      把两个命令连起来使用,一个命令的输出作为另外一个命令的输入,英文是 pipeline,可以想象一个个水管连接起来,管道算是重定向流的一种。

      这里举个简单的例子

      root@hcss-ecs-c2b8:/var/test# ll | grep -E "*.txt"
      -rw-r--r--  1 root root 1278 Sep 24 21:25 files.txt
      -rw-r--r--  1 root root  117 Sep 24 21:33 file.txt
      -rw-r--r--  1 root root   92 Sep 24 21:53 res.txt
      -rw-r--r--  1 root root 1071 Sep 24 21:16 root_files.txt
      -rw-r--r--  1 root root  681 Sep 24 22:06 sort.txt
      # 这里管道符将ll命令的输出作为grep命令的输入并将grep命令的输出重定向到txt_type_files.text文件中
      root@hcss-ecs-c2b8:/var/test# ll | grep -E "*.txt" > txt_type_files.text
      root@hcss-ecs-c2b8:/var/test# cat txt_type_files.text 
      -rw-r--r--  1 root root 1278 Sep 24 21:25 files.txt
      -rw-r--r--  1 root root  117 Sep 24 21:33 file.txt
      -rw-r--r--  1 root root   92 Sep 24 21:53 res.txt
      -rw-r--r--  1 root root 1071 Sep 24 21:16 root_files.txt
      -rw-r--r--  1 root root  681 Sep 24 22:06 sort.txt
      -rw-r--r--  1 root root    0 Sep 24 22:10 txt_type_files.text
      
      

http://www.kler.cn/news/318232.html

相关文章:

  • 前端组件库Element UI 的使用
  • 深入解析:Kubernetes 如何使用 etcd 作为配置中心和注册中心
  • 鸿蒙OpenHarmony【小型系统基础内核(进程管理调度器)】子系统开发
  • 【爬虫】PlayWright使用说明
  • 如何查看docker 镜像的sha256值
  • Python编码系列—Python模板方法模式:定义算法骨架,让子类实现细节
  • Element Plus图片上传组件二次扩展
  • 《探索云原生与相关技术》
  • 【nvm管理多版本node】下载安装以及常见问题和解决方案
  • 分发饼干00
  • 苹果手机邮箱添加阿里云邮箱的设置步骤
  • Tomcat 乱码问题彻底解决
  • BigDecimal的使用
  • Vue.js props 子组件可以从父组件接收数据(通过 props)并可以向父组件发送事件(通过 $emit)
  • 力扣206.反转链表
  • 24/9/24 算法笔记 超参数优化算法
  • 模型验证 — 交叉验证Cross Validation的方法
  • 大数据新视界 --大数据大厂之算法在大数据中的核心作用:提升效率与智能决策
  • 【linux】file命令
  • 数据篇| 关于Selenium反爬杂谈
  • Python3将Excel数据转换为文本文件
  • 【鸿蒙 HarmonyOS NEXT】组件嵌套滚动:nestedScroll
  • 【html】基础(二)
  • 量化交易----数据透视表----融资融券优惠代码
  • 响应式布局-媒体查询父级布局容器
  • spring boot导入多个配置文件
  • #C++ enum枚举
  • Qt/C++ 多线程同步机制详解及应用
  • Shiro-550—漏洞分析(CVE-2016-4437)
  • 详解QT插件机制