3

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?

7

You have used the -print0 option with find, and -0 with xargs, but you forgot to use -z for sort, so sort essentially sees a single line (unless your filenames contain \n). The output you see with ls is probably ls doing some sorting.

find /opt/minecraft/wonders/logs/ -name '20*' -type f -mtime -3 -print0 |
  sort -z | xargs -r0 gunzip -vc | grep /opt

(Note: 20* is a glob and needs to be quoted for the shell so it's passed literally to find, you don't want to escape / for grep, what that does is unspecified, no need for .* at the end of the regexp if all you want is print the matching line)

  • 2
    @JesseYishai If the answer helped you, don't forget to mark the answer as accepted by clicking on the check mark beside the answer ! :) – Ikaros Aug 21 '18 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.