Linux标准IO和管道

                Linux标准IO和管道

                                           作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.标准输入和输出

程序:指令+数据  
  读入数据:Input
  输出数据:Output

打开的文件都有一个fd: file descriptor (文件描述符)

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

I/O重定向:改变默认位置
[root@node101.yinzhengjie.org.cn ~]# ll /dev/std*                  #这个是系统固定的三个描述符,可用很清楚的看到相应数字对应的链接设备。
lrwxrwxrwx. 1 root root 15 Aug  6 12:27 /dev/stderr -> /proc/self/fd/2
lrwxrwxrwx. 1 root root 15 Aug  6 12:27 /dev/stdin -> /proc/self/fd/0
lrwxrwxrwx. 1 root root 15 Aug  6 12:27 /dev/stdout -> /proc/self/fd/1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /proc/self/fd               #我们查看当前终端被映射成了3个数字,而这3个数字有对应了一个终端连接,我们可用使用tty命令查看。
total 0
lrwx------. 1 root root 64 Aug 14 09:39 0 -> /dev/pts/2
lrwx------. 1 root root 64 Aug 14 09:39 1 -> /dev/pts/2
lrwx------. 1 root root 64 Aug 14 09:39 2 -> /dev/pts/2
lr-x------. 1 root root 64 Aug 14 09:39 3 -> /proc/67458/fd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tty                       #我们可用查看当前正在使用的终端
/dev/pts/2
[root@node101.yinzhengjie.org.cn ~]# 
查看I/O设备方式详情(请使劲儿戳我)

 

二.把输出和错误重新定向到文件

STDOUT和STDERR可以被重定向到文件  
  格式为:"命令 操作符号 文件名",支持的操作符号包括: 
    >  把STDOUT重定向到文件 
    2> 把STDERR重定向到文件 
    &> 把所有输出重定向到文件 

> 文件内容会被覆盖      
  set  -C  禁止将内容覆盖已有文件,但可追加      
  >| file 强制覆盖      
  set +C  允许覆盖 

>>  原有内容基础上,追加内容 
 

2>   覆盖重定向错误输出数据流 

2>> 追加重定向错误输出数据流 

标准输出和错误输出各自定向至不同位置   如:"COMMAND > /path/to/file.out 2> /path/to/error.out" 

合并标准输出和错误输出为同一个数据流进行重定向  
  &>   覆盖重定向  
  &>>  追加重定向  
  COMMAND > /path/to/file.out 2>&1 (顺序很重要,具体看下面的案例)  
  COMMAND >> /path/to/file.out 2>&1 

():合并多个程序的STDOUT  ( cal 2007 ; cal 2008 ) > all.txt 

1>.将标准输出重定向案例

使用xshell连接一个终端:
[root@node101.yinzhengjie.org.cn ~]# tty                #查看当前终端为/dev/pts/1"
/dev/pts/1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uname -r 1> /dev/pts/2     #注意,此处的"1"可用缺省,因为默认值就是"1",我们这样操作是将标准输出到"/dev/pts/2"这个终端上去。
[root@node101.yinzhengjie.org.cn ~]# 


同时使用xshell复制一个终端在执行以上操作:
[root@node101.yinzhengjie.org.cn ~]# tty                #查看当前终端为"/dev/pts/2"
/dev/pts/2
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 3.10.0-957.el7.x86_64     #不难发现,这里有相应的这里的确有输出,这个输出是"/dev/pts/1"终端重定向过来的。
[root@node101.yinzhengjie.org.cn ~]# uname -r 1> /dev/pts/2          #注意,此处的"1"可用缺省,因为默认值就是"1",我们这样操作是将标准输出到"/dev/pts/2"这个终端上去。

2>.将标准错误重定向案例

开启一个终端:
[root@node101.yinzhengjie.org.cn ~]# aaa                  #当前操作系统没有"aaa"这个命令,于是有相应的报错提示~
bash: aaa: command not found...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tty                  #查看当前终端
/dev/pts/1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# aaa 2> /dev/pts/2          #我们把错误输出重定向到"/dev/pts/2"这个设备上,此时错误输出就不会在当前终端输出啦~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 


同时在执行上述操作前开启另外一个终端:
[root@node101.yinzhengjie.org.cn ~]# tty
/dev/pts/2
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bash: aaa: command not found...    #熟悉不?这个报错我们在"/dev/pts/1"中已经看到过来,很明显发现报错信息是一致的~
[root@node101.yinzhengjie.org.cn ~]# aaa 2> /dev/pts/2            #我们把错误输出重定向到"/dev/pts/2"这个设备上,此时错误输出就不会在当前终端输出啦~

