一、重定向命令:输出重定向和输入重定向。
1、正确输出重定向,覆盖到原来的后面:1>等价于>
2、正确输出重定向,追加到原来的后面:1>>等价于>>
3、错误输出重定向,覆盖到原来的后面:2>
4、错误输出重定向,追加到到原来的后面:2>>
[root@localhost ~]# cd /home/jiaofan
[root@localhost jiaofan]# ls /home /ddd
ls: 无法访问/ddd: 没有那个文件或目录
/home:
jiao1 jiao2 jiaofan user100
[root@localhost jiaofan]# ls /home /ddd >> yes.txt 2>> no.txt
[root@localhost jiaofan]# cat yes.txt
/home:
jiao1
jiao2
jiaofan
user100
[root@localhost jiaofan]# cat no.txt
ls: 无法访问/ddd: 没有那个文件或目录
[root@localhost jiaofan]#
4、正确错误所有的都重定向到同一个文件中:&>
[root@localhost jiaofan]# ls /home /aaaa &> yesno.txt
[root@localhost jiaofan]# cat yesno.txt
ls: 无法访问/aaaa: 没有那个文件或目录
/home:
jiao1
jiao2
jiaofan
user100
[root@localhost jiaofan]#
5、输入重定向:0<等价于<
5.1 发送邮件实验;:mail -s “标题” 用户名
说明:mail(发送邮件),-s(标题),.(结束)
[root@localhost jiaofan]# mail -s "aa" jiaofan
aaa
ddd
.
EOT
[root@localhost jiaofan]#
[jiaofan@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/jiaofan": 1 message 1 new
>N 1 root Tue Mar 23 11:36 19/613 "aa"
& 1
Message 1:
From root@localhost.localdomain Tue Mar 23 11:36:17 2021
Return-Path: <root@localhost.localdomain>
X-Original-To: jiaofan
Delivered-To: jiaofan@localhost.localdomain
Date: Tue, 23 Mar 2021 11:36:16 +0800
To: jiaofan@localhost.localdomain
Subject: aa
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
aaa
ddd
&
5.2 利用重定向发送邮件:
[root@localhost jiaofan]# cat yes.txt
/home:
jiao1
jiao2
jiaofan
user100
[root@localhost jiaofan]# mail -s "a" jiaofan < yes.txt
[jiaofan@localhost ~]$ su - jiaofan
密码:
上一次登录:二 3月 23 11:38:53 CST 2021pts/0 上
[jiaofan@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/jiaofan": 2 messages 2 new
>N 1 root Tue Mar 23 11:36 19/613 "aa"
N 2 root Tue Mar 23 11:38 22/639 "a"
& 2
Message 2:
From root@localhost.localdomain Tue Mar 23 11:38:43 2021
Return-Path: <root@localhost.localdomain>
X-Original-To: jiaofan
Delivered-To: jiaofan@localhost.localdomain
Date: Tue, 23 Mar 2021 11:38:43 +0800
To: jiaofan@localhost.localdomain
Subject: a
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R
/home:
jiao1
jiao2
jiaofan
user100
& q
Held 2 messages in /var/spool/mail/jiaofan
您在 /var/spool/mail/jiaofan 中有邮件
[jiaofan@localhost ~]$
二、管道命令:进程管道Piping、tee管道、参数传递Xargs.
1、管道命令:|
2、tee管道
[root@localhost network-scripts]# cat /etc/passwd | head -4 | tee /home/jiaofan/1.txt | head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost network-scripts]# cat /home/jiaofan/1.txt
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
[root@localhost network-scripts]#
cat /etc/passwd | head -4 查看文件passwd的前四行
tee /home/jiaofan/1.txt 把cat /etc/passwd | head -4的结果放入文件1.txt.
head -2在屏幕中显示两行
3、管道后面接cp、rm需要用Xargs:删除/home/jiaofan/下的file1、file2、fil3
[root@localhost jiaofan]# touch file{1..5}
[root@localhost jiaofan]# ls
111.txt 2.txt file2 file4 yesno.txt 视频 音乐
11.txt dir1 file2.txt file5 yes.txt 图片 桌面
1.txt file1 file3 ifcfg-ens33 公共 文档
22.txt file1.txt file3.txt no.txt 模板 下载
[root@localhost jiaofan]# cat 2.txt
/home/jiaofan/file1
/home/jiaofan/file2
/home/jiaofan/file3
[root@localhost jiaofan]# cat 2.txt | xargs rm -rvf
已删除"/home/jiaofan/file1"
已删除"/home/jiaofan/file2"
已删除"/home/jiaofan/file3"
[root@localhost jiaofan]#