Jes*_*hai 3 find xargs gzip sort gunzip
我试图按字母顺序(在这种情况下也意味着日期和迭代)顺序提取某些文件的内容,并且当我首先使用ls
以下命令测试该过程时:
$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 \
| sort | xargs -r0 ls -l | awk -F' ' '{print $6 " " $7 " " $9}'
Run Code Online (Sandbox Code Playgroud)
我得到了一个积极的结果:
Aug 18 /opt/minecraft/wonders/logs/2018-08-17-3.log.gz
Aug 18 /opt/minecraft/wonders/logs/2018-08-18-1.log.gz
Aug 19 /opt/minecraft/wonders/logs/2018-08-18-2.log.gz
Aug 19 /opt/minecraft/wonders/logs/2018-08-19-1.log.gz
Aug 20 /opt/minecraft/wonders/logs/2018-08-19-2.log.gz
Aug 20 /opt/minecraft/wonders/logs/2018-08-20-1.log.gz
Run Code Online (Sandbox Code Playgroud)
但是,当我实际提取文件时,排序顺序丢失了:
$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 \
| sort | xargs -r0 gunzip -vc | grep "\/opt.*"`
/opt/minecraft/wonders/logs/2018-08-18-1.log.gz: 66.8%
/opt/minecraft/wonders/logs/2018-08-18-2.log.gz: 83.1%
/opt/minecraft/wonders/logs/2018-08-19-1.log.gz: 70.3%
/opt/minecraft/wonders/logs/2018-08-19-2.log.gz: 72.9%
/opt/minecraft/wonders/logs/2018-08-20-1.log.gz: 73.3%
/opt/minecraft/wonders/logs/2018-08-17-3.log.gz: 90.2%
Run Code Online (Sandbox Code Playgroud)
解压缩这些文件时如何保持排序顺序?
您已使用-print0
withfind
和-0
with选项xargs
,但您忘记使用-z
for sort
,因此sort
基本上只看到一行(除非您的文件名包含\n
)。您看到的输出ls
可能ls
正在做一些排序。
find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
sort -z | xargs -r0 gunzip -vc | grep /opt
Run Code Online (Sandbox Code Playgroud)
(注意:20*
是一个 glob,需要为 shell 引用,所以它按字面传递给find
,你不想转义/
for grep
,它的作用是未指定的,.*
如果你想要的只是打印,则不需要在正则表达式的末尾匹配行)