Linux中,重定向和管道

Linux中的重定向和管道

llinux中的三种IO设备:
  1. 标准输入(STDIN),文件描述符号为:0,默认从键盘获取输入
  2. 标准输出(STDOUT),文件描述符号位:1,默认输出到显示终端
  3. 标准错误(STDERR),文件描述符号为:2,默认输出到显示终端

IO重定向是为了改变默认输入、输出的位置:

1. 输出重定向(标准输出STDOUT和标准错误输出STDERR)
  • 【>】标准输出覆盖重定向
  • 【>>】标准输出追加重定向
  • 【2>】标准错误输出重定向
  • 【2>>】标准错误输出追加重定向
  • 【&>】将标准输出和标准错误输出都同时重定向(相当于二次重定向【2>&1】)
  • 【&>>】将标准输出和标准错误输出都同时追加重定向
  • 【set -C】命令:启用系统禁止覆盖重定向(set -C执行后,再做重定向时,重定向文件如果存在则会报错 cannot overwrite existing file)如果在启用【set -C】命令后依然需要覆盖重定向文件,可以使用【>|】来强制覆盖
  • 【set +C】命令:解除禁止覆盖重定向(set +C执行后,重定向文件如果存在则不会报错,直接覆盖)
2. 输入重定向(标准输入)
  • 【<】输入重定向;
  • 【<
3. 管道
  • 【|】把前一个命令的执行结果当做后一个命令的输入

4. 输出重定向例子

4.1 把/etc/fstab文件内容重定向到/tmp目录下的gdy.out文件中
【cat /etc/fstab >/tmp/gdy.out】

[root@xdd ~]# cat /etc/fstab >/tmp/gdy.out
[root@xdd ~]# cat /tmp/gdy.out 

#
# /etc/fstab
# Created by anaconda on Tue Mar 19 10:24:36 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0ee2f477-c0e7-4dfb-b3d1-d980c811280d /                       xfs     defaults        0 0
UUID=92dd6792-0481-4f44-91e1-7d6741499c64 /boot                   xfs     defaults        0 0
UUID=ee923deb-0d78-4158-93c2-29d15ec99ebe /data                   xfs     defaults        0 0
UUID=5c8854b5-9f4c-41d0-baf9-90aab19c6dd8 swap                    swap    defaults        0 0
[root@xdd ~]# 

4.2 把 hello word 追加到gdy.out文件中
【echo “hello word” >>/tmp/gdy.out】

[root@xdd ~]# echo "hello word" >>/tmp/gdy.out
[root@xdd ~]# cat /tmp/gdy.out 

#
# /etc/fstab
# Created by anaconda on Tue Mar 19 10:24:36 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=0ee2f477-c0e7-4dfb-b3d1-d980c811280d /                       xfs     defaults        0 0
UUID=92dd6792-0481-4f44-91e1-7d6741499c64 /boot                   xfs     defaults        0 0
UUID=ee923deb-0d78-4158-93c2-29d15ec99ebe /data                   xfs     defaults        0 0
UUID=5c8854b5-9f4c-41d0-baf9-90aab19c6dd8 swap                    swap    defaults        0 0
hello word
[root@xdd ~]# 

4.3 设置进制覆盖重定向
【set -C】 执行后再使用覆盖重定向,会提示cannot overwrite existing file无法覆盖

[root@xdd ~]# set -C
[root@xdd ~]# echo "hello word" >/tmp/gdy.out 
-bash: /tmp/gdy.out: cannot overwrite existing file
[root@xdd ~]# 

解除禁止覆盖重定向
【set +C】

[root@xdd ~]# set +C
[root@xdd ~]# echo "hello word" >/tmp/gdy.out 
[root@xdd ~]# cat /tmp/gdy.out 
hello word
[root@xdd ~]# 

4.4 设置标准输出重定向和错误重定向
【ls abc.text gdy.text >logOk 2>logError】

[root@xdd ~]# ls
anaconda-ks.cfg  gdy  gdy.text  initial-setup-ks.cfg
[root@xdd ~]# ls abc.text gdy.text
ls: cannot access abc.text: No such file or directory
gdy.text
[root@xdd ~]# ls abc.text gdy.text >logOk 2>logError
[root@xdd ~]# ls
anaconda-ks.cfg  gdy  gdy.text  initial-setup-ks.cfg  logError  logOk
[root@xdd ~]# cat logError 
ls: cannot access abc.text: No such file or directory
[root@xdd ~]# cat logOk 
gdy.text
[root@xdd ~]# 

4.5 合并标准输出和错误输出重定向到指定文件中
【ls abc.txt gdy.text &>all.log】

[root@xdd ~]# ls abc.txt gdy.text &>all.log
[root@xdd ~]# cat all.log
ls: cannot access abc.txt: No such file or directory
gdy.text
[root@xdd ~]# 

