【Linux】重定向与通配符

一.输入输出重定向

输入重定向:把文件导入到命令中。

输出重定向:指把原本要输出到屏幕的数据消息写入到指定文件中。

输出重定向

概念

在日常学习和工作中,相较于输入重定向,我们使用输出重定向的频率更高,所以又将输出重定向分为了标准输出重定向错误输出重定向两种不同的技术,以及清空写入追加写入两种模式。这些东西都不难理解,认真看完下面的简绍相信大家会有所收获。

  • 标准输出重定向(stdout,文件描述符为1):默认输出到屏幕
  • 错误输出重定向(stderr,文件描述符为2):默认输出到屏幕

接下来让我们通过一个例子,了解这两个输出重定向的区别:

当我们查看两个文件的属性信息,其中一个文件是存在的,另一个不存在,对这两个文件的操作都可以在屏幕上输出一些数据信息,但这两个操作的差异却很大:

[root@VM-16-5-centos blogs]# touch test.c
[root@VM-16-5-centos blogs]# ll test.c   //test.c为存在的文件
-rw-r--r-- 1 root root 0 Dec 16 04:29 test.c
[root@VM-16-5-centos blogs]# ll test1.c  //test1.c为不存在的文件
ls: cannot access test1.c: No such file or directory

标准输出信息:名为test.c的文件是存在的,输出的是一些关于该文件的属性信息,也称标准输出信息。
错误输出信息:名为test1.c的文件是不存在的,执行完命令后,显示的报错提示信息,也就是错误输出信息。

输出重定向是将原本输出到屏幕上的数据,输出到指定文件中,输出标准输出信息的就是标准输出重定向,输出错误输出信息的就是错误输出重定向

概念我们已经清楚了,清空写入追加写入我们将会结合着符号的使用向大家介绍,现在让我们看一下,输出重定向用到的符号及其作用。

符号 作用
命令 > 文件 将标准输出重定向到一个文件中(清空原有文件的数据)
命令 2> 文件 将错误输出重定向到一个文件中(清空原有文件的数据)
命令 >> 文件 将标准输出重定向到一个文件中(追加原有文件的数据)
命令 2>> 文件 将错误输出重定向到一个文件中(追加原有文件的数据)
命令 >> 文件 2>&1 或 命令 &>> 文件 将标准输出和错误输出共同写入到文件中(追加到原有内容的后面)

在这里插入图片描述

其中的2就是上文提到的文件描述符,用于告诉输出重定向输出的是什么。标准输出重定向,可以省略文件描述符1不写,但是错误输出重定向必须要加文件描述符2

清空写入:只使用一个 >
追加写入:使用 >>

小试牛刀

命令 > 文件:将标准输出重定向到一个文件中(清空原有文件的数据)

[root@VM-16-5-centos blogs]# echo "hello world1" > test.c   //echo指令:打印指令。该行意为将字符串输出到test.c文件中
[root@VM-16-5-centos blogs]# cat test.c                     //cat指令:查看文件内容
hello world1
[root@VM-16-5-centos blogs]# echo "hello world2" > test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world2
[root@VM-16-5-centos blogs]# echo "hello world3" > test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world3

我们共向test.c文件输出了三行字符串,而最后查看的结果却只有最后输出的内容,这就是清空写入,在向文件中输入新的字符串时,之前的内容就会被清空。

那么我们可以使用如下对文件执行清空操作:

> 文件名      //什么都没有向文件输出,却清空了之前的所以内容

具体操作:

[root@VM-16-5-centos blogs]# echo "hello world3" > test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world3
[root@VM-16-5-centos blogs]# > test.c
[root@VM-16-5-centos blogs]# cat test.c
[root@VM-16-5-centos blogs]#

命令 2> 文件:将错误输出重定向到一个文件中(清空原有文件的数据)

[root@VM-16-5-centos blogs]# ll test1 > test.c    //没有使用文件描述符,发生报错
ls: cannot access test1: No such file or directory
[root@VM-16-5-centos blogs]# cat test.c
[root@VM-16-5-centos blogs]# ll test1 2 > test.c  //test1目录不存在
[root@VM-16-5-centos blogs]# cat test.c 
ls: cannot access test1: No such file or directory

该命令与上一条相似,只是执行的对象不同。

使用错误输出重定向去输出一个存在的文件:

[root@VM-16-5-centos blogs]# mkdir test1
[root@VM-16-5-centos blogs]# ll test1 2 > test.c
total 0
[root@VM-16-5-centos blogs]# cat test.c
[root@VM-16-5-centos blogs]# 

因为文件是真实存在的,依然会在屏幕上输出test1目录的内容,而test.c内却没有数据写入

命令 >> 文件 : 将标准输出重定向到一个文件中(追加原有文件的数据)

[root@VM-16-5-centos blogs]# echo "hello world1" >> test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world1
[root@VM-16-5-centos blogs]# echo "hello world2" >> test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world1
hello world2
[root@VM-16-5-centos blogs]# echo "hello world3" >> test.c
[root@VM-16-5-centos blogs]# cat test.c
hello world1
hello world2
hello world3

我们可以看到,原有的数据并没有被清空。

命令 2>> 文件 : 将错误输出重定向到一个文件中(追加原有文件的数据)

[root@VM-16-5-centos blogs]# > test.c              //清空文件
[root@VM-16-5-centos blogs]# ll test2 2>> test.c   //test2目录不存在
[root@VM-16-5-centos blogs]# cat test.c
ls: cannot access test2: No such file or directory
[root@VM-16-5-centos blogs]# ll test3 2>> test.c   //test3目录不存在
[root@VM-16-5-centos blogs]# cat test.c 
ls: cannot access test2: No such file or directory
ls: cannot access test3: No such file or directory

