Linux I/O重定向

  • 标准输入(stdin):文件描述符0
  • 标准输入(stdout):文件描述符1
  • 标准错误(stderr):文件描述符2

file descriptors(FD,文件描述符 或 Process I/O channels);

进程使用文件描述符来管理打开的文件

[root@centos7- ~]# ls /proc/$$/fd
      

0, 1, and 2, known as standard input, standard output, and standard error

输出重定向(覆盖、追加)

  • 正确输出:1>  1>>  等价于 > >>
  • 错误输出:2>  2>>

输出重定向(覆盖)

[root@centos7- ~]# date > date.txt

输出重定向(追加)

[root@centos7- ~]# date >> date.txt

错误输出重定向

[root@centos7- ~]# ls /home/ /aaaa >list.txt
ls: 无法访问/aaaa: 没有那个文件或目录
[root@centos7- ~]# ls /home/ /aaaa >list.txt >err.txt      //重定向到不同的位置

正确和错误都输出到相同位置 &>

[root@centos7- ~]# ls /home/ /aaaa &>list.txt          //混合输出

正确和错误都输出到相同位置 2>&1

[root@centos7- ~]# ls /home/ /aaaa >list.txt >&        //重定向到相同的位置

重定向到空设备/dev/null

[root@centos7- ~]# ls /home/ /aaaa >list.txt >/dev/null     //空设备,即将产生的输出丢掉

[root@centos7- ~]# ls /home/ /aaaa &>/dev/null     //空设备,即将产生的输出丢掉

/dev/null 补充

/dev/null:是一个空设备,黑洞,任何文件都可以扔进去,但是看不见

如果/dev/null设备被删除怎么办? rm -f /dev/null

1、手动创建

# mknod -m  /dev/null c   
[root@centos7- ~]# ll /dev/null /dev/zero
crw-rw-rw- root root , 11月 : /dev/null
crw-rw-rw- root root , 11月 : /dev/zero
主设备号 从设备号
MAJOR MINOR

主设备号相同:表示为同一种设备类型,也可以认为keme使用的是相同的驱动

从设备号:在同一类型中的一个序号

[root@centos7- ~]# ll /dev/null /dev/vda1 /etc/hosts
crw-rw-rw- root root , 11月 : /dev/null
brw-rw---- root disk , 11月 : /dev/vda1
-rw-r--r--. root root 10月 : /etc/hosts
c表示字符设备

普通文件和设备文件的区别:

从表面上看,普通文件有大小;块设备文件没有大小,有主设备号和从设备号。

字符设备和快设备的区别:

字符设备没有缓存,块设备有缓存

脚本中使用重定向

案例1:脚本中使用重定向
# vim ping.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq ];then
echo "up.."
else
echo "down.."
fi
# bash ping.sh 案例2:脚本中使用重定向
# vim ping2.sh
#!/usr/bin/bash
ping -c1 172.16.120.254 &>/dev/null
if [ $? -eq ];then
echo "172.16.120.254 up.." > /up.txt
else
echo "172.16.120.254 down.." >/down.txt
fi
# bash ping2.sh

示例

输入重定向

标准输入: <     等价于   0<

案例1

[root@centos7- ~]# mail -s "ssss" alice               //没有改变输入的方向,默认键盘

^D
[root@centos7- ~]# su - alice
[alice@centos7- ~]$ mail
Mail version 8.1 //. Type ? for help.
"/var/spool/mail/alice": message new
>N root@tianyun.local Mon Oct : / "ssss"
& [root@centos7- ~]# mail -s "test01" alice < /etc/hosts //输入重定向,来自于文件

案例2

[root@centos7- ~]# grep 'root'                         //没有改变输入的方向,默认键盘,此时等待输入...
yang sss
sssrootssss..
sssrootssss.. [root@centos7- ~]# grep 'root' < /etc/passwd
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin

案例3

