How to repeatedly un tar files that are within the tar itselfMake directory copies using findHow to scp/tar files that are in between specific days?Way to write new filename on wildcard input?tar list files onlyBash if conditions of variables within intervalsHow to copy modified files while preserving folder structureCreating a tar archive without including the parent directory when files are stored in different directoriesFind and replace Pipe using awk command
Disordered Cryptic Orders
What does this text mean with capitalized letters?
What is the design rationale for having armor and magic penetration mechanics?
Has there been a Kraken patron for the Warlock class in Unearthed Arcana?
Applicable country for US Visa In Icelandair Airline Website
How can I prove mathematically the reflection matrix has only the eigenvalues 1 or -1?
When did 5 foot squares become standard in D&D?
Do you say "good game" after a game in which your opponent played poorly?
Anatomically Correct Baku
Why does b+=(4,) work and b = b + (4,) doesn't work when b is a list?
What does the British parliament hope to achieve by requesting a third Brexit extension?
Should I avoid "big words" when writing to a younger audience?
How to repeatedly un tar files that are within the tar itself
How should the 23rd judge vote?
Usefulness of Nash embedding theorem
I am often given, occasionally stolen, rarely sold, and never borrowed
How does Firefox know my ISP login page?
"A tin of biscuits" vs "A biscuit tin"
What are the branches of statistics?
What is this game with a red cricket pushing a ball?
Is the algebra of compact operators flat?
Do businesses save their customers' credit card information until the payment is finalized?
Why is technology bad for children?
XDSL Internet Connection Over Optical Fibre Telephone Line?
How to repeatedly un tar files that are within the tar itself
Make directory copies using findHow to scp/tar files that are in between specific days?Way to write new filename on wildcard input?tar list files onlyBash if conditions of variables within intervalsHow to copy modified files while preserving folder structureCreating a tar archive without including the parent directory when files are stored in different directoriesFind and replace Pipe using awk command
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I have a tar file that has been tar-ed many times. It follows a similar structure like this:
1000.tar
|
--- filler.txt (random text)
999.tar
|
--- filler.txt
998.tar
...
How can I write a command/piece of code to get the final .txt of the tar structure?
I can do this by hand tar -xf 1000.tar, but it's more efficient to use code.
I'm thinking the code should go like this:
for i in range 1000, 1:
tar -xf string(i) + ".tar"
shell tar
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
|
show 5 more comments
I have a tar file that has been tar-ed many times. It follows a similar structure like this:
1000.tar
|
--- filler.txt (random text)
999.tar
|
--- filler.txt
998.tar
...
How can I write a command/piece of code to get the final .txt of the tar structure?
I can do this by hand tar -xf 1000.tar, but it's more efficient to use code.
I'm thinking the code should go like this:
for i in range 1000, 1:
tar -xf string(i) + ".tar"
shell tar
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Extract to the current directory and useseqor brace expansion. Btw, is this from some sort of challenge?
– Panki
8 hours ago
@Panki yeah, it is. So i would just dotar -xf seq 1 1000 1 .tar, or something similar?
– Amin Karic
8 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Is the numbering of the.tarfiles fixed or do you have to extract as long as you find another.tarfile inside the previous one? Does every.tarfile contain at most one.tarfile? Do you want to throw away thefiller.txtfiles or other files contained in the outer.tarfiles? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.
– Bodo
7 hours ago
1
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago
|
show 5 more comments
I have a tar file that has been tar-ed many times. It follows a similar structure like this:
1000.tar
|
--- filler.txt (random text)
999.tar
|
--- filler.txt
998.tar
...
How can I write a command/piece of code to get the final .txt of the tar structure?
I can do this by hand tar -xf 1000.tar, but it's more efficient to use code.
I'm thinking the code should go like this:
for i in range 1000, 1:
tar -xf string(i) + ".tar"
shell tar
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have a tar file that has been tar-ed many times. It follows a similar structure like this:
1000.tar
|
--- filler.txt (random text)
999.tar
|
--- filler.txt
998.tar
...
How can I write a command/piece of code to get the final .txt of the tar structure?
I can do this by hand tar -xf 1000.tar, but it's more efficient to use code.
I'm thinking the code should go like this:
for i in range 1000, 1:
tar -xf string(i) + ".tar"
shell tar
shell tar
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 8 hours ago
Amin Karic
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 8 hours ago
Amin KaricAmin Karic
134 bronze badges
134 bronze badges
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amin Karic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Extract to the current directory and useseqor brace expansion. Btw, is this from some sort of challenge?
– Panki
8 hours ago
@Panki yeah, it is. So i would just dotar -xf seq 1 1000 1 .tar, or something similar?
– Amin Karic
8 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Is the numbering of the.tarfiles fixed or do you have to extract as long as you find another.tarfile inside the previous one? Does every.tarfile contain at most one.tarfile? Do you want to throw away thefiller.txtfiles or other files contained in the outer.tarfiles? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.
– Bodo
7 hours ago
1
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago
|
show 5 more comments
Extract to the current directory and useseqor brace expansion. Btw, is this from some sort of challenge?
– Panki
8 hours ago
@Panki yeah, it is. So i would just dotar -xf seq 1 1000 1 .tar, or something similar?
– Amin Karic
8 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Is the numbering of the.tarfiles fixed or do you have to extract as long as you find another.tarfile inside the previous one? Does every.tarfile contain at most one.tarfile? Do you want to throw away thefiller.txtfiles or other files contained in the outer.tarfiles? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.
– Bodo
7 hours ago
1
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago
Extract to the current directory and use
seq or brace expansion. Btw, is this from some sort of challenge?– Panki
8 hours ago
Extract to the current directory and use
seq or brace expansion. Btw, is this from some sort of challenge?– Panki
8 hours ago
@Panki yeah, it is. So i would just do
tar -xf seq 1 1000 1 .tar, or something similar?– Amin Karic
8 hours ago
@Panki yeah, it is. So i would just do
tar -xf seq 1 1000 1 .tar, or something similar?– Amin Karic
8 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Is the numbering of the
.tar files fixed or do you have to extract as long as you find another .tar file inside the previous one? Does every .tar file contain at most one .tarfile? Do you want to throw away the filler.txt files or other files contained in the outer .tar files? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.– Bodo
7 hours ago
Is the numbering of the
.tar files fixed or do you have to extract as long as you find another .tar file inside the previous one? Does every .tar file contain at most one .tarfile? Do you want to throw away the filler.txt files or other files contained in the outer .tar files? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.– Bodo
7 hours ago
1
1
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago
|
show 5 more comments
2 Answers
2
active
oldest
votes
This simple bash for loop should do what you need:
for i in 1000..1; do tar -xf $i.tar; done
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought oftar --list, but I don't think that can recurse into sub-tars. How would you get to the finalfilewithout untarring the parents?
– terdon♦
7 hours ago
@terdon Create atararchive of a text file, then look at it in an editor or something (stringsmaybe). If you know what you're looking for, all you need is a simpleawkprogram to get the last text in the archive of archives.
– Kusalananda♦
7 hours ago
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
add a comment
|
Here's a recursive approach that doesn't require you to know the filenames of the tarballs in advance. Put the file in a directory that contains nothing else. Then, assuming all the tarballs contained in the parent archive have a .tar extension, you can simply do:
file=(*tar); while [[ -e $file ]]; do tar xf "$file"; rm "$file"; file=(*tar); done
Explanation
file=(*tar);: set the variable$fileto contain the file name. There should be only one file that matches the*targlob in the directory you run this in.while [[ -e $file ]]; do: while$fileexists...tar xf $file; rm "$file"; file=(*tar);: untar the current value of$file, then delete the tarball you just extracted, and finally set the$filevariable to the name of the new, now only, tar file in the directory.
And here's an even more direct approach (thanks @kusalananda!). I made the tar archive using these commands:
$ cat file
This is the text!
$ file=file; for i in 1..1000; do tar cf $i.tar $file; file=$i.tar; done
So this is a file called file which contains the text This is the text!n. The file has been tarred 1000 times and we now have 1000.tar. We can print the original text out with:
$ awk -F'' '/[^]/print $(NF)' 1000.tar
This is the text!
Note that that isn't actually the original text. The original newline was eaten by tar but replaced by awk. To get the real value from the archive, you'd need (the last $ there is my prompt, there was no newline):
$ awk -F'' '/[^]/printf "%s", $NF' 1000.tar
This is the text!$
In this particular case, I was able to recreate the file name as well by telling awk to print the last field ($NF), and the 438th field before the last one:
awk -F'' '/[^]/print $(NF) >$(NF-438)' 1000.tar
That created a new file with the contents of the original. So the same thing as extracting. However, I don't know if the -428 is a magic number. tar seems to add multiple NULLs in its archive, so I found that by running:
$ awk -F'' 'for(i=1;i<=NF;i++) if($i ~ /file/)print i,NF-i' 1000.tar
434674 438
That told me that field 434674 had the contents file and that is 438 fields before the final one.
If your awk supports gsub, you can probably make it more general with:
awk -F'' '/[^]/gsub(/+/,""); print $NF > $(NF-11)' 1000.tar
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/4.0/"u003ecc by-sa 4.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
);
);
Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.
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%2f545543%2fhow-to-repeatedly-un-tar-files-that-are-within-the-tar-itself%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This simple bash for loop should do what you need:
for i in 1000..1; do tar -xf $i.tar; done
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought oftar --list, but I don't think that can recurse into sub-tars. How would you get to the finalfilewithout untarring the parents?
– terdon♦
7 hours ago
@terdon Create atararchive of a text file, then look at it in an editor or something (stringsmaybe). If you know what you're looking for, all you need is a simpleawkprogram to get the last text in the archive of archives.
– Kusalananda♦
7 hours ago
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
add a comment
|
This simple bash for loop should do what you need:
for i in 1000..1; do tar -xf $i.tar; done
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought oftar --list, but I don't think that can recurse into sub-tars. How would you get to the finalfilewithout untarring the parents?
– terdon♦
7 hours ago
@terdon Create atararchive of a text file, then look at it in an editor or something (stringsmaybe). If you know what you're looking for, all you need is a simpleawkprogram to get the last text in the archive of archives.
– Kusalananda♦
7 hours ago
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
add a comment
|
This simple bash for loop should do what you need:
for i in 1000..1; do tar -xf $i.tar; done
This simple bash for loop should do what you need:
for i in 1000..1; do tar -xf $i.tar; done
answered 8 hours ago
PankiPanki
1,5959 silver badges15 bronze badges
1,5959 silver badges15 bronze badges
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought oftar --list, but I don't think that can recurse into sub-tars. How would you get to the finalfilewithout untarring the parents?
– terdon♦
7 hours ago
@terdon Create atararchive of a text file, then look at it in an editor or something (stringsmaybe). If you know what you're looking for, all you need is a simpleawkprogram to get the last text in the archive of archives.
– Kusalananda♦
7 hours ago
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
add a comment
|
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought oftar --list, but I don't think that can recurse into sub-tars. How would you get to the finalfilewithout untarring the parents?
– terdon♦
7 hours ago
@terdon Create atararchive of a text file, then look at it in an editor or something (stringsmaybe). If you know what you're looking for, all you need is a simpleawkprogram to get the last text in the archive of archives.
– Kusalananda♦
7 hours ago
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
Ohh, thanks so much! Btw, my directory has a size quota, but i can just make it so it deletes the original tar after.
– Amin Karic
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
You don't have to extract the archive at all if you know what to look for. The archives are not compressed, so the files inside it would be (mostly) readable.
– Kusalananda♦
7 hours ago
@Kusalananda how? I thought of
tar --list, but I don't think that can recurse into sub-tars. How would you get to the final file without untarring the parents?– terdon♦
7 hours ago
@Kusalananda how? I thought of
tar --list, but I don't think that can recurse into sub-tars. How would you get to the final file without untarring the parents?– terdon♦
7 hours ago
@terdon Create a
tar archive of a text file, then look at it in an editor or something (strings maybe). If you know what you're looking for, all you need is a simple awk program to get the last text in the archive of archives.– Kusalananda♦
7 hours ago
@terdon Create a
tar archive of a text file, then look at it in an editor or something (strings maybe). If you know what you're looking for, all you need is a simple awk program to get the last text in the archive of archives.– Kusalananda♦
7 hours ago
1
1
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
@Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.
– terdon♦
7 hours ago
add a comment
|
Here's a recursive approach that doesn't require you to know the filenames of the tarballs in advance. Put the file in a directory that contains nothing else. Then, assuming all the tarballs contained in the parent archive have a .tar extension, you can simply do:
file=(*tar); while [[ -e $file ]]; do tar xf "$file"; rm "$file"; file=(*tar); done
Explanation
file=(*tar);: set the variable$fileto contain the file name. There should be only one file that matches the*targlob in the directory you run this in.while [[ -e $file ]]; do: while$fileexists...tar xf $file; rm "$file"; file=(*tar);: untar the current value of$file, then delete the tarball you just extracted, and finally set the$filevariable to the name of the new, now only, tar file in the directory.
And here's an even more direct approach (thanks @kusalananda!). I made the tar archive using these commands:
$ cat file
This is the text!
$ file=file; for i in 1..1000; do tar cf $i.tar $file; file=$i.tar; done
So this is a file called file which contains the text This is the text!n. The file has been tarred 1000 times and we now have 1000.tar. We can print the original text out with:
$ awk -F'' '/[^]/print $(NF)' 1000.tar
This is the text!
Note that that isn't actually the original text. The original newline was eaten by tar but replaced by awk. To get the real value from the archive, you'd need (the last $ there is my prompt, there was no newline):
$ awk -F'' '/[^]/printf "%s", $NF' 1000.tar
This is the text!$
In this particular case, I was able to recreate the file name as well by telling awk to print the last field ($NF), and the 438th field before the last one:
awk -F'' '/[^]/print $(NF) >$(NF-438)' 1000.tar
That created a new file with the contents of the original. So the same thing as extracting. However, I don't know if the -428 is a magic number. tar seems to add multiple NULLs in its archive, so I found that by running:
$ awk -F'' 'for(i=1;i<=NF;i++) if($i ~ /file/)print i,NF-i' 1000.tar
434674 438
That told me that field 434674 had the contents file and that is 438 fields before the final one.
If your awk supports gsub, you can probably make it more general with:
awk -F'' '/[^]/gsub(/+/,""); print $NF > $(NF-11)' 1000.tar
add a comment
|
Here's a recursive approach that doesn't require you to know the filenames of the tarballs in advance. Put the file in a directory that contains nothing else. Then, assuming all the tarballs contained in the parent archive have a .tar extension, you can simply do:
file=(*tar); while [[ -e $file ]]; do tar xf "$file"; rm "$file"; file=(*tar); done
Explanation
file=(*tar);: set the variable$fileto contain the file name. There should be only one file that matches the*targlob in the directory you run this in.while [[ -e $file ]]; do: while$fileexists...tar xf $file; rm "$file"; file=(*tar);: untar the current value of$file, then delete the tarball you just extracted, and finally set the$filevariable to the name of the new, now only, tar file in the directory.
And here's an even more direct approach (thanks @kusalananda!). I made the tar archive using these commands:
$ cat file
This is the text!
$ file=file; for i in 1..1000; do tar cf $i.tar $file; file=$i.tar; done
So this is a file called file which contains the text This is the text!n. The file has been tarred 1000 times and we now have 1000.tar. We can print the original text out with:
$ awk -F'' '/[^]/print $(NF)' 1000.tar
This is the text!
Note that that isn't actually the original text. The original newline was eaten by tar but replaced by awk. To get the real value from the archive, you'd need (the last $ there is my prompt, there was no newline):
$ awk -F'' '/[^]/printf "%s", $NF' 1000.tar
This is the text!$
In this particular case, I was able to recreate the file name as well by telling awk to print the last field ($NF), and the 438th field before the last one:
awk -F'' '/[^]/print $(NF) >$(NF-438)' 1000.tar
That created a new file with the contents of the original. So the same thing as extracting. However, I don't know if the -428 is a magic number. tar seems to add multiple NULLs in its archive, so I found that by running:
$ awk -F'' 'for(i=1;i<=NF;i++) if($i ~ /file/)print i,NF-i' 1000.tar
434674 438
That told me that field 434674 had the contents file and that is 438 fields before the final one.
If your awk supports gsub, you can probably make it more general with:
awk -F'' '/[^]/gsub(/+/,""); print $NF > $(NF-11)' 1000.tar
add a comment
|
Here's a recursive approach that doesn't require you to know the filenames of the tarballs in advance. Put the file in a directory that contains nothing else. Then, assuming all the tarballs contained in the parent archive have a .tar extension, you can simply do:
file=(*tar); while [[ -e $file ]]; do tar xf "$file"; rm "$file"; file=(*tar); done
Explanation
file=(*tar);: set the variable$fileto contain the file name. There should be only one file that matches the*targlob in the directory you run this in.while [[ -e $file ]]; do: while$fileexists...tar xf $file; rm "$file"; file=(*tar);: untar the current value of$file, then delete the tarball you just extracted, and finally set the$filevariable to the name of the new, now only, tar file in the directory.
And here's an even more direct approach (thanks @kusalananda!). I made the tar archive using these commands:
$ cat file
This is the text!
$ file=file; for i in 1..1000; do tar cf $i.tar $file; file=$i.tar; done
So this is a file called file which contains the text This is the text!n. The file has been tarred 1000 times and we now have 1000.tar. We can print the original text out with:
$ awk -F'' '/[^]/print $(NF)' 1000.tar
This is the text!
Note that that isn't actually the original text. The original newline was eaten by tar but replaced by awk. To get the real value from the archive, you'd need (the last $ there is my prompt, there was no newline):
$ awk -F'' '/[^]/printf "%s", $NF' 1000.tar
This is the text!$
In this particular case, I was able to recreate the file name as well by telling awk to print the last field ($NF), and the 438th field before the last one:
awk -F'' '/[^]/print $(NF) >$(NF-438)' 1000.tar
That created a new file with the contents of the original. So the same thing as extracting. However, I don't know if the -428 is a magic number. tar seems to add multiple NULLs in its archive, so I found that by running:
$ awk -F'' 'for(i=1;i<=NF;i++) if($i ~ /file/)print i,NF-i' 1000.tar
434674 438
That told me that field 434674 had the contents file and that is 438 fields before the final one.
If your awk supports gsub, you can probably make it more general with:
awk -F'' '/[^]/gsub(/+/,""); print $NF > $(NF-11)' 1000.tar
Here's a recursive approach that doesn't require you to know the filenames of the tarballs in advance. Put the file in a directory that contains nothing else. Then, assuming all the tarballs contained in the parent archive have a .tar extension, you can simply do:
file=(*tar); while [[ -e $file ]]; do tar xf "$file"; rm "$file"; file=(*tar); done
Explanation
file=(*tar);: set the variable$fileto contain the file name. There should be only one file that matches the*targlob in the directory you run this in.while [[ -e $file ]]; do: while$fileexists...tar xf $file; rm "$file"; file=(*tar);: untar the current value of$file, then delete the tarball you just extracted, and finally set the$filevariable to the name of the new, now only, tar file in the directory.
And here's an even more direct approach (thanks @kusalananda!). I made the tar archive using these commands:
$ cat file
This is the text!
$ file=file; for i in 1..1000; do tar cf $i.tar $file; file=$i.tar; done
So this is a file called file which contains the text This is the text!n. The file has been tarred 1000 times and we now have 1000.tar. We can print the original text out with:
$ awk -F'' '/[^]/print $(NF)' 1000.tar
This is the text!
Note that that isn't actually the original text. The original newline was eaten by tar but replaced by awk. To get the real value from the archive, you'd need (the last $ there is my prompt, there was no newline):
$ awk -F'' '/[^]/printf "%s", $NF' 1000.tar
This is the text!$
In this particular case, I was able to recreate the file name as well by telling awk to print the last field ($NF), and the 438th field before the last one:
awk -F'' '/[^]/print $(NF) >$(NF-438)' 1000.tar
That created a new file with the contents of the original. So the same thing as extracting. However, I don't know if the -428 is a magic number. tar seems to add multiple NULLs in its archive, so I found that by running:
$ awk -F'' 'for(i=1;i<=NF;i++) if($i ~ /file/)print i,NF-i' 1000.tar
434674 438
That told me that field 434674 had the contents file and that is 438 fields before the final one.
If your awk supports gsub, you can probably make it more general with:
awk -F'' '/[^]/gsub(/+/,""); print $NF > $(NF-11)' 1000.tar
edited 7 hours ago
answered 7 hours ago
terdon♦terdon
143k35 gold badges296 silver badges473 bronze badges
143k35 gold badges296 silver badges473 bronze badges
add a comment
|
add a comment
|
Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.
Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.
Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.
Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.
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%2f545543%2fhow-to-repeatedly-un-tar-files-that-are-within-the-tar-itself%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
Extract to the current directory and use
seqor brace expansion. Btw, is this from some sort of challenge?– Panki
8 hours ago
@Panki yeah, it is. So i would just do
tar -xf seq 1 1000 1 .tar, or something similar?– Amin Karic
8 hours ago
Almost ;) See my answer below.
– Panki
7 hours ago
Is the numbering of the
.tarfiles fixed or do you have to extract as long as you find another.tarfile inside the previous one? Does every.tarfile contain at most one.tarfile? Do you want to throw away thefiller.txtfiles or other files contained in the outer.tarfiles? Please edit your question to add more details. This will make the question and answer(s) more helpful for others.– Bodo
7 hours ago
1
@RuiFRibeiro its supposedly "forensics"
– Amin Karic
7 hours ago