How to untar all .tgz files in a folder and write to new files with different Naming convention












1















I have folder thay contains many .tgz files, each uncompressed file has SAME file name "000000_0".



Can someone write me a command to do below?



1. Untar all the .tgz files from a folder
2. rename each uncompressed file based on the .tgz file name


For example, 2017-07-01.tgz -> I want the uncompressed file to be renamed as "2017-07-01.dat"



Suppose I have list of .tgz file in a directory:



2017-07-01.tgz
2017-07-02.tgz
2017-07-03.tgz
2017-07-04.tgz
2017-07-05.tgz


Note: each .tgz file only have one file inside, "000000_0".










share|improve this question



























    1















    I have folder thay contains many .tgz files, each uncompressed file has SAME file name "000000_0".



    Can someone write me a command to do below?



    1. Untar all the .tgz files from a folder
    2. rename each uncompressed file based on the .tgz file name


    For example, 2017-07-01.tgz -> I want the uncompressed file to be renamed as "2017-07-01.dat"



    Suppose I have list of .tgz file in a directory:



    2017-07-01.tgz
    2017-07-02.tgz
    2017-07-03.tgz
    2017-07-04.tgz
    2017-07-05.tgz


    Note: each .tgz file only have one file inside, "000000_0".










    share|improve this question

























      1












      1








      1








      I have folder thay contains many .tgz files, each uncompressed file has SAME file name "000000_0".



      Can someone write me a command to do below?



      1. Untar all the .tgz files from a folder
      2. rename each uncompressed file based on the .tgz file name


      For example, 2017-07-01.tgz -> I want the uncompressed file to be renamed as "2017-07-01.dat"



      Suppose I have list of .tgz file in a directory:



      2017-07-01.tgz
      2017-07-02.tgz
      2017-07-03.tgz
      2017-07-04.tgz
      2017-07-05.tgz


      Note: each .tgz file only have one file inside, "000000_0".










      share|improve this question














      I have folder thay contains many .tgz files, each uncompressed file has SAME file name "000000_0".



      Can someone write me a command to do below?



      1. Untar all the .tgz files from a folder
      2. rename each uncompressed file based on the .tgz file name


      For example, 2017-07-01.tgz -> I want the uncompressed file to be renamed as "2017-07-01.dat"



      Suppose I have list of .tgz file in a directory:



      2017-07-01.tgz
      2017-07-02.tgz
      2017-07-03.tgz
      2017-07-04.tgz
      2017-07-05.tgz


      Note: each .tgz file only have one file inside, "000000_0".







      linux shell tar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 6 at 19:27









      azCatsazCats

      82




      82






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Using gnu parallel:



          $ parallel 'tar -xf {} --to-stdout > {.}.dat' ::: *.tgz


          For each .tgz file the file is uncompressed (-xf) and the content of the file streamed stdout (--to-stdout). So we can redirect (>) it to a file with a new filename, using the basename of the compressed file ({.}.dat)



          Without gnu parallel:



          for file in *.tgz; do 
          tar -xf $file --to-stdout > $(basename -s .tgz $file).dat;
          done


          If the basename version is to old, the -s parameter doesn't exist. Use $(basename $file .tgz) instead. This should work in current versions as well.






          share|improve this answer


























          • i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

            – azCats
            Feb 6 at 19:54











          • See my edit please.

            – finswimmer
            Feb 6 at 20:03











          • I got this error: basename: invalid option -- s

            – azCats
            Feb 6 at 20:11











          • basename --version basename (GNU coreutils) 5.97

            – azCats
            Feb 6 at 20:12











          • Wow, this version is from the stone age ;) Edit my post again.

            – finswimmer
            Feb 6 at 20:17











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499142%2fhow-to-untar-all-tgz-files-in-a-folder-and-write-to-new-files-with-different-na%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Using gnu parallel:



          $ parallel 'tar -xf {} --to-stdout > {.}.dat' ::: *.tgz


          For each .tgz file the file is uncompressed (-xf) and the content of the file streamed stdout (--to-stdout). So we can redirect (>) it to a file with a new filename, using the basename of the compressed file ({.}.dat)



          Without gnu parallel:



          for file in *.tgz; do 
          tar -xf $file --to-stdout > $(basename -s .tgz $file).dat;
          done


          If the basename version is to old, the -s parameter doesn't exist. Use $(basename $file .tgz) instead. This should work in current versions as well.






          share|improve this answer


























          • i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

            – azCats
            Feb 6 at 19:54











          • See my edit please.

            – finswimmer
            Feb 6 at 20:03











          • I got this error: basename: invalid option -- s

            – azCats
            Feb 6 at 20:11











          • basename --version basename (GNU coreutils) 5.97

            – azCats
            Feb 6 at 20:12











          • Wow, this version is from the stone age ;) Edit my post again.

            – finswimmer
            Feb 6 at 20:17
















          1














          Using gnu parallel:



          $ parallel 'tar -xf {} --to-stdout > {.}.dat' ::: *.tgz


          For each .tgz file the file is uncompressed (-xf) and the content of the file streamed stdout (--to-stdout). So we can redirect (>) it to a file with a new filename, using the basename of the compressed file ({.}.dat)



          Without gnu parallel:



          for file in *.tgz; do 
          tar -xf $file --to-stdout > $(basename -s .tgz $file).dat;
          done


          If the basename version is to old, the -s parameter doesn't exist. Use $(basename $file .tgz) instead. This should work in current versions as well.






          share|improve this answer


























          • i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

            – azCats
            Feb 6 at 19:54











          • See my edit please.

            – finswimmer
            Feb 6 at 20:03











          • I got this error: basename: invalid option -- s

            – azCats
            Feb 6 at 20:11











          • basename --version basename (GNU coreutils) 5.97

            – azCats
            Feb 6 at 20:12











          • Wow, this version is from the stone age ;) Edit my post again.

            – finswimmer
            Feb 6 at 20:17














          1












          1








          1







          Using gnu parallel:



          $ parallel 'tar -xf {} --to-stdout > {.}.dat' ::: *.tgz


          For each .tgz file the file is uncompressed (-xf) and the content of the file streamed stdout (--to-stdout). So we can redirect (>) it to a file with a new filename, using the basename of the compressed file ({.}.dat)



          Without gnu parallel:



          for file in *.tgz; do 
          tar -xf $file --to-stdout > $(basename -s .tgz $file).dat;
          done


          If the basename version is to old, the -s parameter doesn't exist. Use $(basename $file .tgz) instead. This should work in current versions as well.






          share|improve this answer















          Using gnu parallel:



          $ parallel 'tar -xf {} --to-stdout > {.}.dat' ::: *.tgz


          For each .tgz file the file is uncompressed (-xf) and the content of the file streamed stdout (--to-stdout). So we can redirect (>) it to a file with a new filename, using the basename of the compressed file ({.}.dat)



          Without gnu parallel:



          for file in *.tgz; do 
          tar -xf $file --to-stdout > $(basename -s .tgz $file).dat;
          done


          If the basename version is to old, the -s parameter doesn't exist. Use $(basename $file .tgz) instead. This should work in current versions as well.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 6 at 20:16

























          answered Feb 6 at 19:50









          finswimmerfinswimmer

          60917




          60917













          • i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

            – azCats
            Feb 6 at 19:54











          • See my edit please.

            – finswimmer
            Feb 6 at 20:03











          • I got this error: basename: invalid option -- s

            – azCats
            Feb 6 at 20:11











          • basename --version basename (GNU coreutils) 5.97

            – azCats
            Feb 6 at 20:12











          • Wow, this version is from the stone age ;) Edit my post again.

            – finswimmer
            Feb 6 at 20:17



















          • i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

            – azCats
            Feb 6 at 19:54











          • See my edit please.

            – finswimmer
            Feb 6 at 20:03











          • I got this error: basename: invalid option -- s

            – azCats
            Feb 6 at 20:11











          • basename --version basename (GNU coreutils) 5.97

            – azCats
            Feb 6 at 20:12











          • Wow, this version is from the stone age ;) Edit my post again.

            – finswimmer
            Feb 6 at 20:17

















          i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

          – azCats
          Feb 6 at 19:54





          i don't have the privilege to install GNU parallel utility, is there any alternative way to do this?

          – azCats
          Feb 6 at 19:54













          See my edit please.

          – finswimmer
          Feb 6 at 20:03





          See my edit please.

          – finswimmer
          Feb 6 at 20:03













          I got this error: basename: invalid option -- s

          – azCats
          Feb 6 at 20:11





          I got this error: basename: invalid option -- s

          – azCats
          Feb 6 at 20:11













          basename --version basename (GNU coreutils) 5.97

          – azCats
          Feb 6 at 20:12





          basename --version basename (GNU coreutils) 5.97

          – azCats
          Feb 6 at 20:12













          Wow, this version is from the stone age ;) Edit my post again.

          – finswimmer
          Feb 6 at 20:17





          Wow, this version is from the stone age ;) Edit my post again.

          – finswimmer
          Feb 6 at 20:17


















          draft saved

          draft discarded




















































          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%2f499142%2fhow-to-untar-all-tgz-files-in-a-folder-and-write-to-new-files-with-different-na%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

          How to make a Squid Proxy server?

          Is this a new Fibonacci Identity?

          19世紀