[root@centos7- ~]# dd if=/dev/zero of=/file1.txt bs=1M count=
[root@centos7- ~]# dd </dev/zero >/file2.txt bs=1M count=

案例4 mysql表结构导入

[root@centos7- ~]# mysql -uroot -p123 < bbs.sql

重定向综合案例

综合案例1:利用重定向建立多行的文件

[root@centos7- ~]# echo "" > file1.txt
[root@centos7- ~]# cat file1.txt [root@centos7- ~]# cat >file2.txt ^D
[root@centos7- ~]# cat file2.txt 请问:file2.txt有几行? [root@centos7- ~]# cat >>file3.txt
aaa
bbb
ccc
ddd
^D
[root@centos7- ~]# cat file3.txt 请问:file3.txt有几行? [root@centos7- ~]# cat >file4 <<EOF
>
>
>
> EOF
[root@centos7- ~]# cat file4

综合案例2: 脚本中利用重定向打印消息

[root@centos7- ~]# vim yang.sh
#!/usr/bin/bash
cat <<-EOF
+------------------------------------------------+
| |
| ====================== |
| 虚拟机基本管理centos |
| |
| ====================== |
| . 安装虚拟机 |
| . 重置所有Linux虚拟机 |
| . 重置Windows虚拟机 |
| . 重置Windows虚拟机 [完全] |
| . 重置指定的虚拟机 |
| q. 退出管理程序 |
| |
+------------------------------------------------+
EOF

综合案例3

[root@centos7- ~]# ls; date &>/dev/null          //希望两条命令输出都重定向

[root@centos7- ~]# ls &>/dev/null; date &>/dev/null

[root@centos7- ~]# (ls; date) &>/dev/null

[root@centos7- ~]# (while :; do date; sleep ; done) &    //在后台运行,但输出依然在终端显示

[root@centos7- ~]# (while :; do date; sleep ; done) &>date.txt &
[]
[root@centos7- ~]# tailf date.txt
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST
Tue Apr :: CST [root@centos7- ~]# jobs
[]+ Running ( while :; do
date; sleep ;
done ) &>date.txt &
[root@centos7- ~]# kill %
[root@centos7- ~]# jobs

进程管道Piping

进程管道

用法:command1 | command2 | command3 |....