3>.使用重定向">"覆盖写文件内容案例

[root@node101.yinzhengjie.org.cn ~]# df -h | grep boot          #我们可用查看一下boot分区的总大小才1G空间
/dev/sda1               1014M  179M  836M  18% /boot
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# dd if=/dev/zero of=/boot/bigfile bs=1M count=800        #我们创建一个1G的大文件
800+0 records in
800+0 records out
838860800 bytes (839 MB) copied, 0.279534 s, 3.0 GB/s
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h /boot/bigfile         #查看创建的文件大小
-rw-r--r--. 1 root root 800M Aug 14 10:16 /boot/bigfile
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# df -h | grep boot          #发现在"/boot"目录下创建了一个大文件立马使用率就上去了
/dev/sda1               1014M  979M   36M  97% /boot
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# >/boot/bigfile             #我们使用">"重定向文件内容,即会将"/boot/bigfile"文件内容覆盖,由于我们什么命令都没有写直接使用了">",因此文件内容会被清空。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# df -h | grep boot          #不难发现,文件内容很轻松的就被情况啦
/dev/sda1               1014M  179M  836M  18% /boot
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -f /boot/bigfile         #情况文件后,我们再去删除文件名称即可,再生产环境中如果有大文件正在被使用(如vim)我们直接使用rm命令删除文件名称,文件的确是被删除了,但磁盘空间并没有立即释放出来。这种情况如果数据文件不重要的话就可用考虑先将文件清空然后在删除文件的方案。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# >/boot/bigfile               #我们使用">"重定向文件内容,即会将"/boot/bigfile"文件内容覆盖,由于我们什么命令都没有写直接使用了">",因此文件内容会被清空。
[root@node101.yinzhengjie.org.cn ~]# cat b.txt                 #查看"b.txt"文件内容
jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# set -C                   #禁止将内容覆盖已有文件,但可用追加
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# >b.txt                    #由于我们的"b.txt"文件已经存在啦,上面使用禁用覆盖的设置,因此这里我们想要覆盖文件的时候会有报错的提示哟~
-bash: b.txt: cannot overwrite existing file
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "2019" >> b.txt           #很明显覆盖不能使用但追加是可用成功的
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
jason
2019
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# set -C                   #禁止将内容覆盖已有文件,但可用追加
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
jason
2019
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# >b.txt                   #我们发现文件无法被覆盖
-bash: b.txt: cannot overwrite existing file
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# set +C                  #我们也可用设置允许覆盖选项
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# >b.txt                   #发现文件是可用被覆盖的啦~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# set +C                   #我们也可用设置允许覆盖选项
[root@node101.yinzhengjie.org.cn ~]# cat b.txt                   #查看源文件内容
jason
2019
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# set -C                    #我们这里设置禁止覆盖文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "尹正杰到此一游" > b.txt          #很明显配置生效了,我们没法进行覆盖操作
-bash: b.txt: cannot overwrite existing file
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "尹正杰到此一游" >| b.txt        #虽然设置了禁止覆盖文件,但是我们可用使用">|"命令来进行强行覆盖操作
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
尹正杰到此一游
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "尹正杰到此一游" >| b.txt       #虽然设置了禁止覆盖文件,但是我们可用使用">|"命令来进行强行覆盖操作

4>.使用重定向">>"追加写文件内容案例

[root@node101.yinzhengjie.org.cn ~]# ls                    #查看"ls"命令的输出结果
b.txt  my_web  name.txt  smiley.jpg  test.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# who                    #查看"who"命令的输出结果
root     :0           2019-08-06 12:27 (:0)
root     pts/0        2019-08-06 12:29 (:0)
root     pts/1        2019-08-06 12:29 (172.30.1.1)
root     pts/3        2019-08-14 10:15 (172.30.1.1)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls >> output.log            #我们将"ls"命令的输出结果追加到一个名叫"output.log"的文件中,如果文件不存在就创建,如果文件存在并不会覆盖源文件内容而是以追加写的方式写入。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat output.log             #很明显内容的确被我们追加到文件中去了
b.txt
my_web
name.txt
output.log
smiley.jpg
test.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# who >> output.log            #紧接着,我们又将另一个命令的结果也追加到文件中去
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat output.log              #再次查看文件内容,发现命令有所不同,文件并没有清空而是紧接着上一次文件末尾继续追加内容
b.txt
my_web
name.txt
output.log
smiley.jpg
test.txt
root     :0           2019-08-06 12:27 (:0)
root     pts/0        2019-08-06 12:29 (:0)
root     pts/1        2019-08-06 12:29 (172.30.1.1)
root     pts/3        2019-08-14 10:15 (172.30.1.1)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls >> output.log             #我们将"ls"命令的输出结果追加到一个名叫"output.log"的文件中,如果文件不存在就创建,如果文件存在并不会覆盖源文件内容而是以追加写的方式写入。

