有时候为了清晰会将多个指令的结果合并在一行中显示,比如查看文件及属性的命令 ls
和 file
,
单独使用时
ls --full-time -h
explain shell
-rwxr-xr-x 1 lufei www 7.5M 2019-07-09 14:23:48.041432487 +0800 1.mp4
-rw-r--r-- 1 lufei www 3.1M 2019-07-09 14:24:17.872506355 +0800 2.mp4
-rw-r-xr-- 1 lufei www 905K 2019-07-09 14:23:17.672339243 +0800 3.mp4
file *
explain shell
1.mp4: ISO Media, MPEG v4 system, version 2
2.mp4: ISO Media, MPEG v4 system, version 1
3.mp4: ISO Media, MPEG v4 system, version 1
如何将两个指令合并一行显示那?
这时候 join
指令就排上用场了。用法参考 man join
或 join --help
或 join command
下面继续使用上面的例子来说明:
join -1 1 -2 5 <(file * | sed 's/[:,]//g') <(ls --full-time -h | awk '{print $5" "$6" "$7" "$8" "$9}') | column -t
explain shell
1.mp4 ISO Media MPEG v4 system version 2 7.5M 2019-07-09 14:23:48.041432487 +0800
2.mp4 ISO Media MPEG v4 system version 1 3.1M 2019-07-09 14:24:17.872506355 +0800
3.mp4 ISO Media MPEG v4 system version 1 905K 2019-07-09 14:23:17.672339243 +0800
shell 命令解释可以使用在线工具 explainshell
file * | sed 's/[:,]//g'
explain shell 将file
输出中的 :
和 ,
去除。
ls --full-time -h | awk '{print $5" "$6" "$7" "$8" "$9}'
explain shell 仅保留ls
输出中的 5~9
列。
column -t
explain shell 保证列对齐。
join -1 1 -2 5 <(stream 1) <(stream2)
根据 stream 1
第 1
列作为 key
(file name) 和 stream 2
第 5
列作为 key
(file name) 来进行合并(文件名相同的合并为一列)。
Refer to Concatenate in bash the output of two commands without newline character
Combine two command output, refer to How to create a clean zip archive of a PHP git project without development files and folders?
{ git ls-tree -r master --name-only; find /vendor /node_modules -type f; }