[root@centos7- ~]# ll /dev/ |less
[root@centos7- ~]# ps aux |grep 'sshd'
[root@centos7- ~]# rpm -qa |grep 'httpd' //查询所有安装的软件包,过滤包含httpd的包
[root@centos7- ~]# yum list |grep 'httpd'
  • 案例1:将/etc/password中的用户按UID大小排序

    [root@centos7- ~]# sort -t":" -k3 -n /etc/passwd   //以: 分隔,将第三列按字数升序
    [root@centos7- ~]# sort -t":" -k3 -n /etc/passwd -r //逆序
    [root@centos7- ~]# sort -t":" -k3 -n /etc/passwd |head -t 指定字段分隔符--field-separator
    -k 指定列
    -n 按数值
  • 案例2:统计出最占CPU的5个进程
    [root@centos7- ~]# ps aux --sort=-%cpu |head -
  • 案例3:统计当前/etc/password中用户使用的shell类型
    [root@centos7- ~]# awk -F: '{print $7}' /etc/passwd
    [root@centos7- ~]# awk -F: '{print $7}' /etc/passwd |sort
    [root@centos7- ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq
    [root@centos7- ~]# awk -F: '{print $7}' /etc/passwd |sort |uniq -c
    /bin/bash
    /bin/sync
    /sbin/halt
    /sbin/nologin
    /sbin/shutdown
  • 案例4:统计网站的访问情况 top5
    [root@centos7- ~]# ss -an |grep : |awk -F":" '{print $8}' |sort |uniq -c
    192.168.0.66
    192.168.10.11
    192.168.10.125
    192.168.10.183
    192.168.10.213
    192.168.10.35
    192.168.10.39
    [root@centos7- ~]# ss -an |grep : |awk -F":" '{print $8}' |sort |uniq -c |sort -k1 -rn |head -n
  • 案例5:打印当前所有IP
    [root@centos7- ~]# ip addr |grep 'inet ' |awk '{print $2}' |awk -F"/" '{print $1}'
    192.168.122.205
  • 案例6:打印根分区已用空间的百分比(仅打印数字)
    [root@centos7- ~]# df -P  |grep '/$' |awk '{print $5}' |awk -F"%" '{print $1}'

tee管道

[root@centos7- ~]#  ip addr |grep 'inet ' |tee ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7- ~]# cat ip.txt
inet 127.0.0.1/ scope host lo
inet 192.168.122.205/ brd 192.168.122.255 scope global eth0
[root@centos7- ~]# ip addr |grep 'inet ' |tee -a ip.txt |awk -F"/" '{print $1}' |awk '{print $2}'
127.0.0.1
192.168.122.205
[root@centos7- ~]# date |tee date.txt
2018年 11月 07日 星期三 :: CST
[root@centos7- ~]# cat date.txt
2018年 11月 07日 星期三 :: CST

参数传递Xargs

awk sed grep sort uniq less more xargs
xargs: ls cp rm

  • 案例1

    [root@localhost ~]# touch /home/file{..}
    
    [root@localhost ~]# vim files.txt
    /home/file1
    /home/file2
    /home/file3
    /home/file4
    /home/file5 [root@localhost ~]# cat files.txt |ls -l
    [root@localhost ~]# cat files.txt |rm -rvf cont.
    [root@localhost ~]# cat files.txt |xargs ls -l
    -rw-r--r--. root root Mar : /home/file1
    -rw-r--r--. root root Mar : /home/file2
    -rw-r--r--. root root Mar : /home/file4
    -rw-r--r--. root root Mar : /home/file5 [root@localhost ~]# cat files.txt |xargs rm -rvf
    removed ‘/home/file1’
    removed ‘/home/file2’
    removed ‘/home/file4’
    removed ‘/home/file5’
  • 案例2
    [root@localhost ~]# touch /home/file{..}
    [root@localhost ~]# cat files.txt |xargs -I {} ls -l {}
    -rw-r--r--. root root Mar : /home/file1
    -rw-r--r--. root root Mar : /home/file2
    -rw-r--r--. root root Mar : /home/file4
    -rw-r--r--. root root Mar : /home/file5 [root@localhost ~]# cat files.txt |xargs -I {} cp -rvf {} /tmp
    ‘/home/file1’ -> ‘/tmp/file1’
    ‘/home/file2’ -> ‘/tmp/file2’
    ‘/home/file4’ -> ‘/tmp/file4’
    ‘/home/file5’ -> ‘/tmp/file5’ [root@localhost ~]# cat files.txt |xargs -I YANG cp -rvf YANG /var/tmp
    ‘/home/file1’ -> ‘/var/tmp/file1’
    ‘/home/file2’ -> ‘/var/tmp/file2’
    ‘/home/file4’ -> ‘/var/tmp/file4’
    ‘/home/file5’ -> ‘/var/tmp/file5’
  • 案例3
    [root@localhost ~]# find /etc -iname "*ifcfg*" |xargs -I {} cp -rf {} /tmp

Linux-I/O重定向和管道的更多相关文章

  1. linux输入输出、重定向、管道

    本篇讲述linux系统的输入输出.管道和重定向. 1. liunx的输入输出 一个linux系统要想发挥作用,就要有输入输出,这样才可以与外界交互. 类型 设备文件名 文件描述符 设备名称 说明 备注 ...

  2. LINUX常用命令--重定向、管道篇(四)

    一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...

  3. linux——(8)数据流重定向、管道命令

    概念一:数据流重定向 数据流分输入流和输出流,还有一个标准错误流,负责管理出错信息,比如一般的命令的输出会输出到屏幕上,我们可以用重定向让他输入到某个文件内. 相关操作: 1,标准输入(stdin): ...

  4. linux I/O重定向及管道

    一,I/O重定向 重定向正常输出 > :覆盖以存在文件的内容,很危险的操作 >>:如果文件已经存在,则是在原内容的最后追加. 可以禁止>的覆盖行为,使用set -C.只在当前会 ...

  5. Linux Shell 重定向与管道【转帖】

    by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...

  6. Linux 入门记录:十六、Linux 多命令协作:管道及重定向

    一.多命令协作 在 Linux 系统当中,大多数命令都很简单,很少出现复杂功能的命令,每个命令往往只实现一个或多个很简单的功能.通过将不同功能的命令组合一起使用,可以实现某个复杂功能的. Linux ...

  7. 【Linux】【专项突破】Linux重定向与管道

    [专项突破]Linux重定向与管道 This article is written by Xrilang(Chinese Name:萌狼蓝天) If you want find me ,You can ...

  8. Linux I/O 重定向详解及应用实例

    Linux I/O 重定向详解及应用实例 简解 > 输出 < 输入 >> 追加 & [> | < | >>]之前:输入输出; ls /dev & ...

  9. linux 标准输入输出 重定向

    背景: 屏幕打印不一定都是从标准输出来的,也包括标准错误输出流stderr中的信息 文件描述符定义(系统定义了12个) 0 标准输入 1 标准输出 2 标准错误   0 默认键盘输入 1,2默认从屏幕 ...

随机推荐

  1. 初学Shiro

    Shiro Shiro是什么? Apache Shiro是Java的一个安全(权限)框架. Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境下,也可以用者JavaEE环境下 ...

  2. 用mapreduce 处理气象数据集

    用mapreduce 处理气象数据集 编写程序求每日最高最低气温,区间最高最低气温 气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa 按学号后三位下载不 ...

  3. Vue开发插件

    (一)Vue.js的插件应该有一个公开方法:install. 这个方法的第一个参数是Vue构造器,第二个参数是一个可选的选项对象,一般是如下操作: MyPlugin.install = functio ...

  4. UE3中Object和Actor的创建与销毁

    创建Object ① 在uc脚本中使用new运算符来创建 /********************************************************************** ...

  5. C# 获取当前服务器域名

    "http://"是协议名 "www.test.com"是域名 "aaa"是站点名 "bbb.aspx"是页面名(文件名 ...

  6. C#格式化

    格式化表示的一般格式 { N [ , M ] [ :格式码 ] } N:  指定参数序列中的输出序号,比如{0} , {1}, {2}等. M: 指定参数输出的最小长度. 如果参数长度小于M,则空格填 ...

  7. Windows Server 2008 R2 Enterprise x64 部署 nginx、tomcat、mysql

    部署nginx nginx主要做反向代理用,可以单独部署到其它机器上,这里nginx和tomcat部署在同一台机器上. 下载nginx-1.14.1.zip,并解压到目标目录,打开cmd进入到解压后的 ...

  8. 我的第一个python web开发框架(37)——职位管理功能

    对于职位管理,我们可以理解它为角色权限的管理,就像前面所说的一样,有了职位管理,后台管理系统绑定好对应的权限以后,新进员工.离职或岗位调整,管理员操作起来就非常的便捷了,只需要重新绑定对应职位就可以做 ...

  9. 四。Hibernate 使用MAVEN工具

    maven工具的使用1.作用:打包项目以及jar包的版本管理2.使用步骤: a.下载maven工具,修改conf目录下的setting.xml文件 <mirror> <id>a ...

  10. Android布局理解

    参考菜鸟教程,原文请查看:https://www.runoob.com/w3cnote/android-tutorial-linearlayout.html 1.FrameLayout(帧布局) 帧布 ...