5>.把所有输出重定向到指定文件案例

[root@node101.yinzhengjie.org.cn ~]# ls /etc/passwd /etc/yinzhengjie 1>/root/stdout.log 2>/root/stderr.log        #我们分别将标准输出和错误输出重定向到不同的文件中
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /etc/passwd /etc/yinzhengjie &>/root/all.log                     #将所有输出都重定向到一个文件中,包括标准输出和错误输出
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 12
-rw-r--r--. 1 root root 74 Aug 14 10:55 all.log
-rw-r--r--. 1 root root 62 Aug 14 10:54 stderr.log
-rw-r--r--. 1 root root 12 Aug 14 10:54 stdout.log
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat stdout.log                                         #查看标准输出文件内容
/etc/passwd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat stderr.log                                         #查看错误输出文件内容
ls: cannot access /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat all.log                                           #查看收纳所有输出的文件内容
ls: cannot access /etc/yinzhengjie: No such file or directory
/etc/passwd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /etc/passwd /etc/yinzhengjie &>/root/all.log           #将所有输出都重定向到一个文件中,包括标准输出和错误输出(这种操作相对较新,CentOS7以上的版本基本上已经支持)
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/yinzhengjie
cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue /etc/yinzhengjie >/root/all.log 2>&1        #其实我们这样设置也可以将标准错误和标准输出都重定向到指定的文件中哟~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat all.log 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue /etc/yinzhengjie >/root/all.log 2>&1        #其实我们这样设置也可以将标准错误和标准输出都重定向到指定的文件中哟~(这个操作比较古老)
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/yinzhengjie
cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]#         
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue /etc/yinzhengjie 2>/root/stderr.log 1>&2      #注意,我这里是将标准输出重定向到错误输出里面啦,其实原理和上面的案例类似。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat stderr.log 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue /etc/yinzhengjie 2>/root/stderr.log 1>&2      #注意,我这里是将标准输出重定向到错误输出里面啦,其实原理和上面的案例类似。
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/yinzhengjie
cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue /etc/yinzhengjie 2>&1 >/root/all.log       #如果我们这样执行,会将标准错误输出到当前屏幕,将标准输出重定向到"/root/all.log"文件中去啦!
cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /root/all.log 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# (cat /etc/issue /etc/yinzhengjie 2>&1)>/root/all.log    #我们也可以这样写,优先执行小括号里面的指令,会将标准错误(2)的输出重定向到标准输出(1)中,然后再将结果重定向到文件中去。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /root/all.log 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

cat: /etc/yinzhengjie: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# (cat /etc/issue /etc/yinzhengjie 2>&1)>/root/all.log        #我们也可以这样写,优先执行小括号里面的指令,会将标准错误(2)的输出重定向到标准输出(1)中,然后再将结果重定向到文件中去。
[root@node101.yinzhengjie.org.cn ~]# passwd yinzhengjie &> /dev/null                   #我们可以将不想再屏幕输出的提示信息直接丢给"黑洞文件",数据会被彻底丢失!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /dev/null                               #我们发现,"黑洞文件"和明显他也是一个字符设备
crw-rw-rw-. 1 root root 1, 3 Aug  6 12:27 /dev/null
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# passwd yinzhengjie &> /dev/null                   #我们可以将不想再屏幕输出的提示信息直接丢给"黑洞文件",数据会被彻底丢失!

6>.将多个命令的标准输出重定向到指定文件案例

[root@node101.yinzhengjie.org.cn ~]# hostname
node101.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.0-957.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# (hostname;uname -r) > res.log            #我们可用将多个命令的执行结果重定向到指定的文件中,需要使用小括号和将命令包裹起来且命令之间用分号隔开。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat res.log 
node101.yinzhengjie.org.cn
3.10.0-957.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# (hostname;uname -r) > res.log    #我们可用将多个命令的执行结果重定向到指定的文件中,需要使用小括号和将命令包裹起来且命令之间用分号隔开。

7>.标准输入的案例(更多标准输入可参考tr命令)

