Rhcsa的管道命令和重定向的使用

管道

管道操作符——“|”
        管道是由符号“|”隔开的若干条命令组成的序列。
管道符“|”的作用:
        将前一个命令的输出通过一个无形的“管道”作为下一个命令的输入,即实现将前一个命令的输出的数据结果作为后一条命令所需要的数据源参数
进程管道
        用法:command1 | command2 |command3 |...

管道符的使用场合:
        当输出内容较多时,为便于浏览,可将输出内容,通过管道操作符,传递给more命令来分页查看,也可传递给grep命令实现对指定对象的查看。
注意:
        1、管道命令只处理前一个命令正确输出,不处理错误输出。
        2、管道命令右边命令,必须能够接收标准输入流命令才行。

[root@localhost ~]# rpm -qa |grep 'httpd' //查询所有安装的软件
包,过滤包含httpd的包
案例1:将/etc/passwd中的用户按UID大小排序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd //以: 分隔,将第三
列按字数升序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
[root@localhost ~]# sort -t":" -k3 -n /etc/passwd |head
-t 指定字段分隔符--field-separator
-k 指定列
-n 按数值

案例2:统计出最占CPU的5个进程
[root@localhost ~]# ps aux --sort=-%cpu |head -6
案例3:统计当前/etc/passwd中用户使用的shell类型
思路:取出第七列(shell) | 排序(把相同归类)| 去重
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
[root@localhost ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
案例4: 统计网站的访问情况 top 20
思路: 打印所有访问的连接 | 过滤访问网站的连接 | 打印用户的IP | 排序 | 去重
[root@localhost ~]# ss -an |grep :80 |awk -F":" '{print $8}' |sort |uniq -c
案例5: 打印当前所有IP
[root@localhost ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print
$1}'
案例6:打印根分区已用空间的百分比(仅打印数字)
[root@localhost ~]# df -P |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

L练习:将echo "This is my first time to use pipe"内容输出到屏幕上,且保存到pipe_data.txt中

[root@ccw ~]# echo "This is my first time to use pipe" | tee -a pipe_data.txt
This is my first time to use pipe

 上面我们运用管道操作符 “ | ”+ tee -a pipe_data.txt,将我们需要输出在屏幕上的内容,保存到pipe_data.txt文件中。
大家或许会对“ tee “,感到疑惑,接下来我给大家讲解一下 ” tee “的用法。

tee的用法
Linux tee命令用于读取标准输入的数据,并将其内容输出成文件。
tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。

 语法使用:

tee [-ai][--help][--version][文件...]

参数:
        -a或--append 附加到既有文件的后面,而非覆盖它.
        -i或--ignore-interrupts 忽略中断信号。
        --help 在线帮助。
        --version 显示版本信息
接下来我给大家说一下如何在linux上使用

[command] | tee [file]

如何确保 tee 命令追加信息到文件中?

[command] | tee -a [file]

接下来大家可能会想如何让 tee 写入多个文件?
这非常之简单。你仅仅只需要写明文件名即可

[command] | tee [file1] [file2] [file3]

 我们由此输入可以看到我们将echo "This is my first time to use pipe"内容输入到屏幕,而且还创建了pipe_data.txt,pipe_date2.txt,pipe3_data.txt,三个文件。

[root@ccw ~]# echo "This is my first time to use pipe" | tee  pipe_data.txt pipe_data2.txt pipe3_data.txt
This is my first time to use pipe
[root@ccw ~]# ls
1                                             log3            test1.txt
12345                                         ls_test         test2
anaconda-ks.cfg                               Music           test3
betxin.txt                                    Pictures        test4
cont-1743875-13176484-155100_adpkg-ad_hd.mp4  pipe3_data.txt  test5
Desktop                                       pipe_data2.txt  test8
Documents                                     pipe_data.txt   Videos
Downloads                                     Public          wget-log3
index.html                                    redirect_file   wget-log_backup
index.html.1                                  tee_file        wget-log_hlink
index.html.2                                  Templates       wget-log_link
index.html.3                                  test            wget-log_slink
initial-setup-ks.cfg                          test1
[root@ccw ~]# ^C

 小结:

匿名管道由pipe函数创建并打开。
命名管道由mkfifo函数创建,打开用open
FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一量这些工作在Linux系统下,命名管道可由两种方式创建(假设创建一个名为“fifoexample”的有名道):
(1)mkfifo("fifoexample","rw");
(2)mknod fifoexample p
        mkfifo是一个函数,mknod是一个系统调用完成之后,它们具有相同的语义。

 输出/输入重定向

1.重定向操作符——“>”、“>>”、“<”、“<<”
在Linux系统中
默认的输入设备(标准输入)是键盘
默认的输出设备(标准输出)是屏幕
利用重定向操作符可以重新定义命令涉及的默认的输入和输出设备对象,即重定向操作符可以将命令输入
和输出数据流从默认设备重定向到其他位置。
重定向操作符本身不是一条命令,而是命令中附加的可改变命令的输入和输出对象的特殊符号,
“>”、“>>”称为输出重定向操作符,
“<”、“<<”称为输入重定向操作符。

 标准输入、标准输出、标准错误

输出重定向(覆盖)
[root@localhost ~]# date 1> date.txt

输出重定向(追加)
[root@localhost ~]# date >> date.txt

 错误输出重定向
[root@localhost ~]# ls /home/ /aaaaaaaaa >list.txt
ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
[root@localhost ~]# ls /home/ /aaaaaaaaa >list.txt 2>error.txt //重定向到不同的位置

正确和错误都输入到相同位置
[root@localhost ~]# ls /home/ /aaaaaaaaa &>list.txt //混合输出

正确和错误都输入到相同位置
[root@localhost ~]# ls /home/ /aaaaaaaaa >list.txt 2>&1 //重定向到相同的位置

重定向到空设备/dev/null
[root@localhost ~]# ls /home/ /aaaaaaaaa >list.txt 2>/dev/null //空设备,即将产生的输出丢掉
[root@localhost ~]# ls /home/ /aaaaaaaaa &>/dev/null //空设备,即将产生的输出丢掉

接下来我们来做一些练习:

a.新建一个文件redirect.txt,并在其中写入20210804RHCSA,保存并退出

[root@ccw ~]# touch redirect.txt
[root@ccw ~]# ls
1                                             index.html.1          Public
12345                                         index.html.2          redirect.txt
anaconda-ks.cfg                               index.html.3          Templates
betxin.txt                                    initial-setup-ks.cfg  test
cont-1743875-13176484-155100_adpkg-ad_hd.mp4  log3                  test1
Desktop                                       ls_test               test1.txt
Documents                                     Music                 Videos
Downloads                                     Pictures
index.html                                    pipe_data.txt
[root@ccw ~]# vim redirect.txt
[root@ccw ~]# more redirect.txt
20210804RHCSA
[root@ccw ~]# 

  


b.将cat redirect.txt 的输出结果重定向到 redirect1.txt中

[root@ccw ~]# cat redirect.txt > redirect1.txt
[root@ccw ~]# more redirect1.txt
20210804RHCSA
[root@ccw ~]# 


c.将cat redirect.txt noexist.txt的标准输出重定向到redirect2.txt, 标准错误输出重定向到error1.txt

[root@ccw ~]# cat redirect.txt > redirect2.txt
[root@ccw ~]# cat noexist.txt 2> error1.txt
[root@ccw ~]# more redirect2.txt
20210804RHCSA
[root@ccw ~]# more error1.txt
cat: noexist.txt: No such file or directory
[root@ccw ~]# 

d.将cat redirect.txt noexist.txt的标准输出和错误输出都从定向到redirect3.txt中

[root@ccw ~]# cat redirect.txt noexist.txt &> redirect3.txt
[root@ccw ~]# more redirect3.txt
20210804RHCSA
cat: noexist.txt: No such file or directory
[root@ccw ~]# 


e.输入重定向:将从键盘输入Welcome to my zone.换行YYDS然后输入EOF结束键盘输入,且重定向到my_input.txt中

[root@ccw ~]# cat <<EOF > my_input.txt
> Welcome to my zone.
> YYDS
> EOF
[root@ccw ~]# more my_input.txt
Welcome to my zone.
YYDS
[root@ccw ~]# 

我们用 cat <<EOF > my_input.txt,进行内容的输入,并且重新定向到my_input.txt文件中,这里我们的结束语是EOF,如果我们输入eof,它并不会退出

[root@ccw ~]# cat <<EOF >my_input1.txt
> Welcome to my zone
> CTT CCW
> eof
> EOF
[root@ccw ~]# vim my_input1.txt
[root@ccw ~]# more my_input1.txt
Welcome to my zone
CCW LOVE CTT
eof
[root@ccw ~]# 

 而且你在使用more命令查找是小写的eof也是会被输出的,直到你输入EOF它才会停止。
我们的练习就做到这里,接下来给大家讲解一下,文件重定位的组合和使用文件描述符重定位

使用文件描述符重定向

使用文件描述符的重定向都使用了&符号
cmd >&n 把输出送到文件描述符n
cmd m>&n 把输出到文件符m的信息重定向到文件描述符n
cmd >&- 关闭标准输出
cmd <&n 输入来自文件描述符n
cmd m<&n m来自文件描述符n
cmd <&- 关闭标准输入
cmd <&n- 移动输入文件描述符n而非复制它。
cmd >&n- 移动输出文件描述符 n而非复制它。
2>&1:将stderr重定向到标准输出

重定向组合使用

cmd 2>file <>cmd &> file <>cmd >& file
cmd > file 2>&1
cmd > f1 2>f2
tee files

cmd >/dev/null 2>&1 <==> &> /dev/null

 特殊应用:

cat > catfile <<EOF
 

 查看文本文件的内容——cat命令

命令格式:cat [选项] 文件名列表
说明:文件名可使用通配符
常用选项:
-n——对输出内容中的所有行标注行号。
-b——对输出内容中的非空行标注行号。

[root@ccw ~]# cat -n /etc/passwd
     1	root:x:0:0:root:/root:/bin/bash
     2	bin:x:1:1:bin:/bin:/sbin/nologin
     3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4	adm:x:3:4:adm:/var/adm:/sbin/nologin
     5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6	sync:x:5:0:sync:/sbin:/bin/sync
     7	shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
     8	halt:x:7:0:halt:/sbin:/sbin/halt
     9	mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    10	operator:x:11:0:operator:/root:/sbin/nologin
    11	games:x:12:100:games:/usr/games:/sbin/nologin
    12	ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    13	nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
    14	dbus:x:81:81:System message bus:/:/sbin/nologin
    15	systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
    16	systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin

 分页查看文件内容——more和less命令

命令格式: more | less [选项] 文件名
常用选项:
-数字——仅适用于more命令,用来指定分页显示时每页的行数。
+num——指定从文件的第num行开始显示。
-c——从顶部清屏然后显示文件内容。
-N——仅适用于less命令,其作用是在每行前添加输出行号。
交互操作方法:
按Enter键向下逐行滚动
按空格键向下翻一屏、按b键向上翻一屏
文件末尾时more会自动退出,less 按q键退出

[root@ccw ~]# more -10 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@ccw ~]# less -10 /etc/passwd

查看文件开头或末尾的部分内容——head和tail命令

 命令:head | tail [选项] 文件名
常用选项有:
-num——指定需要显示文件多少行的内容,若不指定,默认只显示十行。
-f——使tail不停地去读取和显示文件最新的内容, 以监视文件内容的变化。这样有实时监视的效果。
tail命令更多的用于查看系统日志文件,以便于观察重要的系统消息,特别是结合使用-f选项,tail会自动实时地把打开文件中的新消息显示到屏幕上,从而跟踪日志文件末尾的内容变化,直至按【Ctrl+C】键终止显示和跟踪。

[root@ccw ~]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@ccw ~]# tail -5 /etc/passwd
testuser4:x:1002:1002:testuser4:/home/testuser4:/bin/bash
testuser5:x:1003:1003:testuser5:/home/testuser5:/bin/bash
testuser6:x:1004:1004::/home/testuser6:/bin/bash
usertest8:x:1133:1122::/home/usertest8:/bin/bash
usertest10:x:1134:1122::/home/usertest10:/bin/bash

 检索、过滤文件内容——grep命令

功能:在指定的文件中查找并显示含有指定字符串的行。
格式:grep [选项] 要查找的字符串或条件表达式 被查找的文件名
选项:
-i——查找时忽略大小写
-v——反转查找,输出与查找条件不相符的行
在grep命令中,可以直接指定关键字串作为查找条件,也可以使用复杂的条件表达式,例如:字符“^”表示行的开始;字符“$”表示行的结尾;如果查找的字符串中带有空格,可以用单引号或双引号括起来。
又如:
"^read"表示以read开始;"read$"表示以read结束;"^$"表示空行。

[root@ccw ~]# grep -i root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

本次分享就到这里谢谢大家的阅读😊😊!
 


 


 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SereinWen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值