I'm attempting to extract the contents of some files by alphabetical (which in this case also means date and iteration) order and when I test the process first with ls
:
$ find /opt/minecraft/wonders/logs/ -name 20* -type f -mtime -3 -print0 \
| sort | xargs -r0 ls -l | awk -F' ' '{print $6 " " $7 " " $9}'
I get a positive result:
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
However, when I go to actually extract the files the sort order is lost:
$ 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%
How can I maintain the sort order while unzipping these files?
Best Answer
You have used the
-print0
option withfind
, and-0
withxargs
, but you forgot to use-z
forsort
, sosort
essentially sees a single line (unless your filenames contain\n
). The output you see withls
is probablyls
doing some sorting.(Note:
20*
is a glob and needs to be quoted for the shell so it's passed literally tofind
, you don't want to escape/
forgrep
, what that does is unspecified, no need for.*
at the end of the regexp if all you want is print the matching line)