[root@node101.yinzhengjie.org.cn ~]# echo {1..10} | tr ' ' '+'  > output.log      #我们先将表达式存入一个文件中
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat output.log 
1+2+3+4+5+6+7+8+9+10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bc < output.log                     #我们使用标准输入的方式读取文件的内容,将这个内容传递给bc工具,来计算得到相应的结果,我们发现在安装很多项目的时候都需要我们手动导入数据库的表结果,其实就是使用标准输入来创建对应的表而已。
55
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bc < output.log             #我们使用标准输入的方式读取文件的内容,将这个内容传递给bc工具,来计算得到相应的结果,我们发现在安装很多项目的时候都需要我们手动导入数据库的表结果,其实就是使用标准输入来创建对应的表而已。
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/issue            #我们也可用使用cat命令来接受标准输入 
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/issue            #我们也可用使用cat命令来接受标准输入
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/issue > /etc/issue_bak        #我们很轻松就能用cat实现文件的拷贝功能,其本质就是利用了标准输入和标准输出
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/issue_bak
terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/issue > /etc/issue_bak  #我们很轻松就能用cat实现文件的拷贝功能,其本质就是利用了标准输入和标准输出
[root@node101.yinzhengjie.org.cn ~]# cat > res.log               #将cat命令标准输入的数据重定向到一个文件中去
尹正杰到此一游!   
https://www.cnblogs.com/yinzhengjie/
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat res.log 
尹正杰到此一一游!
https://www.cnblogs.com/yinzhengjie/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat > res.log               #将cat命令标准输入的数据重定向到一个文件中去(单行重定向,一行重定向一次)
使用root用户登录服务器:
[root@node101.yinzhengjie.org.cn ~]# mail -s hi yinzhengjie      #注意,这里的"-s"选项是用来去确定邮件的标题的,而"yinzhengjie"表示发送给哪个用户的哟
尹正杰到此一游!
https://www.cnblogs.com/yinzhengjie/
.  #我们这里输入一个点(".")来表示邮件正文的结束
EOT
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mail -s h2 yinzhengjie < /etc/issue      #除了上面的发送方式外,我们还可用使用mail的标准输入来发送文件内容。
[root@node101.yinzhengjie.org.cn ~]# 


使用普通用户登录服务器
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ mail                    #不难发现我们介绍到了2封邮件
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/yinzhengjie": 2 messages 1 new
    1 root                  Wed Aug 14 12:48  20/716   "hi"
>N  2 root                  Wed Aug 14 12:50  29/769   "h2"
& 2                                               #我们需要在这里输入相应的数字,才能查看对应的邮件信息
Message  2:
From root@node101.yinzhengjie.org.cn  Wed Aug 14 12:50:02 2019
Return-Path: <root@node101.yinzhengjie.org.cn>
X-Original-To: yinzhengjie
Delivered-To: yinzhengjie@node101.yinzhengjie.org.cn
Date: Wed, 14 Aug 2019 12:50:02 +0800
To: yinzhengjie@node101.yinzhengjie.org.cn
Subject: h2
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@node101.yinzhengjie.org.cn (root)
Status: R

terminal is \l

hostname is \n

time is \t


welcome to https://www.cnblogs.com/yinzhengjie/    !!!

\S
Kernel \r on an \m

& ?                                               #我们使用"?"可以查看相应的帮助信息哟~
               mail commands
type <message list>             type messages
next                            goto and type next message
from <message list>             give head lines of messages
headers                         print out active message headers
delete <message list>           delete messages
undelete <message list>         undelete messages
save <message list> folder      append messages to folder and mark as saved
copy <message list> folder      append messages to folder without marking them
write <message list> file       append message texts to file, save attachments
preserve <message list>         keep incoming messages in mailbox even if saved
Reply <message list>            reply to message senders
reply <message list>            reply to message senders and all recipients
mail addresses                  mail to specific recipients
file folder                     change to another folder
quit                            quit and apply changes to folder
xit                             quit and discard changes made to folder
!                               shell escape
cd <directory>                  chdir to directory or home if none given
list                            list names of all available commands

