linux数据流重定向与管道

Ouka傅 Ouka傅     2022-09-13     130

关键词:

数据流重定向简单来说就是把原本应该输出到某处(比如说屏幕)的数据,重定向其输出目的地,到其他的地方(比如文件)。

linux中的输入与输出:

  标准输入(stdin):默认从键盘输入

  标准输出(stdout):执行的正常结果信息,默认输出到屏幕

  标准错误输出(stderr):执行的错误信息,默认输出到屏幕

那就让我们来体验下这三个鬼东西吧:

[[email protected] 08:32 ~]$ ll  <-- 标准输出,默认结果输出到屏幕
总用量 24
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
drwxrwxr-x. 2 fuwh fuwh 4096 8月   3 20:34 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
[[email protected] 08:33 ~]$ ll dat  <-- 标准错误输出,默认结果输出到屏幕
ls: 无法访问dat: 没有那个文件或目录
[[email protected] 08:33 ~]$ cat name
cat: name: 没有那个文件或目录
[[email protected] 08:33 ~]$ cat he
cat: he: 没有那个文件或目录
[[email protected] 08:33 ~]$ cat > he  <-- 标准输入,默认键盘
name
go
[[email protected] 08:34 ~]$ 

 

输出或者错误输出重定向就是,我不想把这些信息输出到屏幕上了,我希望输出到文件里面之类的。

输入从定向就是我不希望从键盘输入,我希望用一个文件来作为输入之类的。

  标准输入:代码为0,使用 < 或 <<

  标准输出:代码为1,使用 > 或 >>

  标准错误输出:代码为2,使用 2> 或 2>>

  ※>>或<<表示累加,其中>就是表示1>

下面我们就来试一下:

[[email protected] 08:41 ~/stu]$ ll
总用量 0
[[email protected] 08:41 ~/stu]$ ll ../
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:41 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
[[email protected] 08:42 ~/stu]$ ll ../ > home.txt  <-- 标准输出重定向,重定向到一个文件
[[email protected] 08:42 ~/stu]$ ll ../dat
ls: 无法访问../dat: 没有那个文件或目录
[[email protected] 08:42 ~/stu]$ ll ../dat 2> err.txt <-- 标准错误输出重定向,重定向到一个文件
[[email protected] 08:42 ~/stu]$
[[email protected] 08:42 ~/stu]$ cat > c.txt
name
[[email protected] 08:44 ~/stu]$ cat c.txt
name
age[[email protected] 08:44 ~/stu]$ cat home.txt
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:42 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
[[email protected] 08:44 ~/stu]$ cat > c.txt < home.txt <-- 标准输入重定向,从文件输入
[[email protected] 08:44 ~/stu]$ cat c.txt
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:42 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
[[email protected] 08:44 ~/stu]$

如果我们想让标准输入和标准错误输入都输出到一个文件,那又应该怎么操作呢?

那这时候需要使用一种特殊写法了

[[email protected] 08:49 ~/stu]$ cat c.txt a.txt
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:42 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
cat: a.txt: 没有那个文件或目录
[[email protected] 08:49 ~/stu]$ cat c.txt a.txt > jt.txt 2>&1
[[email protected] 08:50 ~/stu]$ cat jt.txt
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:42 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
cat: a.txt: 没有那个文件或目录
[[email protected] 08:50 ~/stu]$ cat c.txt a.txt  &> jt2.txt
[[email protected] 08:51 ~/stu]$ cat jt2.txt
总用量 28
drwxrwxr-x. 2 fuwh fuwh 4096 7月  30 13:42 bin
drwxr-xr-x. 3 fuwh fuwh 4096 7月  26 12:04 data
-rw-rw-r--. 1 fuwh fuwh    8 8月   5 08:33 he
drwxrwxr-x. 2 fuwh fuwh 4096 8月   5 08:42 stu
drwxrwxr-x. 3 fuwh fuwh 4096 7月  25 10:27 stu2
-rw-r--r--. 1 root root 2439 7月  26 11:57 stu2.tar.bz2
-rw-r--r--. 1 root root  165 7月  26 11:57 stu2.tar.gz
cat: a.txt: 没有那个文件或目录
[[email protected] 08:51 ~/stu]$

 

但是,试想,如果有时候的错误信息,我们既不想输出到屏幕上,也不想保存到文件中去,那应该怎么办呢?

