Extract .tar.gz files in different subdirectories
up vote
0
down vote
favorite
i have phenological data of different tree species, but they are saved by the bigger Treegroup.
The output of find . -name *.tar.gz
is:
./Tilia/PEP725_DE_129_070.tar.gz
./Tilia/PEP725_DE_129_071.tar.gz
./Fagus/PEP725_DE_108_010.tar.gz
./Acer/PEP725_DE_115_000.tar.gz
./Acer/PEP725_DE_115_030.tar.gz
./Betula/PEP725_DE_106_020.tar.gz
I want to extract every file in their subdirectory and the name of the output directory should be the same as the tar-file.
I manage to extract all with find . -name *.tar.gz -execdir tar -xvzf "{}" ;
But this does not create a directory name after the zipped files.
How do i do this? -C
needs the directory already to be existend...
So in the end i would like to have
Tilia/EP725_DE_129_070/content_of_PEP725_DE_129_070.tar.gz
and so on...
linux command-line bash unix tar
add a comment |
up vote
0
down vote
favorite
i have phenological data of different tree species, but they are saved by the bigger Treegroup.
The output of find . -name *.tar.gz
is:
./Tilia/PEP725_DE_129_070.tar.gz
./Tilia/PEP725_DE_129_071.tar.gz
./Fagus/PEP725_DE_108_010.tar.gz
./Acer/PEP725_DE_115_000.tar.gz
./Acer/PEP725_DE_115_030.tar.gz
./Betula/PEP725_DE_106_020.tar.gz
I want to extract every file in their subdirectory and the name of the output directory should be the same as the tar-file.
I manage to extract all with find . -name *.tar.gz -execdir tar -xvzf "{}" ;
But this does not create a directory name after the zipped files.
How do i do this? -C
needs the directory already to be existend...
So in the end i would like to have
Tilia/EP725_DE_129_070/content_of_PEP725_DE_129_070.tar.gz
and so on...
linux command-line bash unix tar
Note: quote properly,-name "*.tar.gz"
. Comparefind
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.
– Kamil Maciorowski
Nov 27 at 15:38
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i have phenological data of different tree species, but they are saved by the bigger Treegroup.
The output of find . -name *.tar.gz
is:
./Tilia/PEP725_DE_129_070.tar.gz
./Tilia/PEP725_DE_129_071.tar.gz
./Fagus/PEP725_DE_108_010.tar.gz
./Acer/PEP725_DE_115_000.tar.gz
./Acer/PEP725_DE_115_030.tar.gz
./Betula/PEP725_DE_106_020.tar.gz
I want to extract every file in their subdirectory and the name of the output directory should be the same as the tar-file.
I manage to extract all with find . -name *.tar.gz -execdir tar -xvzf "{}" ;
But this does not create a directory name after the zipped files.
How do i do this? -C
needs the directory already to be existend...
So in the end i would like to have
Tilia/EP725_DE_129_070/content_of_PEP725_DE_129_070.tar.gz
and so on...
linux command-line bash unix tar
i have phenological data of different tree species, but they are saved by the bigger Treegroup.
The output of find . -name *.tar.gz
is:
./Tilia/PEP725_DE_129_070.tar.gz
./Tilia/PEP725_DE_129_071.tar.gz
./Fagus/PEP725_DE_108_010.tar.gz
./Acer/PEP725_DE_115_000.tar.gz
./Acer/PEP725_DE_115_030.tar.gz
./Betula/PEP725_DE_106_020.tar.gz
I want to extract every file in their subdirectory and the name of the output directory should be the same as the tar-file.
I manage to extract all with find . -name *.tar.gz -execdir tar -xvzf "{}" ;
But this does not create a directory name after the zipped files.
How do i do this? -C
needs the directory already to be existend...
So in the end i would like to have
Tilia/EP725_DE_129_070/content_of_PEP725_DE_129_070.tar.gz
and so on...
linux command-line bash unix tar
linux command-line bash unix tar
edited Nov 27 at 15:37
asked Nov 27 at 15:28
m4D_guY
33
33
Note: quote properly,-name "*.tar.gz"
. Comparefind
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.
– Kamil Maciorowski
Nov 27 at 15:38
add a comment |
Note: quote properly,-name "*.tar.gz"
. Comparefind
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.
– Kamil Maciorowski
Nov 27 at 15:38
Note: quote properly,
-name "*.tar.gz"
. Compare find
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.– Kamil Maciorowski
Nov 27 at 15:38
Note: quote properly,
-name "*.tar.gz"
. Compare find
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.– Kamil Maciorowski
Nov 27 at 15:38
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
If your tar
supports --one-top-level
option:
find . -name "*.tar.gz" -execdir tar --one-top-level -xvzf {} ;
From man 1 tar
:
--one-top-level[=DIR]
Extract all files into
DIR
, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by--auto-compress
).
Note: {}
may or may not be quoted but *.tar.gz
should be quoted to avoid mishaps like this: find
utility does not output all files when using wildcards.
If your tar
doesn't support --one-top-level
option then -C
is quite a good idea, you just need to create a respective directory first. This command, however, goes a step further and doesn't even use -C
:
find . -type f -name "*.tar.gz" -execdir sh -c '
dirn="${1%.tar.gz}" # desired directory name
mkdir -- "$dirn" # creating a directory
cd -- "$dirn" &&
tar -xvzf ../"$1" # extracting to it
' find-sh {} ;
The only non-POSIX component here is… the tar
itself. tar
is only recognized as a legacy tool without gzip support. In POSIX (since 2001), the equivalent of tar
is the pax
program, also without gzip support. As far as I know there is no gzip
(nor equivalent) in POSIX, so it's impossible to create a solution fully compliant with the formal POSIX standard.
Fortunately gzip
is a de facto standard. In the worst case the above piece of code should run gzip
before tar
(or pax
).
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
add a comment |
up vote
0
down vote
Something like:
for f in $(find . -name *.tar.gz);
do
cd $(dirname $d) # to ./Tilia/
d=$(basename $f .tar.gz)
mkdir $d # ./Tilia/PEP725_DE_129_070
cd $d # To ./Tilia/PEP725_DE_129_070
tar -xvzf ../$d.tar.gz # extracting ./Tilia/PEP725_DE_129_070.tar.gz in ./Tilia/PEP725_DE_129_070
cd ../.. # back to top
done
Untested, use at your own risk.
Copy above to a file and source
the file.
If you feel adventurous, you can also make that a one-liner.
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If your tar
supports --one-top-level
option:
find . -name "*.tar.gz" -execdir tar --one-top-level -xvzf {} ;
From man 1 tar
:
--one-top-level[=DIR]
Extract all files into
DIR
, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by--auto-compress
).
Note: {}
may or may not be quoted but *.tar.gz
should be quoted to avoid mishaps like this: find
utility does not output all files when using wildcards.
If your tar
doesn't support --one-top-level
option then -C
is quite a good idea, you just need to create a respective directory first. This command, however, goes a step further and doesn't even use -C
:
find . -type f -name "*.tar.gz" -execdir sh -c '
dirn="${1%.tar.gz}" # desired directory name
mkdir -- "$dirn" # creating a directory
cd -- "$dirn" &&
tar -xvzf ../"$1" # extracting to it
' find-sh {} ;
The only non-POSIX component here is… the tar
itself. tar
is only recognized as a legacy tool without gzip support. In POSIX (since 2001), the equivalent of tar
is the pax
program, also without gzip support. As far as I know there is no gzip
(nor equivalent) in POSIX, so it's impossible to create a solution fully compliant with the formal POSIX standard.
Fortunately gzip
is a de facto standard. In the worst case the above piece of code should run gzip
before tar
(or pax
).
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
add a comment |
up vote
0
down vote
accepted
If your tar
supports --one-top-level
option:
find . -name "*.tar.gz" -execdir tar --one-top-level -xvzf {} ;
From man 1 tar
:
--one-top-level[=DIR]
Extract all files into
DIR
, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by--auto-compress
).
Note: {}
may or may not be quoted but *.tar.gz
should be quoted to avoid mishaps like this: find
utility does not output all files when using wildcards.
If your tar
doesn't support --one-top-level
option then -C
is quite a good idea, you just need to create a respective directory first. This command, however, goes a step further and doesn't even use -C
:
find . -type f -name "*.tar.gz" -execdir sh -c '
dirn="${1%.tar.gz}" # desired directory name
mkdir -- "$dirn" # creating a directory
cd -- "$dirn" &&
tar -xvzf ../"$1" # extracting to it
' find-sh {} ;
The only non-POSIX component here is… the tar
itself. tar
is only recognized as a legacy tool without gzip support. In POSIX (since 2001), the equivalent of tar
is the pax
program, also without gzip support. As far as I know there is no gzip
(nor equivalent) in POSIX, so it's impossible to create a solution fully compliant with the formal POSIX standard.
Fortunately gzip
is a de facto standard. In the worst case the above piece of code should run gzip
before tar
(or pax
).
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If your tar
supports --one-top-level
option:
find . -name "*.tar.gz" -execdir tar --one-top-level -xvzf {} ;
From man 1 tar
:
--one-top-level[=DIR]
Extract all files into
DIR
, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by--auto-compress
).
Note: {}
may or may not be quoted but *.tar.gz
should be quoted to avoid mishaps like this: find
utility does not output all files when using wildcards.
If your tar
doesn't support --one-top-level
option then -C
is quite a good idea, you just need to create a respective directory first. This command, however, goes a step further and doesn't even use -C
:
find . -type f -name "*.tar.gz" -execdir sh -c '
dirn="${1%.tar.gz}" # desired directory name
mkdir -- "$dirn" # creating a directory
cd -- "$dirn" &&
tar -xvzf ../"$1" # extracting to it
' find-sh {} ;
The only non-POSIX component here is… the tar
itself. tar
is only recognized as a legacy tool without gzip support. In POSIX (since 2001), the equivalent of tar
is the pax
program, also without gzip support. As far as I know there is no gzip
(nor equivalent) in POSIX, so it's impossible to create a solution fully compliant with the formal POSIX standard.
Fortunately gzip
is a de facto standard. In the worst case the above piece of code should run gzip
before tar
(or pax
).
If your tar
supports --one-top-level
option:
find . -name "*.tar.gz" -execdir tar --one-top-level -xvzf {} ;
From man 1 tar
:
--one-top-level[=DIR]
Extract all files into
DIR
, or, if used without argument, into a subdirectory named by the base name of the archive (minus standard compression suffixes recognizable by--auto-compress
).
Note: {}
may or may not be quoted but *.tar.gz
should be quoted to avoid mishaps like this: find
utility does not output all files when using wildcards.
If your tar
doesn't support --one-top-level
option then -C
is quite a good idea, you just need to create a respective directory first. This command, however, goes a step further and doesn't even use -C
:
find . -type f -name "*.tar.gz" -execdir sh -c '
dirn="${1%.tar.gz}" # desired directory name
mkdir -- "$dirn" # creating a directory
cd -- "$dirn" &&
tar -xvzf ../"$1" # extracting to it
' find-sh {} ;
The only non-POSIX component here is… the tar
itself. tar
is only recognized as a legacy tool without gzip support. In POSIX (since 2001), the equivalent of tar
is the pax
program, also without gzip support. As far as I know there is no gzip
(nor equivalent) in POSIX, so it's impossible to create a solution fully compliant with the formal POSIX standard.
Fortunately gzip
is a de facto standard. In the worst case the above piece of code should run gzip
before tar
(or pax
).
edited Nov 27 at 17:57
answered Nov 27 at 16:40
Kamil Maciorowski
23.1k155072
23.1k155072
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
add a comment |
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
Thanks, it worked. What does POSIX mean?
– m4D_guY
Nov 28 at 20:25
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
@m4D_guY Portable Operating System Interface. A solution that is fully POSIX-compliant will work on variety of systems. In general there are many tools (or options) only supported on "large" Linuxes. If you can use them, great! But if you work with an embedded "minimal" Unix, or on Mac (where tools often differ from these of Linux) etc. then a solution that doesn't go beyond POSIX may be a lifesaver.
– Kamil Maciorowski
Nov 28 at 20:41
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
To be honest i dont know how this works... i always only click the up button not the "check" button, where is the difference, a link is totally fine
– m4D_guY
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
@m4D_guY Please take our tour then. You need 15 reputation to vote up; there is no such restriction for accepting an answer to any of your questions. If you got a good answer to your other question (not necessarily on Super User, Stack Overflow maybe?), consider accepting it as well.
– Kamil Maciorowski
2 days ago
add a comment |
up vote
0
down vote
Something like:
for f in $(find . -name *.tar.gz);
do
cd $(dirname $d) # to ./Tilia/
d=$(basename $f .tar.gz)
mkdir $d # ./Tilia/PEP725_DE_129_070
cd $d # To ./Tilia/PEP725_DE_129_070
tar -xvzf ../$d.tar.gz # extracting ./Tilia/PEP725_DE_129_070.tar.gz in ./Tilia/PEP725_DE_129_070
cd ../.. # back to top
done
Untested, use at your own risk.
Copy above to a file and source
the file.
If you feel adventurous, you can also make that a one-liner.
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
add a comment |
up vote
0
down vote
Something like:
for f in $(find . -name *.tar.gz);
do
cd $(dirname $d) # to ./Tilia/
d=$(basename $f .tar.gz)
mkdir $d # ./Tilia/PEP725_DE_129_070
cd $d # To ./Tilia/PEP725_DE_129_070
tar -xvzf ../$d.tar.gz # extracting ./Tilia/PEP725_DE_129_070.tar.gz in ./Tilia/PEP725_DE_129_070
cd ../.. # back to top
done
Untested, use at your own risk.
Copy above to a file and source
the file.
If you feel adventurous, you can also make that a one-liner.
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
add a comment |
up vote
0
down vote
up vote
0
down vote
Something like:
for f in $(find . -name *.tar.gz);
do
cd $(dirname $d) # to ./Tilia/
d=$(basename $f .tar.gz)
mkdir $d # ./Tilia/PEP725_DE_129_070
cd $d # To ./Tilia/PEP725_DE_129_070
tar -xvzf ../$d.tar.gz # extracting ./Tilia/PEP725_DE_129_070.tar.gz in ./Tilia/PEP725_DE_129_070
cd ../.. # back to top
done
Untested, use at your own risk.
Copy above to a file and source
the file.
If you feel adventurous, you can also make that a one-liner.
Something like:
for f in $(find . -name *.tar.gz);
do
cd $(dirname $d) # to ./Tilia/
d=$(basename $f .tar.gz)
mkdir $d # ./Tilia/PEP725_DE_129_070
cd $d # To ./Tilia/PEP725_DE_129_070
tar -xvzf ../$d.tar.gz # extracting ./Tilia/PEP725_DE_129_070.tar.gz in ./Tilia/PEP725_DE_129_070
cd ../.. # back to top
done
Untested, use at your own risk.
Copy above to a file and source
the file.
If you feel adventurous, you can also make that a one-liner.
answered Nov 27 at 16:08
xenoid
3,5333718
3,5333718
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
add a comment |
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
Until wild cherry (or another name with space in it) pops up…
– Kamil Maciorowski
Nov 27 at 16:17
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1378797%2fextract-tar-gz-files-in-different-subdirectories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note: quote properly,
-name "*.tar.gz"
. Comparefind
utility does not output all files when using wildcards. You may get away few times but it will hit you eventually.– Kamil Maciorowski
Nov 27 at 15:38