linux的重定向

简介: 对于重定向的一些理解

@TOC

一、重定向

1.输出重定向:>

1.写入指定文件

[root@VM-8-8-centos lesson5]#  cat file.txt
[root@VM-8-8-centos lesson5]# echo "hello world" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
hello world
[root@VM-8-8-centos lesson5]# cat file.txt > test.c
[root@VM-8-8-centos lesson5]#  cat test.c
hello world

将 cat file.txt默认到显示器上的内容 显示到了 test.c文件中

2. 覆盖写

[root@VM-8-8-centos lesson5]# cat file.txt
hello world
[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you

file.txt文件的原来内容是 hello world,被变成了 you can see me

将原来的文件内容清空,再重新写

2.追加重定向 :>>

[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
[root@VM-8-8-centos lesson5]# echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
you can see me
[root@VM-8-8-centos lesson5]#  echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]# cat file.txt
you can see you
you can see me
you can see me

把file.txt文件的内容 you can see me 打印后,

使用 >> 发现会在文件结尾 追加内容

3.输出重定向:<

1.键盘显示

[root@VM-8-8-centos lesson5]# cat
abcdefhgjkl
abcdefhgjkl

cat 不跟文件,默认从键盘读到什么就显示什么。

2.文件显示

使用 < 变为 从 指定文件中读取数据


         

c

[root@VM-8-8-centos lesson5]# cat < file.txt

you can see you

you can see me

you can see me

[root@VM-8-8-centos lesson5]#  cat file.txt

you can see you

you can see me

you can see me

>cat < file.txt 与 cat file.txt等价
>cat < file.txt :**从fille.txt文件中读取数据**

4.重定向的一些认知误区

1. test.c只显示错误的

find /home -name test.c > msg.c

```

寻找 主目录中的 test.c文件 并重定向到 msg .c文件中

在这里插入图片描述

发现只能显示出权限不够而不能访问的 即错误的

2. msg.c只显示正确的

打印 cat msg.c文件 只显示正确的

在这里插入图片描述

结论:显示器输出的信息中,有正确的,也有错误的,

只把正确的进行了重定向

3.分析

  在这里插入图片描述  

标准输出 和 标准错误输出 都是在显示器上打印,是两个不同的文件

所以 >只重定向 标准输出

find /home -name test.c > msg.c

默认重定向 是 find /home -name test.c 1> msg.c

只不过把代码是1省略了 ,而代码1对应标准输出

4.显示出正确的

find /home -name test.c  2> msg.c

这里就代表将代码为2重定向到 msg.c文件,代码2代表标准输出

此时 test.c只显示正确的

在这里插入图片描述
目录
相关文章
|
3月前
|
Unix Linux C语言
Linux重定向:深入理解与实践
Linux重定向:深入理解与实践
|
3月前
|
Linux
Linux dup函数文件重定向
dup函数创建一个新的文件描述符,该新文件描述符和原有文件描述符file_descriptor指向相同的文件、管道或者网络连接。并且dup返回的文件描述符总是取系统当前可用的最小整数值。dup2和dup类似,不过它将返回第一个不小于file_descriptor_two的整数值。dup和dup2系统调用失败时返回-1并设置errno。有时我们希望把标准输入重定向到一个文件,或者把标准输出重定向到一个网络连接(比如CGI编程)。
19 0
|
4月前
|
Linux Shell PHP
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
linux(十五)文件和目录相关命令-控制台输出命令echo和重定向
48 0
|
5月前
|
弹性计算 运维 Shell
Linux加强篇003-管道符、重定向与环境变量
山重水复疑无路,柳暗花明又一村
248 1
|
6月前
|
Unix Shell Linux
|
存储 Shell Linux
Linux--文件(2)-重定向和文件缓冲
Linux--文件(2)-重定向和文件缓冲
|
2月前
|
Linux C++
认识Linux指令之 “ 重定向” 符号
认识Linux指令之 “ 重定向” 符号
认识Linux指令之 “ 重定向” 符号
|
2月前
|
Linux C语言
Linux:重定向
Linux:重定向
73 1
|
2月前
|
Unix Shell Linux
在Unix/Linux Shell中,管道(`|`)和重定向
在Unix/Linux Shell中,管道(`|`)和重定向
23 1
|
2月前
|
Linux
如何将 Linux 命令输出重定向到文件?
如何将 Linux 命令输出重定向到文件?
212 1