这个时候,就需要使用一个垃圾桶黑洞的装置了。

[[email protected] 08:53 ~/stu]$ ll
总用量 20
-rw-rw-r--. 1 fuwh fuwh 367 8月   5 08:44 c.txt
-rw-rw-r--. 1 fuwh fuwh  52 8月   5 08:42 err.txt
-rw-rw-r--. 1 fuwh fuwh 367 8月   5 08:42 home.txt
-rw-rw-r--. 1 fuwh fuwh 407 8月   5 08:51 jt2.txt
-rw-rw-r--. 1 fuwh fuwh 407 8月   5 08:50 jt.txt
[[email protected] 08:54 ~/stu]$ ll > /dev/null
[[email protected] 08:54 ~/stu]$ ll
总用量 20
-rw-rw-r--. 1 fuwh fuwh 367 8月   5 08:44 c.txt
-rw-rw-r--. 1 fuwh fuwh  52 8月   5 08:42 err.txt
-rw-rw-r--. 1 fuwh fuwh 367 8月   5 08:42 home.txt
-rw-rw-r--. 1 fuwh fuwh 407 8月   5 08:51 jt2.txt
-rw-rw-r--. 1 fuwh fuwh 407 8月   5 08:50 jt.txt
[[email protected] 08:54 ~/stu]$

 

管道:|

  管道命令就是将上一个命令的标准输出作为下一个命令的标准输入

[[email protected] 08:57 ~/stu]$ ll
总用量 0
[[email protected] 08:57 ~/stu]$ touch a.txt
[[email protected] 08:57 ~/stu]$ touch ab.txt
[[email protected] 08:57 ~/stu]$ ll
总用量 0
-rw-rw-r--. 1 fuwh fuwh 0 8月   5 08:57 ab.txt
-rw-rw-r--. 1 fuwh fuwh 0 8月   5 08:57 a.txt
[[email protected] 08:57 ~/stu]$ ll | cat > c.txt
[[email protected] 08:57 ~/stu]$ ll
总用量 4
-rw-rw-r--. 1 fuwh fuwh   0 8月   5 08:57 ab.txt
-rw-rw-r--. 1 fuwh fuwh   0 8月   5 08:57 a.txt
-rw-rw-r--. 1 fuwh fuwh 154 8月   5 08:57 c.txt[[email protected] 08:58 ~/stu]$ cat c.txt
总用量 0
-rw-rw-r--. 1 fuwh fuwh 0 8月   5 08:57 ab.txt
-rw-rw-r--. 1 fuwh fuwh 0 8月   5 08:57 a.txt
-rw-rw-r--. 1 fuwh fuwh 0 8月   5 08:57 c.txt
[[email protected] 08:58 ~/stu]$
[[email protected] 08:59 ~/stu]$ ll a.txt d.txt | cat > f.txt
ls: 无法访问d.txt: 没有那个文件或目录

可以发现,这时候标准错误输出是没有被管道传送的。

 

linux管道符重定向与环境变量

——《Linux就该这么学》笔记输入输出重定向输入重定向  指把文件导入到命令中输出重定向  指把原本要输出到屏幕的数据信息写入到指定文件中输出重定向  分为标准输出重定向和错误输出重定向  每种有清空写入... 查看详情

linux重定向与管道

Linux——重定向与管道系统的输入输出包括:默认输入设备:标准输入,STDIN,描述符为0默认输出设备:标准输出,STDOUT,描述符为1             标准错误输出,STDERR,描述符为2 ... 查看详情

linux管道符和重定向与环境变量

一丶输入输出重定向概念:输入重定向指把文件导入到命令中,输出重定向指把原本输出到屏幕的信息写入指定文件中.种类:1.标准输入重定向STDIN:默认键盘输入,也可以从其他文件或命令中输入文件描述为:02.标准输出重定向STDOUT:默... 查看详情

linux重定向与管道

重定向redirection 每个命令有输入源和输出目的地,默认行为,是标准输入和标准输出。大多数情况,标准输入是键盘,标准输出是屏幕。可以为单独的操作修改输入和输出,这就是重定向。重定向可以使某个命令从源文件输... 查看详情

管道符重定向与环境变量(代码片段)