A <message list> consists of integers, ranges of same, or other criteria
separated by spaces.  If omitted, mail uses the last message typed.
& quit                                            #根据帮助信息可以看到"quit"表示退出当前交互字符界面
Held 2 messages in /var/spool/mail/yinzhengjie
[yinzhengjie@node101.yinzhengjie.org.cn ~]$ 
[root@node101.yinzhengjie.org.cn ~]# mail -s h2 yinzhengjie < /etc/issue  #除了上面的发送方式外,我们还可用使用mail的标准输入来发送文件内容。
[root@node101.yinzhengjie.org.cn ~]# cat > /etc/motd <<END      #注意,我们使用"<<终止词"命令从键盘把多行重定向给标准输入,编辑内容后需输入终止词(我这里以"END"为例,你可以用其他的自定义字符,比如"EOF"之类的,终止词一般为字母数字等)
> welcome to https://www.cnblogs.com/yinzhengjie/!!!
> good good study,day day up!
> 尹正杰到此一游!
> My hostname is $HOSTNAME
> My username is $USER
> My user id is `id -u`
> END
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/motd 
welcome to https://www.cnblogs.com/yinzhengjie/!!!
good good study,day day up!
尹正杰到此一游!
My hostname is node101.yinzhengjie.org.cn
My username is root
My user id is 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat > /etc/motd <<END          #注意,我们使用"<<终止词"命令从键盘把多行重定向给标准输入(多行重定向,直到遇到终止的标识符后才会进行一次重定向)
[root@node101.yinzhengjie.org.cn ~]# cat /etc/motd 
welcome to https://www.cnblogs.com/yinzhengjie/!!!
good good study,day day up!
尹正杰到此一游!
My hostname is node101.yinzhengjie.org.cn
My username is root
My user id is 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /etc/motd 
-rw-r--r--. 1 root root 180 Aug 14 13:58 /etc/motd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/motd >> /etc/motd       #这个操作很危险,它会死循环的将"/etc/motd"文件原内容不断的该文件末尾追加,如果我们不执行中命令会直到将磁盘容量写满为止!
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /etc/motd                 #你可心里默数5秒钟,你就会发现瞬间该文件就从180B暴涨到366MB左右啦~
-rw-r--r--. 1 root root 383728500 Aug 14 14:06 /etc/motd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h /etc/motd 
-rw-r--r--. 1 root root 366M Aug 14 14:06 /etc/motd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/motd >> /etc/motd       #这个操作很危险,它会死循环的将"/etc/motd"文件原内容不断的该文件末尾追加,如果我们不执行中命令会直到将磁盘容量写满为止!
[root@node101.yinzhengjie.org.cn ~]# ll -h /etc/motd 
-rw-r--r--. 1 root root 366M Aug 14 14:06 /etc/motd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/motd > /etc/motd       #这也是危险操作,看似将源文件内容读出在进行重定向操作,其实是会直接将文件清空的!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -h /etc/motd 
-rw-r--r--. 1 root root 0 Aug 14 14:13 /etc/motd
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat < /etc/motd > /etc/motd       #这也是危险操作,看似将源文件内容读出在进行重定向操作,其实是会直接将文件清空的!

 

三. tr命令

1>.查看tr命令的帮助信息

[root@node101.yinzhengjie.org.cn ~]# tr --help
Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,          #转换,压缩和删除字符
writing to standard output.

  -c, -C, --complement    use the complement of SET1                  #取字符集的补集
  -d, --delete            delete characters in SET1, do not translate        #删除所有属于第一个字符集的字符
  -s, --squeeze-repeats   replace each input sequence of a repeated character   #把连续重复的字符以单独一个字符表示
                            that is listed in SET1 with a single occurrence
                            of that character
  -t, --truncate-set1     first truncate SET1 to length of SET2            #将第一个字符集对应字符转换为第二字符集对应的字符
      --help     display this help and exit
      --version  output version information and exit

SETs are specified as strings of characters.  Most represent themselves.
Interpreted sequences are:

  \NNN            character with octal value NNN (1 to 3 octal digits)
  \\              backslash                          #反斜线
  \a              audible BEL                         #报警名称
  \b              backspace            
  \f              form feed
  \n              new line
  \r              return
  \t              horizontal tab
  \v              vertical tab
  CHAR1-CHAR2     all characters from CHAR1 to CHAR2 in ascending order
  [CHAR*]         in SET2, copies of CHAR until length of SET1
  [CHAR*REPEAT]   REPEAT copies of CHAR, REPEAT octal if starting with 0
  [:alnum:]       all letters and digits                    #字母和数字
  [:alpha:]       all letters                           #字母
  [:blank:]       all horizontal whitespace                  #背景色,默认全水平白色
  [:cntrl:]       all control characters                    #控制(非打印)字符                   
  [:digit:]       all digits                           #数字
  [:graph:]       all printable characters, not including space      #图形字符
  [:lower:]       all lower case letters                    #小写字母
  [:print:]       all printable characters, including space         #可打印字符
  [:punct:]       all punctuation characters                  #标点符号
  [:space:]       all horizontal or vertical whitespace           #空白字符
  [:upper:]       all upper case letters                    #大写字母
  [:xdigit:]      all hexadecimal digits                    #十六进制字符
  [=CHAR=]        all characters which are equivalent to CHAR

