Linux重定向&管道 文件句柄FD

一、重定向
1.FD简介
FD file descriptors ,文件描述符,又称文件句柄
进程使用文件描述符来管理打开的文件。
FD是从0-255, 0代表stdin标准输入、1代表stdout标准输出、2代表stderr标准错误;3-255代表用户编辑的文件的绝对路径。
图示解析
在这里插入图片描述在这里插入图片描述总结
FD是访问文件的标识,即链接文件,它代表着文件的绝对路径,使程序在使用文件时直接调用FD,从而省去了冗余的绝对路径。
示例
1)通过一个终端,打开一个文本。
[root@localhost ~]# vim 1.txt
2)通过另一个终端,查询文本程序的进程号
[root@localhost ~]# ps aux| grep vim
root 3906 1.0 0.2 149748 5484 pts/0 S+ 21:02 0:00 vim 1.txt
3)在/proc目录中查看文本程序的FD
[root@localhost ~]# ls /proc/3906/fd/
0 1 2 3
[root@localhost ~]# ls -l /proc/3906/fd/
总用量 0
lrwx------. 1 root root 64 12月 4 21:04 0 -> /dev/pts/0
lrwx------. 1 root root 64 12月 4 21:04 1 -> /dev/pts/0
lrwx------. 1 root root 64 12月 4 21:03 2 -> /dev/pts/0
lrwx------. 1 root root 64 12月 4 21:04 3 -> /root/.1.txt.swp
4)总结
0 -> /dev/pts/0 标椎输入
1 -> /dev/pts/0 标准输出
2 -> /dev/pts/0 标准错误
3 -> /root/.1.txt.swp 常规文件
l 链接文件
程序通过描述符访问文件,可以是常规文件,也可以是设备文件。
2.重定向案例 > <
输出重定向分为:
==(1)标准输出重定向 1>等价于 > ==
==(2)错误输出重定向 2> ==
输入重定向
1)案例1:输出重定向
[root@localhost ~]# date 1> date.txt
追加重定向 >>
[root@localhost ~]# date >> date.txt
[root@localhost ~]# cat date.txt
2020年 05月 29日 星期五 11:30:14 CST
2)案例2:错误输出重定向
错误示范
[root@localhost ~]# ls /home/ 2> list.txt
list.txt文件中没有内容,因为没有错误信息
正确示范
当某条命令产生错误时,才会有错误输出:
[root@localhost ~]# ls /aaaaaaaaa 2> list.txt
[root@localhost ~]#cat list.txt
ls: 无法访问/aaaaaaaaa: 没有那个文件或目录
3)案例3: 正确和错误输出都写入到相同位置
[root@localhost ~]# ls /home/ /aaaaaaaaa &>list.txt
正确和错误输出都写入入到空设备,不写入丢掉
/dev/null黑洞文件,Linux系统的回收站和垃圾箱
[root@localhost ~]# ls /home/ /aaaaaaaaa &> /dev/null
4)案例4:正确和错误输出写入到不同位置
正确的输出写入到test.txt,错误的输出到屏幕上
[root@localhost ~]# ls /home/ /aaaaaaaaa 1> test.txt
正确的输出写入到test.txt,错误的输出写入到erro.txt
[root@localhost ~]# ls /home/ /aaaaaaaaa 1> test.txt /aaaaaaaaa 2>erro.txt
3.输入重定向及结合案例
标准输入: < 等价 0<
案例:输入重定向发送邮件
1)观察默认发送邮件的过程
编写邮件
[root@localhost ~]# mail -s “hello” jack
hello
123
12345
56789
.
EOT

解析
mail 电子邮件
-s 发送主题
“hello” 主题内容
jack 邮件接收人
.点代表邮件编辑已结束
查看邮件
[root@localhost ~]# su - jack
[jack@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/jack”: 1 message 1 new

N 1 root Sat Dec 5 10:05 21/579 “hello”
& 1
Message 1:
From root@localhost.localdomain Sat Dec 5 10:05:37 2020
Return-Path: root@localhost.localdomain
X-Original-To: jack
Delivered-To: jack@localhost.localdomain
Date: Sat, 05 Dec 2020 10:05:37 +0800
To: jack@localhost.localdomain
Subject: hello
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R

hello
123
12345
56789

& q
Held 1 message in /var/spool/mail/jack
总结
mail查看邮件
&
按邮件编号,即可看邮件。
按q 退出。
2)使用重定向快速创建邮件
先准备一段邮件内容
vim word.txt
内容如下:
hello world
The world is wonderful.
hope you will like it.
重定向
[root@localhost ~]# mail -s “test01” alice < word.txt
[root@localhost ~]# su - alice
上一次登录:六 12月 5 10:16:35 CST 2020pts/0 上
[alice@localhost ~]$ mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
“/var/spool/mail/alice”: 1 messages 1 new