4.6 追加标准输出和错误输出到同一文件中
【ls abc.txt gdy.text &>>all.log】

[root@xdd ~]# ls abc.txt gdy.text &>>all.log
[root@xdd ~]# cat all.log
ls: cannot access abc.txt: No such file or directory
gdy.text
ls: cannot access abc.txt: No such file or directory
gdy.text
[root@xdd ~]# 

4.7 【2>&1的使用】
【 ls abc.txt gdy.text >all.log 2>&1】命令相当于【 ls abc.txt gdy.text &>all.log】

[root@xdd ~]# ls
1  all.log  anaconda-ks.cfg  gdy  gdy.text  initial-setup-ks.cfg  logError  logOk
[root@xdd ~]# ls abc.txt gdy.text
ls: cannot access abc.txt: No such file or directory
gdy.text
[root@xdd ~]# ls abc.txt gdy.text >all.log 2>&1
[root@xdd ~]# cat all.log 
ls: cannot access abc.txt: No such file or directory
gdy.text
[root@xdd ~]# 

5.输入重定向例子

5.1 输入重定向方式,把小写字母转换成大写字母
【 tr a-z A-Z <./gdy.txt】

[root@xdd gdy]# tr a-z A-Z <./gdy.txt
WWW.MAGEDU.COM
WWW.BAIDU.COM
[root@xdd gdy]# cat gdy.txt 
www.magedu.com
WWW.BAIDU.COM
[root@xdd gdy]# 

5.2 把gdy.txt文件中的内容写入到gdy2.txt文件中
【cat gdy2.txt】
将gdy.txt文件中的内容作为标准输入传递给cat命令,重定向【cat t

[root@xdd gdy]# cat gdy2.txt
[root@xdd gdy]# cat gdy2.txt 
www.magedu.com
WWW.BAIDU.COM
[root@xdd gdy]# 

5.3 多行输入
【cat > gdy.txt < 等同于【cat <gdy.txt】
注意:cat命令本身是带输入和输出的,这里<gdy.txt为cat命令定义了输出重定向到gdy.txt文件中

[root@xdd gdy]# cat >input.txt < a
> b
> c
> END
[root@xdd gdy]# cat input.txt 
a
b
c
[root@xdd gdy]# cat <input.txt
> a
> b
> END
[root@xdd gdy]# cat input.txt 
a
b
[root@xdd gdy]# 

6管道例子

6.1 将input.txt 文件内容转换成大写输出
【echo “THIS is Test” | tr a-z A-Z】将echo输出的内容传递给tr命令,作为tr命令的输入,将输入的内容小写转换为大写

[root@xdd gdy]# echo "THIS is Test" | tr a-z A-Z
THIS IS TEST
[root@xdd gdy]# 

6.2 一页一页查看输入
【ls -l /etc | less】

[root@xdd gdy]# ls -l /etc | less
total 1428
drwxr-xr-x.  3 root root      101 Mar 19 10:26 abrt
-rw-r--r--.  1 root root       16 Mar 19 10:34 adjtime
-rw-r--r--.  1 root root     1518 Jun  7  2013 aliases
-rw-r--r--.  1 root root    12288 Mar 19 10:37 aliases.db
drwxr-xr-x.  3 root root       65 Mar 19 10:29 alsa
drwxr-xr-x.  2 root root     4096 Mar 19 10:29 alternatives
-rw-------.  1 root root      541 Apr 11  2018 anacrontab
-rw-r--r--.  1 root root       55 Oct 30 22:50 asound.conf
-rw-r--r--.  1 root root        1 Oct 31 01:10 at.deny
drwxr-x---.  3 root root       43 Mar 19 10:26 audisp
drwxr-x---.  3 root root       83 Mar 19 10:36 audit
-rw-r--r--.  1 root root    14622 Oct 31 08:17 autofs.conf
-rw-------.  1 root root      232 Oct 31 08:17 autofs_ldap_auth.conf
-rw-r--r--.  1 root root      795 Oct 31 08:17 auto.master
drwxr-xr-x.  2 root root        6 Oct 31 08:17 auto.master.d
-rw-r--r--.  1 root root      524 Oct 31 08:17 auto.misc
-rwxr-xr-x.  1 root root     1260 Oct 31 08:17 auto.net
-rwxr-xr-x.  1 root root      687 Oct 31 08:17 auto.smb
drwxr-xr-x.  4 root root       71 Mar 19 10:29 avahi
drwxr-xr-x.  2 root root     4096 Mar 19 10:29 bash_completion.d
-rw-r--r--.  1 root root     2853 Oct 31 03:48 bashrc
drwxr-xr-x.  2 root root        6 Oct 31 07:31 binfmt.d
drwxr-xr-x.  2 root root       23 Mar 19 10:26 bluetooth
:

你可能感兴趣的:(linux文件查看,文本处理)