Translation occurs if -d is not given and both SET1 and SET2 appear.
-t may be used only when translating.  SET2 is extended to length of
SET1 by repeating its last character as necessary.  Excess characters
of SET2 are ignored.  Only [:lower:] and [:upper:] are guaranteed to
expand in ascending order; used in SET2 while translating, they may
only be used in pairs to specify case conversion.  -s uses SET1 if not
translating nor deleting; else squeezing uses SET2 and occurs after
translation or deletion.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'tr invocation'
[root@node101.yinzhengjie.org.cn ~]# 

2>.tr命令行的替换案例

[root@node101.yinzhengjie.org.cn ~]# tr 123 xyz            #将“123”转换成为"xyz"字符,注意字符数量要对应哟
123456
xyz456
654321
654zyx
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr 12345 xyz          #我这里故意让字符不对应,我们会发现他就以最后一个字符来替换未对应上的字符
123456789
xyzzz6789
654321
6zzzyx
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr -t 12345 xyz        #使用"-t"选项的话,类似与python的zip拉链函数,指挥用"xyz"来替换"123",形成字符的一一对应的关系!
123456789
xyz456789
654321
654zyx
[root@node101.yinzhengjie.org.cn ~]# tr -t 12345 xyz        #使用"-t"选项的话,类似与python的zip拉链函数,指挥用"xyz"来替换"123",形成字符的一一对应的关系!

3>.大小写字母转换案例

[root@node101.yinzhengjie.org.cn ~]# tr [:lower:] [:upper:]    #我们可用将小写字母转换成大写字母
abcdef
ABCDEF

4>.删除指定字符

