How to maintain sort order with xargs and gunzip
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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?
find sort xargs gzip gunzip
add a comment |Â
up vote
3
down vote
favorite
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?
find sort xargs gzip gunzip
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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?
find sort xargs gzip gunzip
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?
find sort xargs gzip gunzip
edited Aug 21 at 3:53
slm♦
237k65485659
237k65485659
asked Aug 21 at 3:48
Jesse Yishai
184
184
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
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)
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
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 at 8:29
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
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)
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
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 at 8:29
add a comment |Â
up vote
7
down vote
accepted
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)
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
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 at 8:29
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
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)
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)
edited Aug 21 at 6:35


Stéphane Chazelas
282k53521854
282k53521854
answered Aug 21 at 3:59
muru
33.5k577144
33.5k577144
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
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 at 8:29
add a comment |Â
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
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 at 8:29
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
This worked, thank you!
– Jesse Yishai
Aug 21 at 4:07
2
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 at 8:29
@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 at 8:29
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463769%2fhow-to-maintain-sort-order-with-xargs-and-gunzip%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password