...件!部署一个服务等于在修改服务的配置文件!?输入重定向STDIN,文件描述0<将文件作为命令的标准输入输出重定向STDOUT,文件描述1">"清空原文件数据的标准输出重定向到一个文件中(清空写入)">>"在原... 查看详情

linux入门——重定向与管道

 Linux给程序提供三种I/O设备      –标准输入(STDIN)-0默认接受来自键盘的输入      –标准输出(STDOUT)-1默认输出到终端窗口      –标准错误(STDERR)-... 查看详情

linux学习第一周;标准输入输出和错误重定向与管道

标准输入标准输出标准错误重定向:<#标准输入重定向,默认为键盘输入,利用<可以将文件代替键盘输入>#标准输出重定向(会覆盖1.text里面的内容);例子:ls>/data/1.text(将ls命令输出结果重定向道1.text中)>>#标... 查看详情

linux的重定向管道与环境变量path(代码片段)

(文章目录)一、重定向1.输出重定向:>1.写入指定文件[root@VM-8-8-centoslesson5]#catfile.txt[root@VM-8-8-centoslesson5]#echo"helloworld">file.txt[root@VM-8-8-centoslesson5]#catfile.txthelloworld[root@VM-8-8-ce 查看详情

管道与重定向(有重定向习题未做)

在介绍管道与重定向之前,先了解一下Linux默认提供的三个特殊装备,用于终端的显示和输出:  stdin:标准输入,对应于在终端的输入;  stdout:标准输出,对应于终端的输出;  stderr:标准错误输出,对应于终端的输... 查看详情

第五章linux重定向和管道

重定向和管道标准输入和输出:程序:指令+数据读入数据:Input输出数据:Output打开的文件都有一个fd:filedescriptor(文件描述符)Linux给程序提供三种I/O设备标准输入(STDIN)-0默认接受来自键盘的输入标准输出(STDOUT)-1默认输... 查看详情

linux学习笔记之管道重定向与正则表达式

...hello,word”|tee/tmp/hello.out例#wc-l/etc/passwd|cut-d‘‘-f1I/O重定向  >     输出重定向,覆盖输出&nbs 查看详情

linux第三天重定负管道符环境变量

1?输入输出重定向简而言之,输入重定向是指把文件导入到命令中,而输出重定向则是指把原本要输出到屏幕的数据信息写入到指定文件中。在日常的学习和工作中,相较于输入重定向,使用输出重定向的频率更高,所以又将输... 查看详情

3.管道符重定向与环境变量(代码片段)

第3章管道符、重定向与环境变量章节概述:目前为止,我们已经学习了数十个常用的Linux系统命令,如果不能把这些命令进行组合使用,则无法提升工作效率。本章首先讲解与文件读写操作有关的重定向技术的5种模式—标准覆... 查看详情

linux入门-7linux管道重定向以及文本处理

Linux管道、重定向以及文本处理1Linux多命令协作:管道及重定向管道和重定向2Linux命令行文本处理工具文件浏览基于关键字搜索-grep基于列处理文本-cut文本统计-wc文本排序-sort删除重复行文本比较-diff检查拼写-aspell处理文本内容-t... 查看详情

linux下的io重定向与管道相关的知识简析(代码片段)

一、bash重定向部分简单翻译1.1、bash手册关于重定向短短的注解(因为过于经典,所以摘录出来)我的翻译要开始毁经典啦...参考:https://blog.csdn.net/spch2008/article/details/51433353/https://www.cnblogs.com/lqminn/archive/2013/05/30/3108283.htmlhttps://bash.... 查看详情

linux基础入门--io重定向及管道

...示器、文件系统上的常规文件输出保存、网卡等;程序的数据流有三种:  输入 查看详情

管道符重定向与环境变量

管道符、重定向与环境变量管道符用于把前一个命令原本要输出到屏幕的数据当作后一个命令的标准输出。例如使用翻页的形式查看/etc目录中的文件列表及其属性输入输出的重定向管道符作用的对象是命令和命令,重定向作用... 查看详情

linux中io重定向和管道(代码片段)

IO重定向和管道根据冯诺依曼原理的知识,计算机运行有数据流的输入和输出,称之为IO.Linux中一切皆文件思想,表现为具体的文件。在linux中打开的文件都有一个fd(FileDescriptor):文件描述符程序:指令+数据读入数据:Input输出数... 查看详情