与上一条指令相似,只是执行对象不同。

命令 >> 文件 2>&1 或 命令 &>> 文件 : 将标准输出和错误输出共同写入到文件中(追加到原有内容的后面)

[root@VM-16-5-centos blogs]# > test.c                 //清空文件
[root@VM-16-5-centos blogs]# ll test3 &>> test.c      //向文件输出错误输出信息
[root@VM-16-5-centos blogs]# cat test.c
ls: cannot access test3: No such file or directory    
[root@VM-16-5-centos blogs]# ll test1 &>> test.c      //向文件输出标准输出信息
[root@VM-16-5-centos blogs]# cat test.c
ls: cannot access test3: No such file or directory
total 0

使用该指令,无需判断该消息的类型,可以直接输入到文件中,清空写入也可以使用**&**符无视文件类型,这个大家要是感兴趣或是有需要可以去尝试一下,与上面的指令格式相同。

输入重定向

输入重定向的作用是把文件导入到命令中。

输入重定向相对来说有些冷门,在工作中遇到的概率会小一点。

对于输入重定向,用到的符号及作用如下

符号 作用
命令 < 文件 将文件作为命令的标准输入
命令 << 分界符 从标准输入中读入,直到遇见分界符才停止
命令 < 文件1 > 文件2 将文件1作为命令的标准输入并将标准输出到文件2

在这里插入图片描述

输入重定向的作用是把文件直接导入到命令中。接下来使用输入重定向把文件导入wc -l命令,统计一下文件中的内容行数。

[root@VM-16-5-centos blogs]# wc test.c            //wc指令,获得文件中行数,单词数,和字符数
 1  9 52 test.c
[root@VM-16-5-centos blogs]# wc -l test.c         //wc -l指令,获得文件中的行数
1 test.c
[root@VM-16-5-centos blogs]# cat test.c | wc -l   // | 为管道符,该指令首先得出test.c的数据,在将完整的数据交给wc -l指令获得该数据的行数
1
[root@VM-16-5-centos blogs]# wc -l < test.c       //直接将文件中的数据输入到指令wc -l,获得该数据的行数
1

一切皆文件

显示数据到显示器上,就是向显示器传输数据,向显示器写入,所以我们可以把显示器看作一种文件
对比:指令 输入重定向 文件(显示器):将显示器中的信息输入到命令

C语言中的,scanf、gets等,都是从键盘上获得数据,由键盘写入,所以我们可以把键盘看作一种文件
对比:文件(键盘) 输出重定向 文件 :将键盘上的信息输入到文件

内存从键盘获得数据的过程,也就是read,对应着input(输入)到程序;
内存显示数据到显示器的过程,也就是write,对应output(输出)到显示器上。这一过程就是IO。
在这里插入图片描述
既然键盘和显示器都可以看作文件,那其他一些抽象的硬件可以吗? 可以

这就是Linux下的基本哲理之一:Linux下一切皆文件。

Linux的一切皆文件是指,Linux中一切东西都可以通过文件的方式访问、管理,任何东西都挂在文件系统之上,即使它们不是文件,也以文件的形式来呈现。

比如我们经常会讲的进程、设备、Socket,实际上都不是文件,但是你可以以文件系统的规范来访问它,修改属主和属性。

二.命令行的通配符

星号(*)

表示匹配零个或多个字符

[root@VM-16-5-centos blogs]# ll test*
-rw-r--r-- 1 root root   76 Dec 17 01:17 test1.c
-rw-r--r-- 1 root root    0 Dec 20 03:33 test2.c
-rw-r--r-- 1 root root   45 Dec 19 03:17 test.c
-rw-r--r-- 1 root root 9901 Dec 18 23:59 test.txt

test:
total 4
drwxr-xr-x 2 root root 4096 Dec 15 16:50 test1
-rw-r--r-- 1 root root    0 Dec 15 15:37 test1.c
-rw-r--r-- 1 root root    0 Dec 15 16:26 test.c

test1:
total 0

问号(?)

表示匹配单个字符

[root@VM-16-5-centos blogs]# ll test?
total 0
[root@VM-16-5-centos blogs]# ll test??
-rw-r--r-- 1 root root 45 Dec 19 03:17 test.c

中括号[]

[0-9] :表示匹配0~9之间的单个数字的字符

[abc] :表示匹配a、b、c三个字符中的一个

[!list] 或 [^list]:匹配除了list外的其他元素,可以是其他字母或数字,但只能有一个。

[root@VM-16-5-centos blogs]# ll test[0-9].[abc]
-rw-r--r-- 1 root root 76 Dec 17 01:17 test1.c
-rw-r--r-- 1 root root  0 Dec 20 03:33 test2.c

大括号{}

{string1,string2,string3…}:匹配string1 或 string2 (或其他)其中一个字符串

[root@VM-16-5-centos blogs]# ll t{
    
    es,123,.ou}t
ls: cannot access t123t: No such file or directory
ls: cannot access t.out: No such file or directory
test:
total 4
drwxr-xr-x 2 root root 4096 Dec 15 16:50 test1
-rw-r--r-- 1 root root    0 Dec 15 15:37 test1.c
-rw-r--r-- 1 root root    0 Dec 15 16:26 test.c

猜你喜欢

转载自blog.csdn.net/m0_52094687/article/details/128324237