2017文集

Linux:输入输出重定向

2017-04-22  本文已影响9人  停下浮躁的心

输入输出重定向

[space@space Desktop]$ touch heiha
[space@space Desktop]$ ls -l heiha 
-rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
[space@space Desktop]$ ll heiha > re.txt  #标准输出重定向 会覆盖文件中原有内容
[space@space Desktop]$ cat re.txt
-rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
[space@space Desktop]$ ll heiha >> re.txt    # >> 标准输出追加重定向 将输出内容追加到re.txt中
[space@space Desktop]$ cat re.txt 
-rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
[space@space Desktop]$ cat -n re.txt 
     1  -rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
     2  -rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
     3  -rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
[space@space Desktop]$ more re.txt 
-rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
[space@space Desktop]$ ll xxx
ls: cannot access xxx: No such file or directory
[space@space Desktop]$ ll xxx >>re.txt       #还是会显示在屏幕上 因为不是标准输出
ls: cannot access xxx: No such file or directory
[space@space Desktop]$ ll xxx 2>>re.txt  #错误输出追加重定向 不会覆盖文件中原有内容
[space@space Desktop]$ more re.txt 
-rw-rw-r--. 1 space space 0 Apr 17 16:57 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
-rw-rw-r--. 1 space space 47 Apr 17 16:59 heiha
ls: cannot access xxx: No such file or directory
[space@space Desktop]$ ll xxx 2>re.txt           #错误输出重定向 会覆盖文件中原有内容
[space@space Desktop]$ more re.txt 
ls: cannot access xxx: No such file or directory
2>  错误输出重定向
>   标准输出重定向
>>  标准输出追加重定向
2>> 错误输出追加重定向
<   标准输入重定向
[space@space Desktop]$ cat i.txt 
/etc
[space@space Desktop]$ wc -l < i.txt >re.txt # 文件作为命令的标准输入
[space@space Desktop]$ cat re.txt 
1
[space@space Desktop]$ ls < i.txt >re.txt   # 文件中的内容并不能作为命令的参数去执行    这里 < i.txt 不起作用
[space@space Desktop]$ cat re.txt 
a.txt
b.txt
heiha
i
i.txt
j
re.txt
上一篇 下一篇

猜你喜欢

热点阅读