[root@node101.yinzhengjie.org.cn ~]# tr -d '123'          #凡是匹配到"1","2","3"对应的字符统统删除掉
a1b2c3
abc
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr -dc '123'         #和上面的案例相反,就是除了"1","2","3"对应的字符不删除,其他的都得删除掉哟,注意你输入的回车也会被删除掉哟~
a1b2c3
123[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr -d '123'          #凡是匹配到"1","2","3"对应的字符统统删除掉

5>.使用tr命令的标准输入来修改文件的内容

[root@node101.yinzhengjie.org.cn ~]# echo {1..10} > file.txt        #我们生成10个数字
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat file.txt 
1 2 3 4 5 6 7 8 9 10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr ' ' + < file.txt  > file2.txt   #我们使用tr的标准输入将file.txt的文件中空格替换成加号并标准输出到"file2.txt"文件中
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bc < file2.txt              #使用计算器的标准输入接受使用来计算"file2.txt"的内容
55
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat file.txt 
1 2 3 4 5 6 7 8 9 10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat file2.txt 
1+2+3+4+5+6+7+8+9+10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr ' ' + < file.txt > file2.txt   #我们使用tr的标准输入将file.txt的文件中空格替换成加号并标准输出到"file2.txt"文件中

6>.删除重复的字符

[root@node101.yinzhengjie.org.cn ~]# tr -s 'abc'        #我们将重复的字符删除替换成一个字符
aaabbbcccacid
abcacid
^C
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# who > who.out      
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat who.out 
root     :0           2019-08-06 12:27 (:0)
root     pts/0        2019-08-06 12:29 (:0)
root     pts/1        2019-08-06 12:29 (172.30.1.1)
root     pts/3        2019-08-14 10:15 (172.30.1.1)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr -s " " + < who.out     #我们可用把文件中的字符进行替换,比如把空格替换成加号
root+:0+2019-08-06+12:27+(:0)
root+pts/0+2019-08-06+12:29+(:0)
root+pts/1+2019-08-06+12:29+(172.30.1.1)
root+pts/3+2019-08-14+10:15+(172.30.1.1)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr -s 'abc'        #我们将重复的字符删除替换成一个字符

7.使用tr命令和管道生成随机口令

[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20      #注意,我们会得到一个20个字符的随机字符,得到的结果是没有换行符的
rqEqAsgHjGxnjidwGMem[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20
mYDV9atfQYUje8JCXgVA[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20
n1oWyZFwQRrwphifSv66[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20
te6henZAqSaY44YSy7sc[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20
RP8xsUcTLghwu511kUgW[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20
cvazinHNSnNSTmgG]Smv[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc '[0-9a-zA-Z]' | head -c20      #注意,我们会得到一个20个字符的随机字符,得到的结果是没有换行符的
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20            #我们也可以使用"openssl"命令来获取一个随机的20个字符串
hQxv7JB5glvWnwEL+If8afPRmgM=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20
cbcupRFjpMKfrHE37wRZwSB5rLU=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20
iMeVZODLYVUtc3D1qIK19fDGIZw=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20
OP0km5dlsdZq3EMMAIqg7/scs24=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20
UAkJTvH6IatKbfe2o04VCCmh9vk=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20
pigzPhlX6FNJ9824GKRR4eucKtw=
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# openssl rand -base64 20                       #我们也可以使用"openssl"命令来获取一个随机的20个字符串

 

四.管道

管道(使用符号“|”表示)用来连接命令  
  格式为:"命令1 | 命令2 | 命令3 | … "
  将命令1的STDOUT发送给命令2的STDIN,命令2的STDOUT发送到命令3的 STDIN 

STDERR默认不能通过管道转发,可利用2>&1 或 |& 实现 

最后一个命令会在当前shell进程的子shell进程中执行 

可以组合多种工具的功能 

1>.使用标准输入和标准输出来和bc工具来计算1到100数据之和

[root@node101.yinzhengjie.org.cn ~]# echo {1..100}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {1..100} > f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tr ' ' '+' < f1.txt > f2.txt 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat f2.txt 
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+
49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# bc < f2.txt 
5050
[root@node101.yinzhengjie.org.cn ~]# 

2>.使用管道功能操作

[root@node101.yinzhengjie.org.cn ~]# seq -s '+' 100                          #使用"-s"选线来指定分隔符
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+
49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq -s '+' 1 2 100                 #1-100之间的所有奇数
1+3+5+7+9+11+13+15+17+19+21+23+25+27+29+31+33+35+37+39+41+43+45+47+49+51+53+55+57+59+61+63+65+67+69+71+73+75+77+79+81+83+85+87+89+91+93
+95+97+99[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq -s '+' 2 2 100                   #1-100之间的所有偶数
2+4+6+8+10+12+14+16+18+20+22+24+26+28+30+32+34+36+38+40+42+44+46+48+50+52+54+56+58+60+62+64+66+68+70+72+74+76+78+80+82+84+86+88+90+92+9
4+96+98+100[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq -s '+' 100               #使用"-s"选线来指定分隔符
[root@node101.yinzhengjie.org.cn ~]# echo {1..100} | tr ' ' '+' |bc      #方案一
5050
[root@node101.yinzhengjie.org.cn ~]# seq -s '+' 100 | bc             #方案二
5050
[root@node101.yinzhengjie.org.cn ~]#

3>.将“/home”里面的文件打包,但打包的数据不是记录到文件,而是传送到“stdout”,经过管道后,将" tar -cvf - /home" 传送给后面的“tar -xvf -” , 后面的这个“-”则是取前一个命令的 stdout, 因此,就不需要使用临时file了 

[root@node101.yinzhengjie.org.cn ~]# tar -cvf - /home | tar -xvf  - 
tar: Removing leading `/' from member names
/home/
/home/yinzhengjie/
/home/yinzhengjie/.mozilla/
/home/yinzhengjie/.mozilla/extensions/
/home/yinzhengjie/.mozilla/plugins/
/home/yinzhengjie/.bash_logout
/home/yinzhengjie/.bash_profile
/home/yinzhengjie/.bashrc
/home/yinzhengjie/.cache/
/home/yinzhengjie/.cache/abrt/
/home/yinzhengjie/.cache/abrt/lastnotification
/home/yinzhengjie/.config/
/home/yinzhengjie/.config/abrt/
/home/yinzhengjie/.bash_history
home/
home/yinzhengjie/
home/yinzhengjie/.mozilla/
home/yinzhengjie/.mozilla/extensions/
home/yinzhengjie/.mozilla/plugins/
home/yinzhengjie/.bash_logout
home/yinzhengjie/.bash_profile
home/yinzhengjie/.bashrc
home/yinzhengjie/.cache/
home/yinzhengjie/.cache/abrt/
home/yinzhengjie/.cache/abrt/lastnotification
home/yinzhengjie/.config/
home/yinzhengjie/.config/abrt/
home/yinzhengjie/.bash_history
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
drwxr-xr-x. 3 root root 25 Aug  1 22:04 home
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll home/yinzhengjie/ -a
total 16
drwx------. 5 yinzhengjie yinzhengjie 128 Aug 14 12:46 .
drwxr-xr-x. 3 root        root         25 Aug  1 22:04 ..
-rw-------. 1 yinzhengjie yinzhengjie  82 Aug 14 12:47 .bash_history
-rw-r--r--. 1 yinzhengjie yinzhengjie  18 Oct 31  2018 .bash_logout
-rw-r--r--. 1 yinzhengjie yinzhengjie 193 Oct 31  2018 .bash_profile
-rw-r--r--. 1 yinzhengjie yinzhengjie 231 Oct 31  2018 .bashrc
drwxrwxr-x. 3 yinzhengjie yinzhengjie  18 Aug 14 12:45 .cache
drwxrwxr-x. 3 yinzhengjie yinzhengjie  18 Aug 14 12:45 .config
drwxr-xr-x. 4 yinzhengjie yinzhengjie  39 Aug  1 21:58 .mozilla
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tar -cvf - /home | tar -xvf -

 

五.tee命令

命令1 | tee [-a ] 文件名 | 命令2   
  把命令1的STDOUT保存在文件中,做为命令2的输入
  -a 追加

使用:
  保存不同阶段的输出
  复杂管道的故障排除
  同时查看和记录输出
[root@node101.yinzhengjie.org.cn ~]# ls | tee a.log        #我们将命令的结果再终端显示的同时,还可以重定向一份到指定文件中,如果"a.txt"文件存在则覆盖,若不存在直接创建并写入数据
f1.log
f1.txt
f2.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.log           #查看文件内容
f1.log
f1.txt
f2.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# w | tee a.log 
 14:39:56 up 8 days,  2:12,  5 users,  load average: 0.01, 0.02, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               06Aug19 ?xdm?   2days  1.41s /usr/libexec/gnome-session-binary --session gnome-classic
root     pts/0    :0               06Aug19  8days  0.05s  0.05s bash
root     pts/1    172.30.1.1       06Aug19  1:56m  2.52s  2.52s -bash
root     pts/3    172.30.1.1       10:15    4.00s  0.71s  0.01s w
yinzheng pts/4    172.30.1.1       12:46   47:24   0.05s  0.05s -bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.log             #发现文件内容被覆盖写入啦!
 14:39:56 up 8 days,  2:12,  5 users,  load average: 0.01, 0.02, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               06Aug19 ?xdm?   2days  1.41s /usr/libexec/gnome-session-binary --session gnome-classic
root     pts/0    :0               06Aug19  8days  0.05s  0.05s bash
root     pts/1    172.30.1.1       06Aug19  1:56m  2.52s  2.52s -bash
root     pts/3    172.30.1.1       10:15    4.00s  0.71s  0.01s w
yinzheng pts/4    172.30.1.1       12:46   47:24   0.05s  0.05s -bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls | tee a.log          #我们将命令的结果再终端显示的同时,还可以重定向一份到指定文件中,如果"a.txt"文件存在则覆盖,若不存在直接创建并写入数据
[root@node101.yinzhengjie.org.cn ~]# cat a.log 
 14:39:56 up 8 days,  2:12,  5 users,  load average: 0.01, 0.02, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               06Aug19 ?xdm?   2days  1.41s /usr/libexec/gnome-session-binary --session gnome-classic
root     pts/0    :0               06Aug19  8days  0.05s  0.05s bash
root     pts/1    172.30.1.1       06Aug19  1:56m  2.52s  2.52s -bash
root     pts/3    172.30.1.1       10:15    4.00s  0.71s  0.01s w
yinzheng pts/4    172.30.1.1       12:46   47:24   0.05s  0.05s -bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls | tee -a a.log         #使用tee命令的"-a"选项可以以追加的方式写入文件
a.log
f1.log
f1.txt
f2.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.log 
 14:39:56 up 8 days,  2:12,  5 users,  load average: 0.01, 0.02, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               06Aug19 ?xdm?   2days  1.41s /usr/libexec/gnome-session-binary --session gnome-classic
root     pts/0    :0               06Aug19  8days  0.05s  0.05s bash
root     pts/1    172.30.1.1       06Aug19  1:56m  2.52s  2.52s -bash
root     pts/3    172.30.1.1       10:15    4.00s  0.71s  0.01s w
yinzheng pts/4    172.30.1.1       12:46   47:24   0.05s  0.05s -bash
a.log
f1.log
f1.txt
f2.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls | tee -a a.log         #使用tee命令的"-a"选项可以以追加的方式写入文件

 

六.小试牛刀

  (1)将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中 

  (2)将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中 

  (3)一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:      Hello, I am 用户名,The system version is here,please help me to  check it ,thanks! 操作系统版本信息  

  (4)将/root/下文件列表,显示成一行,并文件名之间用空格隔开  

  (5)计算1+2+3+...+99+100的总和 

  (6)删除Windows文本文件中的回车字符 ,即“\r” 

  (7)处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格

  (8)将PATH变量每个目录显示在独立的一行 

  (9)将指定文件中0-9分别替代成a-j

  (10)将文件/etc/centos-release中每个单词(由字母组成)显示在独立一行,并无空行 

 

posted @ 2019-08-13 09:58  尹正杰  阅读(733)  评论(0编辑  收藏  举报