Find

如何使用 xargs 和 gunzip 維護排序順序

  • August 21, 2018

我正在嘗試按字母順序(在這種情況下也意味著日期和迭代)順序以及當我首先使用以下方法測試過程時提取某些文件的內容ls

$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 \
  | sort | xargs -r0 ls -l | awk -F' ' '{print $6 " " $7 " " $9}'

我得到一個積極的結果:

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

但是,當我實際提取文件時,排序順序會失去:

$ 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%

解壓縮這些文件時如何保持排序順序?

您使用了with和with-print0選項,但您忘記使用for ,所以基本上只看到一行(除非您的文件名包含)。您看到的輸出可能正在進行一些排序。find``-0``xargs``-z``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

(注意:是一個glob ,需要20*為 shell 引用,因此它按字面意思傳遞給匹配線)find``/``grep``.*

引用自:https://unix.stackexchange.com/questions/463769