Linux

Linux标准/错误输出重定向

2019-05-20  本文已影响5人  Y大宽

用法

命令 操作符号 文件名

操作符号包括:
>:标准输出的重定向到文件,实际是“>”前省略了数字“1”
2>错误输出用
&>所有输出重定向到文件
可以用来分析文件的输出结果

标准输出

$ df -h >file
(base) pc@pc-System-Product-Name:/project/raw_fq$ cat file1
Filesystem      Size  Used Avail Use% Mounted on
udev             32G     0   32G   0% /dev
tmpfs           6.3G  2.4M  6.3G   1% /run
/dev/nvme0n1p2  228G   11G  207G   5% /
tmpfs            32G     0   32G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs            32G     0   32G   0% /sys/fs/cgroup
/dev/loop0       13M   13M     0 100% /snap/gnome-characters/139
/dev/loop2      2.3M  2.3M     0 100% /snap/gnome-calculator/260
/dev/loop1       36M   36M     0 100% /snap/gtk-common-themes/1198
/dev/loop4      152M  152M     0 100% /snap/gnome-3-28-1804/36
/dev/loop3      141M  141M     0 100% /snap/gnome-3-26-1604/82
/dev/loop5      141M  141M     0 100% /snap/gnome-3-26-1604/74
/dev/loop6      3.8M  3.8M     0 100% /snap/gnome-system-monitor/57
/dev/loop7      4.2M  4.2M     0 100% /snap/gnome-calculator/406
/dev/loop8      1.0M  1.0M     0 100% /snap/gnome-logs/61
/dev/loop9       90M   90M     0 100% /snap/core/6673
/dev/loop10      15M   15M     0 100% /snap/gnome-logs/45
/dev/nvme0n1p1  511M  6.1M  505M   2% /boot/efi
/dev/loop11      35M   35M     0 100% /snap/gtk-common-themes/818
/dev/loop12      91M   91M     0 100% /snap/core/6350
/dev/loop13      15M   15M     0 100% /snap/gnome-characters/254
/dev/loop14      54M   54M     0 100% /snap/core18/941
/dev/loop15     3.8M  3.8M     0 100% /snap/gnome-system-monitor/77
/dev/sda1       7.3T  5.3T  1.6T  78% /data
/dev/sdb1       7.3T   93M  6.9T   1% /public
/dev/sdd1       7.3T  572M  6.9T   1% /home
/dev/sdc1       7.3T  173G  6.7T   3% /project
tmpfs           6.3G   16K  6.3G   1% /run/user/121
tmpfs           6.3G   36K  6.3G   1% /run/user/1000
/dev/loop16      90M   90M     0 100% /snap/core/6818
/dev/loop17      15M   15M     0 100% /snap/gnome-characters/258
/dev/loop18     3.8M  3.8M     0 100% /snap/gnome-system-monitor/81
/dev/loop19     152M  152M     0 100% /snap/gnome-3-28-1804/40
$ df -h > file1
$ diff file1 file
28c28
< /dev/sdc1       7.3T  173G  6.7T   3% /project
---
> /dev/sdc1       7.3T  163G  6.7T   3% /project

标准输出和错误输出分别放两个文件

$ ls /error test >right_file 2>wrong_file
$ cat right_file
test:
hangshu.tex
result.txt
text.txt
$ cat wrong_file
ls: cannot access '/error': No such file or directory

错误和正确的放一个文件

$ ls /error test >all.log 2>&1
$ cat all.log
ls: cannot access '/error': No such file or directory
test:
hangshu.tex
result.txt
text.txt

或者简单写为

ls /error test &>all2.log
$ cat all2.log
ls: cannot access '/error': No such file or directory
test:
hangshu.tex
result.txt
text.txt
上一篇下一篇

猜你喜欢

热点阅读