N 1 root Sat Dec 5 10:21 20/657 “test01”
& 1
Message 1:
From root@localhost.localdomain Sat Dec 5 10:21:10 2020
Return-Path: root@localhost.localdomain
X-Original-To: alice
Delivered-To: alice@localhost.localdomain
Date: Sat, 05 Dec 2020 10:21:09 +0800
To: alice@localhost.localdomain
Subject: test01
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@localhost.localdomain (root)
Status: R

hello world
The world is wonderful.
hope you will like it.

& q
Held 1 messages in /var/spool/mail/alice
原理
利用输入重定向,把文件内容代替人为的输入。
输入重定向来自于文件。
二、管道 |
1.进程管道 Piping
1)简介
管道命令可以将多条命令组合起来,一次性完成复杂的处理任务。
2)语法
command1 | command2 |command3 |…
在这里插入图片描述
指令1的标准输出作为指令2的标准输入
3)案例
[root@localhost ~]# cat /etc/passwd |tail -5
u1:x :1003:1003::/home/u1:/bin/bash
apache:x :48:48:Apache:/usr/share/httpd:/sbin/nologin
www:x :1004:1004::/home/www:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
uu2:x :1006:1006::/home/uu2:/bin/bash
[root@localhost ~]# cat /etc/passwd |tail -5 | grep u1
u1:x :1003:1003::/home/u1:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash

[root@localhost ~]# ps aux | grep sshd
root 1242 0.0 0.2 112920 4316 ? Ss 09:12 0:00 /usr/sbin/sshd -D
root 4457 0.0 0.0 112732 972 pts/0 R+ 10:43 0:00 grep --color=auto sshd
2.tee管道
1)简介
三通管道,即交给另一个程序处理。又保存一份副本。
tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件。
tee 捕获信息 接到管道后面截取信息并保存成副本。
在这里插入图片描述2)案例
[root@localhost ~]# cat /etc/passwd | tail -5 | tee f1.txt
u1:x :1003:1003::/home/u1:/bin/bash
apache:x :48:48:Apache:/usr/share/httpd:/sbin/nologin
www:x :1004:1004::/home/www:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
uu2:x :1006:1006::/home/uu2:/bin/bash
同时备份在文件f1.txt中
[root@localhost ~]# cat f1.txt
u1:x :1003:1003::/home/u1:/bin/bash
apache:x :48:48:Apache:/usr/share/httpd:/sbin/nologin
www:x :1004:1004::/home/www:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
uu2:x :1006:1006::/home/uu2:/bin/bash

[root@localhost ~]# cat /etc/passwd | tail -5 | tee file.txt | grep u1
u1:x :1003:1003::/home/u1:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
同时备份在文件file.txt中
[root@localhost ~]# cat file.txt
u1:x :1003:1003::/home/u1:/bin/bash
apache:x :48:48:Apache:/usr/share/httpd:/sbin/nologin
www:x :1004:1004::/home/www:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
uu2:x :1006:1006::/home/uu2:/bin/bash
[root@localhost ~]# cat /etc/passwd | tail -5 | grep u1 | tee f.txt
u1:x : 1003:1003::/home/u1:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
同时备份在文件f.txt中
[root@localhost ~]# cat f.txt
u1:x :1003:1003::/home/u1:/bin/bash
uu1:x :1005:1005::/home/uu1:/bin/bash
3.参数传递 Xargs
1)简介
这里只涉及有些命令是不接受标准输入的,如rm、cp一些特殊命令,无法识别字符类型。
2)案例
环境准备,准备一些文件。
[root@localhost ~]# touch /home/file{1…5}
[root@localhost ~]# ls /home/
file1 file2 file3 file4 file5
接到消息,部分文件需要删除。
[root@localhost ~]# vim files.txt
/home/file1
/home/file2
/home/file5
使用管道
[root@localhost ~]# cat files.txt |rm -rvf
[root@localhost ~]# ls /home/
file1 file2 file3 file4 file5
失败
通过|xargs成功连接rm命令:
[root@localhost ~]# cat files.txt |xargs rm -rvf
removed ‘/home/file1’
removed ‘/home/file2’
removed ‘/home/file5’
(参数 -v 过程显示)
[root@localhost ~]# ls /home/
file3 file4
删除成功

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值