目录mysql
将本来要输出到屏幕上的数据信息,从新定向到指定的文件中linux
运行程序,或者输入一个命令:默认打开4个文件描述符nginx
名称 | 文件描述符 | 做用 |
---|---|---|
标准输入(stdin) | 0 | 一般键盘,也能够是其余文件或者命令的输出的内容能够做为标准输入 |
标准输出(stdout) | 1 | 默认输出到屏幕 |
错误输出(stderr) | 2 | 默认输出到屏幕 |
文件名称(filename) | 3+ |
/dev/stderr(错误输出) -> /proc/self/fd/2 2 -> /dev/pts/0 /dev/stdin(标准输入) -> /proc/self/fd/0 0 -> /dev/pts/0 /dev/stdout(标准输出) -> /proc/self/fd/1 1 -> /dev/pts/0
1>:标准输出重定向,将命令执行的正确结果输出到指定的文件或者设备中sql
2>:错误输出重定向shell
1>>:标准输出追加剧定向,将命令执行的正确结果追加输出到文件末尾app
2>>:错输出追加剧定向,将命令执行的错误结果追加输出到文件末尾网站
<< :标准输入重定向,将键盘敲的内容,输入到命令或者文件中ui
#将正确的内容追加到文件中,错误会输出到屏幕(不会覆盖源文件) echo "This is network conf" >> abc #将错误的内容输出到文件中,正确的会输出到屏幕(会覆盖源文件) find /etc -name "*.conf" 2>b find /etc -name "*.conf" 1>a 2>b #合并输出,错误的正确的内容都会输出到一个文件(会覆盖源文件) find /etc -name "*.conf" >c 2>&1 find /etc -name "*.conf" >c 2>c find /etc -name "*.conf" &>c #将错误输出重定向到 ‘黑洞’,正确内容输出到屏幕/dev/pts/x ls /root/ /err 2>/dev/null #将错误输出重定向到 ‘黑洞’,正确内容输出到1.txt文件中 ls /root/ /err >1.txt 2>/dev/null
< :0<code
<<:0<<排序
案例1: cat >> zls.txt <<eof zls qiudao eof 案例2: [root@zls ~]# mail zls < /etc/passwd 案例3: [root@oldboyedu ~]# grep 'root' rppr qwe rootasdasdadzxczxc rootasdasdadzxczxc ^C 案例4: [root@oldboyedu ~]# dd if=/dev/zero of=/file1.txt bs=1M count=20 20+0 records in 20+0 records out 20971520 bytes (21 MB) copied, 0.0260574 s, 805 MB/s [root@oldboyedu ~]# dd </dev/zero >/file2.txt bs=1M count=20 20+0 records in 20+0 records out 20971520 bytes (21 MB) copied, 0.011896 s, 1.8 GB/s 案例5: 恢复mysql数据 [root@zls ~]# mysql -uroot -p123 < bbs.sql 案例6:利用重定向创建多行文件 [root@oldboyedu ~]# cat >file1 案例7: #!/bin/sh menu(){ cat <<EOF +------------+ | 1 | apple | +---+--------+ | 2 | pear | +---+--------+ | 3 | banana | +---+--------+ | 4 | cherry | +---+--------+ EOF read -p "please input a num: " fruit } usage(){ echo "USAGE:请输入水果编号" exit 1 } color(){ case "$fruit" in 1) echo -e "\E[1;31mapple \E[0m" ;; 2) echo -e "\E[1;20mpear \E[0m" ;; 3) echo -e "\E[1;33mbanana \E[0m" ;; 4) echo -e "\E[1;35mcherry \E[0m" ;; *) usage esac } menu color 案例8:多条命令重定向 [root@oldboyedu ~]# (ls;date) > a.txt 案例9:后台进程重定向 (while :; do date; sleep 2; done) &>/dev/null &
链接多个命令,将管道符左侧的标准输出,交给管道符右侧的命令标准输入
案例1: 将/etc/passwd 中的UID取出并按大小排序 [root@oldboyedu ~]# awk -F : '{print $3}' /etc/passwd|sort -n 案例2: 统计当前/etc/passwd 中用户使用的 shell 类型 [root@oldboyedu ~]# awk -F : '{print $NF}' /etc/passwd|sort |uniq|wc -l 5 案例4:统计网站访问量top20 [root@driver-zeng nginx]# awk '{print $1}' driverzeng.com_access.log|sort |uniq -c|sort -nr|head -20 案例5:取出cpu已使用的百分比,只显示数字 [root@oldboyedu ~]# df -h |awk -F '[ %]+' 'NR==2 {print $5}' 4
tee:至关于管道符
[root@zls ~]# date > date.txt [root@zls ~]# date |tee date.txt
将参数列表转换成小块分段传递给其余命令
读入stdin
的数据转换为参数添加至命令后面
让一些不支持管道的命令能够使用管道。
注意:
1.在管道后面的命令,都不该该在写文件名
2.在管道中只有标准输出才能够传递下一个命令, 标准错误输出会直接输出终端显示, 建议在使用管道前将标准错误输出重定向。
例如: find /etc -name "*.conf" 2>/dev/null | grep rc
3.有些命令不支持管道技术, 可是能够经过xargs
来实现管道传递。 例如: which cat|xargs ls-l 例如: ls |xargs rm -rvf 例如: ls |xargs cp -rvft /tmp/ -> ls | xargs -I {} cp -rvf {} /tmp 例如: ls |xargs mv -t /tmp/ -> ls | xargs -I {} mv {} /tmp