Find extracted directory name from tar fileManipulate file name piped from find command'Tar' the result of a 'find', preserving the directory structureGet directory name from file nameUsing tar with findExtracting files to current directoryTar file with date as name?How to tar each directory as seperate file and keep the tar in another directory?tar extract after creating directory name based on tar fileFind filename inside tar archive in different directorySorting files into folders based on date in filename?
Why doesn't H₄O²⁺ exist?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Today is the Center
Do infinite dimensional systems make sense?
"You are your self first supporter", a more proper way to say it
Revoked SSL certificate
Malformed Address '10.10.21.08/24', must be X.X.X.X/NN or
Watching something be written to a file live with tail
Has there ever been an airliner design involving reducing generator load by installing solar panels?
What doth I be?
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
NMaximize is not converging to a solution
Perform and show arithmetic with LuaLaTeX
RSA: Danger of using p to create q
Roll the carpet
Does detail obscure or enhance action?
High voltage LED indicator 40-1000 VDC without additional power supply
What does the "remote control" for a QF-4 look like?
What's the output of a record needle playing an out-of-speed record
What would happen to a modern skyscraper if it rains micro blackholes?
Is it possible to run Internet Explorer on OS X El Capitan?
dbcc cleantable batch size explanation
Important Resources for Dark Age Civilizations?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Find extracted directory name from tar file
Manipulate file name piped from find command'Tar' the result of a 'find', preserving the directory structureGet directory name from file nameUsing tar with findExtracting files to current directoryTar file with date as name?How to tar each directory as seperate file and keep the tar in another directory?tar extract after creating directory name based on tar fileFind filename inside tar archive in different directorySorting files into folders based on date in filename?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are:
- there will only be one top level directory
- the folder name doesn't necessarily match the tar.xz/gz filename (in other words something-1.3.5a.tar.gz might extract to a folder called something-1.3.5)
In order to lessen the amount of typing I have to do I wrote a small script, which among other things does the follow:
- extracts the tar.xz/gz file
- cds into the directory
My current hack is to do something like this:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
Basically what this does is it captures the output of the extraction, and takes the first line (which is the top level directory) and then cds into it.
So, my question is this, is there a cleaner/better way of doing this?
bash shell-script
add a comment |
I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are:
- there will only be one top level directory
- the folder name doesn't necessarily match the tar.xz/gz filename (in other words something-1.3.5a.tar.gz might extract to a folder called something-1.3.5)
In order to lessen the amount of typing I have to do I wrote a small script, which among other things does the follow:
- extracts the tar.xz/gz file
- cds into the directory
My current hack is to do something like this:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
Basically what this does is it captures the output of the extraction, and takes the first line (which is the top level directory) and then cds into it.
So, my question is this, is there a cleaner/better way of doing this?
bash shell-script
You could use thet
option intar
to list the contents, e.g.:cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.
– marinus
Sep 14 '15 at 1:53
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
If the option -a works for you, you are not usingtar
but rathergtar
. Since there is also no standard tar option-z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...
– schily
Sep 14 '15 at 8:54
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04
add a comment |
I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are:
- there will only be one top level directory
- the folder name doesn't necessarily match the tar.xz/gz filename (in other words something-1.3.5a.tar.gz might extract to a folder called something-1.3.5)
In order to lessen the amount of typing I have to do I wrote a small script, which among other things does the follow:
- extracts the tar.xz/gz file
- cds into the directory
My current hack is to do something like this:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
Basically what this does is it captures the output of the extraction, and takes the first line (which is the top level directory) and then cds into it.
So, my question is this, is there a cleaner/better way of doing this?
bash shell-script
I have a project where I am downloading and compiling a bunch of files. The assumptions I can make about this files are:
- there will only be one top level directory
- the folder name doesn't necessarily match the tar.xz/gz filename (in other words something-1.3.5a.tar.gz might extract to a folder called something-1.3.5)
In order to lessen the amount of typing I have to do I wrote a small script, which among other things does the follow:
- extracts the tar.xz/gz file
- cds into the directory
My current hack is to do something like this:
test1=$(tar -axvf something-1.3.5a.tar.gz)
cd $(echo $test1 | cut -f1 -d" ")
Basically what this does is it captures the output of the extraction, and takes the first line (which is the top level directory) and then cds into it.
So, my question is this, is there a cleaner/better way of doing this?
bash shell-script
bash shell-script
asked Sep 14 '15 at 1:15
bash_noob-3.14.tar.gzbash_noob-3.14.tar.gz
1612
1612
You could use thet
option intar
to list the contents, e.g.:cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.
– marinus
Sep 14 '15 at 1:53
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
If the option -a works for you, you are not usingtar
but rathergtar
. Since there is also no standard tar option-z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...
– schily
Sep 14 '15 at 8:54
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04
add a comment |
You could use thet
option intar
to list the contents, e.g.:cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.
– marinus
Sep 14 '15 at 1:53
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
If the option -a works for you, you are not usingtar
but rathergtar
. Since there is also no standard tar option-z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...
– schily
Sep 14 '15 at 8:54
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04
You could use the
t
option in tar
to list the contents, e.g.: cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.– marinus
Sep 14 '15 at 1:53
You could use the
t
option in tar
to list the contents, e.g.: cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.– marinus
Sep 14 '15 at 1:53
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
If the option -a works for you, you are not using
tar
but rather gtar
. Since there is also no standard tar option -z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...– schily
Sep 14 '15 at 8:54
If the option -a works for you, you are not using
tar
but rather gtar
. Since there is also no standard tar option -z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...– schily
Sep 14 '15 at 8:54
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04
add a comment |
3 Answers
3
active
oldest
votes
#!/bin/sh
Dir_name=`tar -tzf something-1.3.5a.tar.gz | head -1 | cut -f1 -d"/"`
echo $Dir_name
tar options details,
-t, --list
-z, --gzip, --ungzip filter the archive through gzip
-f, --force-local archive file is local even if has a colon
Here, we are getting the list of file in tar and taking the first line using head -1
cmd, extracting the first field using cut cmd.
One thing to note is that if you're doing this withset pipefail
, it will error ashead
has terminated after the first line.
– IBam
Nov 1 '16 at 15:39
add a comment |
If you have one top directory and nothing, you can simply remember what directories there are before you run tar, and see what you have after you do that, assuming nobody else have changed that directory.
# ksh terminology: set -A BEFORE .*/ */
BEFORE=(.*/ */)
tar -xf blah blah blah
AFTER=(.*/ */)
# We can make it O(n), since glob outputs can be assumed to be sorted.
# Do it.
for (( i = 0; i < "$#BEFORE[@]"; i++ )); do
[ "$BEFORE[i]" == "$AFTER[i]" ] || break
done
cd "$AFTER[i]"
This should work with all shells that has arithmetic for
, arrays starting with a zero index and with $#arr[@]
to access array member count.
There is actually one problem, since */
may expand into literally */
where there is no match. In bash
(assuming you are using bash because of your tag), you can set shopt -s nullglob
for that.
Using */
restricts the globbed items to directories, so you won't worry about files -- Extra files still make it only one dir :)
add a comment |
With a tar.bz2 file, to not get something like:
0 0 2019-04-02 17:20 folder name
I'm using -tf options:
tar -tf file_name.tar.bz2 | head -1 | cut -f1 -d"/"
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2funix.stackexchange.com%2fquestions%2f229504%2ffind-extracted-directory-name-from-tar-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
#!/bin/sh
Dir_name=`tar -tzf something-1.3.5a.tar.gz | head -1 | cut -f1 -d"/"`
echo $Dir_name
tar options details,
-t, --list
-z, --gzip, --ungzip filter the archive through gzip
-f, --force-local archive file is local even if has a colon
Here, we are getting the list of file in tar and taking the first line using head -1
cmd, extracting the first field using cut cmd.
One thing to note is that if you're doing this withset pipefail
, it will error ashead
has terminated after the first line.
– IBam
Nov 1 '16 at 15:39
add a comment |
#!/bin/sh
Dir_name=`tar -tzf something-1.3.5a.tar.gz | head -1 | cut -f1 -d"/"`
echo $Dir_name
tar options details,
-t, --list
-z, --gzip, --ungzip filter the archive through gzip
-f, --force-local archive file is local even if has a colon
Here, we are getting the list of file in tar and taking the first line using head -1
cmd, extracting the first field using cut cmd.
One thing to note is that if you're doing this withset pipefail
, it will error ashead
has terminated after the first line.
– IBam
Nov 1 '16 at 15:39
add a comment |
#!/bin/sh
Dir_name=`tar -tzf something-1.3.5a.tar.gz | head -1 | cut -f1 -d"/"`
echo $Dir_name
tar options details,
-t, --list
-z, --gzip, --ungzip filter the archive through gzip
-f, --force-local archive file is local even if has a colon
Here, we are getting the list of file in tar and taking the first line using head -1
cmd, extracting the first field using cut cmd.
#!/bin/sh
Dir_name=`tar -tzf something-1.3.5a.tar.gz | head -1 | cut -f1 -d"/"`
echo $Dir_name
tar options details,
-t, --list
-z, --gzip, --ungzip filter the archive through gzip
-f, --force-local archive file is local even if has a colon
Here, we are getting the list of file in tar and taking the first line using head -1
cmd, extracting the first field using cut cmd.
edited Dec 2 '15 at 9:21
Archemar
20.5k93973
20.5k93973
answered Dec 2 '15 at 9:10
NagaNaga
5112
5112
One thing to note is that if you're doing this withset pipefail
, it will error ashead
has terminated after the first line.
– IBam
Nov 1 '16 at 15:39
add a comment |
One thing to note is that if you're doing this withset pipefail
, it will error ashead
has terminated after the first line.
– IBam
Nov 1 '16 at 15:39
One thing to note is that if you're doing this with
set pipefail
, it will error as head
has terminated after the first line.– IBam
Nov 1 '16 at 15:39
One thing to note is that if you're doing this with
set pipefail
, it will error as head
has terminated after the first line.– IBam
Nov 1 '16 at 15:39
add a comment |
If you have one top directory and nothing, you can simply remember what directories there are before you run tar, and see what you have after you do that, assuming nobody else have changed that directory.
# ksh terminology: set -A BEFORE .*/ */
BEFORE=(.*/ */)
tar -xf blah blah blah
AFTER=(.*/ */)
# We can make it O(n), since glob outputs can be assumed to be sorted.
# Do it.
for (( i = 0; i < "$#BEFORE[@]"; i++ )); do
[ "$BEFORE[i]" == "$AFTER[i]" ] || break
done
cd "$AFTER[i]"
This should work with all shells that has arithmetic for
, arrays starting with a zero index and with $#arr[@]
to access array member count.
There is actually one problem, since */
may expand into literally */
where there is no match. In bash
(assuming you are using bash because of your tag), you can set shopt -s nullglob
for that.
Using */
restricts the globbed items to directories, so you won't worry about files -- Extra files still make it only one dir :)
add a comment |
If you have one top directory and nothing, you can simply remember what directories there are before you run tar, and see what you have after you do that, assuming nobody else have changed that directory.
# ksh terminology: set -A BEFORE .*/ */
BEFORE=(.*/ */)
tar -xf blah blah blah
AFTER=(.*/ */)
# We can make it O(n), since glob outputs can be assumed to be sorted.
# Do it.
for (( i = 0; i < "$#BEFORE[@]"; i++ )); do
[ "$BEFORE[i]" == "$AFTER[i]" ] || break
done
cd "$AFTER[i]"
This should work with all shells that has arithmetic for
, arrays starting with a zero index and with $#arr[@]
to access array member count.
There is actually one problem, since */
may expand into literally */
where there is no match. In bash
(assuming you are using bash because of your tag), you can set shopt -s nullglob
for that.
Using */
restricts the globbed items to directories, so you won't worry about files -- Extra files still make it only one dir :)
add a comment |
If you have one top directory and nothing, you can simply remember what directories there are before you run tar, and see what you have after you do that, assuming nobody else have changed that directory.
# ksh terminology: set -A BEFORE .*/ */
BEFORE=(.*/ */)
tar -xf blah blah blah
AFTER=(.*/ */)
# We can make it O(n), since glob outputs can be assumed to be sorted.
# Do it.
for (( i = 0; i < "$#BEFORE[@]"; i++ )); do
[ "$BEFORE[i]" == "$AFTER[i]" ] || break
done
cd "$AFTER[i]"
This should work with all shells that has arithmetic for
, arrays starting with a zero index and with $#arr[@]
to access array member count.
There is actually one problem, since */
may expand into literally */
where there is no match. In bash
(assuming you are using bash because of your tag), you can set shopt -s nullglob
for that.
Using */
restricts the globbed items to directories, so you won't worry about files -- Extra files still make it only one dir :)
If you have one top directory and nothing, you can simply remember what directories there are before you run tar, and see what you have after you do that, assuming nobody else have changed that directory.
# ksh terminology: set -A BEFORE .*/ */
BEFORE=(.*/ */)
tar -xf blah blah blah
AFTER=(.*/ */)
# We can make it O(n), since glob outputs can be assumed to be sorted.
# Do it.
for (( i = 0; i < "$#BEFORE[@]"; i++ )); do
[ "$BEFORE[i]" == "$AFTER[i]" ] || break
done
cd "$AFTER[i]"
This should work with all shells that has arithmetic for
, arrays starting with a zero index and with $#arr[@]
to access array member count.
There is actually one problem, since */
may expand into literally */
where there is no match. In bash
(assuming you are using bash because of your tag), you can set shopt -s nullglob
for that.
Using */
restricts the globbed items to directories, so you won't worry about files -- Extra files still make it only one dir :)
edited Sep 15 '15 at 5:11
answered Sep 15 '15 at 4:59
Arthur2e5Arthur2e5
922519
922519
add a comment |
add a comment |
With a tar.bz2 file, to not get something like:
0 0 2019-04-02 17:20 folder name
I'm using -tf options:
tar -tf file_name.tar.bz2 | head -1 | cut -f1 -d"/"
New contributor
add a comment |
With a tar.bz2 file, to not get something like:
0 0 2019-04-02 17:20 folder name
I'm using -tf options:
tar -tf file_name.tar.bz2 | head -1 | cut -f1 -d"/"
New contributor
add a comment |
With a tar.bz2 file, to not get something like:
0 0 2019-04-02 17:20 folder name
I'm using -tf options:
tar -tf file_name.tar.bz2 | head -1 | cut -f1 -d"/"
New contributor
With a tar.bz2 file, to not get something like:
0 0 2019-04-02 17:20 folder name
I'm using -tf options:
tar -tf file_name.tar.bz2 | head -1 | cut -f1 -d"/"
New contributor
New contributor
answered 2 days ago
fabaterafabatera
1
1
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f229504%2ffind-extracted-directory-name-from-tar-file%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
You could use the
t
option intar
to list the contents, e.g.:cd "$(tar tfz "$file" | head -n 1 | cut -f1)"
, but that's not really much better. There's no built-in way to do this (that I know of), as the assumption you make is not always true for every possible tarball.– marinus
Sep 14 '15 at 1:53
Thanks for your suggestion. Yeah, I had a much cleaner solution but I kept on finding exceptions and they weren't really regular enough to capture in sed and my sed command was just getting longer and longer. Since this solution works, I'm not too concerned if there isn't a better way but I wanted to check just in case there was something obvious that I was missing.
– bash_noob-3.14.tar.gz
Sep 14 '15 at 2:12
If the option -a works for you, you are not using
tar
but rathergtar
. Since there is also no standard tar option-z
, in theory the proposal from marinus could fail as well. If you don't like to be portable, you may stay with that...– schily
Sep 14 '15 at 8:54
Thanks for your input, schily. Since I can always expect to use gtar, I think I should be right. There is no point finding and using a more portable solution if I won't ever need it.
– bash_noob-3.14.tar.gz
Sep 15 '15 at 0:04