在gitbook上看到一本不错的书,适合在碎片时间比如地铁上阅读,而且是英文版,格调比较高。
Command Line Cookbook
书中首先讲的是重定向相关的内容,书中讲了一个例子,比如执行两条命令,在第一条命令执行失败时才执行第二条命令,可以用如下方法:

ls file &> /dev/null || echo "File not exist"

假如当前目录下不存在file这个文件,才会打印 File not exist,而&>file是一种特殊的用法,也可以写成>&file,二者的意思完全相同,都等价于

>file 2>&1

>dirlist 2>&1 与 2>&1 >file的区别

Note that the order of redirections is significant. For example, the command ls > dirlist 2>&1 directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command ls 2>&1 > dirlist directs only the standard output to file dirlist,because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.
翻译:命令ls > dirlist 2>&1 把标准输出标准错误重定向到文件dirlist,命令ls 2>&1 > dirlist 只是把标准输出重定向到文件,原因是因为是标准错误重定向到标准输出是在标准输出重定向到文件之后。
执行结果:

[root@centos-linux-7 ~]# ls file >/dev/null 2>&1 || echo "File not exist"
File not exist
[root@centos-linux-7 ~]# ls file 2>&1 >/dev/null  || echo "File not exist"
ls: 无法访问file: 没有那个文件或目录
File not exist
[root@centos-linux-7 ~]# ls file &>/dev/null  || echo "File not exist"
File not exist
[root@centos-linux-7 ~]#

更详细的解释可以参考我之前的一篇文章Linux重定向