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;









1

















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"









share|improve this question









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 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












  • 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







  • 1





    @RuiFRibeiro its supposedly "forensics"

    – Amin Karic
    7 hours ago

















1

















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"









share|improve this question









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 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












  • 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







  • 1





    @RuiFRibeiro its supposedly "forensics"

    – Amin Karic
    7 hours ago













1












1








1


1






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"









share|improve this question









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






share|improve this question









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.










share|improve this question









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.








share|improve this question




share|improve this question



share|improve this question








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 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












  • 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







  • 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











  • @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 .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





    @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










2 Answers
2






active

oldest

votes


















2


















This simple bash for loop should do what you need:



for i in 1000..1; do tar -xf $i.tar; done





share|improve this answer


























  • 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 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







  • 1





    @Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.

    – terdon
    7 hours ago


















2


















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 $file to contain the file name. There should be only one file that matches the *tar glob in the directory you run this in.


  • while [[ -e $file ]]; do : while $file exists...


  • tar xf $file; rm "$file"; file=(*tar); : untar the current value of $file, then delete the tarball you just extracted, and finally set the $file variable 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 





share|improve this answer





























    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.









    draft saved

    draft discarded
















    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









    2


















    This simple bash for loop should do what you need:



    for i in 1000..1; do tar -xf $i.tar; done





    share|improve this answer


























    • 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 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







    • 1





      @Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.

      – terdon
      7 hours ago















    2


















    This simple bash for loop should do what you need:



    for i in 1000..1; do tar -xf $i.tar; done





    share|improve this answer


























    • 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 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







    • 1





      @Kusalananda Nice! I had no idea. Just posted an answer with that approach, hope you don't mind.

      – terdon
      7 hours ago













    2














    2










    2









    This simple bash for loop should do what you need:



    for i in 1000..1; do tar -xf $i.tar; done





    share|improve this answer














    This simple bash for loop should do what you need:



    for i in 1000..1; do tar -xf $i.tar; done






    share|improve this answer













    share|improve this answer




    share|improve this answer



    share|improve this answer










    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 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







    • 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











    • 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











    • @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





      @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













    2


















    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 $file to contain the file name. There should be only one file that matches the *tar glob in the directory you run this in.


    • while [[ -e $file ]]; do : while $file exists...


    • tar xf $file; rm "$file"; file=(*tar); : untar the current value of $file, then delete the tarball you just extracted, and finally set the $file variable 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 





    share|improve this answer
































      2


















      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 $file to contain the file name. There should be only one file that matches the *tar glob in the directory you run this in.


      • while [[ -e $file ]]; do : while $file exists...


      • tar xf $file; rm "$file"; file=(*tar); : untar the current value of $file, then delete the tarball you just extracted, and finally set the $file variable 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 





      share|improve this answer






























        2














        2










        2









        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 $file to contain the file name. There should be only one file that matches the *tar glob in the directory you run this in.


        • while [[ -e $file ]]; do : while $file exists...


        • tar xf $file; rm "$file"; file=(*tar); : untar the current value of $file, then delete the tarball you just extracted, and finally set the $file variable 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 





        share|improve this answer
















        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 $file to contain the file name. There should be only one file that matches the *tar glob in the directory you run this in.


        • while [[ -e $file ]]; do : while $file exists...


        • tar xf $file; rm "$file"; file=(*tar); : untar the current value of $file, then delete the tarball you just extracted, and finally set the $file variable 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 






        share|improve this answer















        share|improve this answer




        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 7 hours ago









        terdonterdon

        143k35 gold badges296 silver badges473 bronze badges




        143k35 gold badges296 silver badges473 bronze badges
























            Amin Karic is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded

















            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.




            draft saved


            draft discarded














            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





















































            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









            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480