How to tar all directories in a directory to tar files named after tared directories

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-2
down vote

favorite












I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh



dir___
|- dirA
|- dirB
|- tar-all.sh


becomes



dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh









share|improve this question





















  • Go ahead and post the code you've written so far, so we can point you in the right direction.
    – Emmanuel Rosa
    Aug 10 at 23:32














up vote
-2
down vote

favorite












I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh



dir___
|- dirA
|- dirB
|- tar-all.sh


becomes



dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh









share|improve this question





















  • Go ahead and post the code you've written so far, so we can point you in the right direction.
    – Emmanuel Rosa
    Aug 10 at 23:32












up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh



dir___
|- dirA
|- dirB
|- tar-all.sh


becomes



dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh









share|improve this question













I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh



dir___
|- dirA
|- dirB
|- tar-all.sh


becomes



dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh






linux tar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 10 at 23:18









the_prole

1153




1153











  • Go ahead and post the code you've written so far, so we can point you in the right direction.
    – Emmanuel Rosa
    Aug 10 at 23:32
















  • Go ahead and post the code you've written so far, so we can point you in the right direction.
    – Emmanuel Rosa
    Aug 10 at 23:32















Go ahead and post the code you've written so far, so we can point you in the right direction.
– Emmanuel Rosa
Aug 10 at 23:32




Go ahead and post the code you've written so far, so we can point you in the right direction.
– Emmanuel Rosa
Aug 10 at 23:32










2 Answers
2






active

oldest

votes

















up vote
0
down vote













Use a for loop:



#!/bin/bash

for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done


Results in:



> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!

|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz





share|improve this answer





























    up vote
    0
    down vote













    The following will find and archive all the dir directory's subdirectories (deleting the original subdirectories).



    for subdir in dir/*/; do
    dirname="$( basename "$subdir" )"
    ( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
    done


    The pattern dir/*/ will only match the pathnames of the subdirectories under dir and no non-directories, due to the / at the end.



    Alternatively, using the -C option of tar instead of an explicit cd:



    for subdir in dir/*/; do
    dirname="$( basename "$subdir" )"
    tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
    done


    Testing:



    $ tree
    .
    `-- dir
    |-- a
    | `-- file
    |-- b
    | `-- file
    |-- c
    | `-- file
    `-- complicated name
    `-- file

    5 directories, 4 files


    (run any of the two loops here)



    $ tree
    .
    `-- dir
    |-- a.tgz
    |-- b.tgz
    |-- c.tgz
    `-- complicated name.tgz

    1 directory, 4 files





    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f461918%2fhow-to-tar-all-directories-in-a-directory-to-tar-files-named-after-tared-directo%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Use a for loop:



      #!/bin/bash

      for f in *
      do
      echo "Creating tarball of $f..."
      tar -zcf $f.tar.gz $f
      done


      Results in:



      > ./tar-all.sh
      > Creating tarball of tar-all.sh...
      > Creating tarball of dirA...
      > Creating tarball of dirB...
      > DONE!

      |- dirA.tar.tgz
      |- dirB.tar.tgz
      |- tar-all.sh.tar.gz





      share|improve this answer


























        up vote
        0
        down vote













        Use a for loop:



        #!/bin/bash

        for f in *
        do
        echo "Creating tarball of $f..."
        tar -zcf $f.tar.gz $f
        done


        Results in:



        > ./tar-all.sh
        > Creating tarball of tar-all.sh...
        > Creating tarball of dirA...
        > Creating tarball of dirB...
        > DONE!

        |- dirA.tar.tgz
        |- dirB.tar.tgz
        |- tar-all.sh.tar.gz





        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          Use a for loop:



          #!/bin/bash

          for f in *
          do
          echo "Creating tarball of $f..."
          tar -zcf $f.tar.gz $f
          done


          Results in:



          > ./tar-all.sh
          > Creating tarball of tar-all.sh...
          > Creating tarball of dirA...
          > Creating tarball of dirB...
          > DONE!

          |- dirA.tar.tgz
          |- dirB.tar.tgz
          |- tar-all.sh.tar.gz





          share|improve this answer














          Use a for loop:



          #!/bin/bash

          for f in *
          do
          echo "Creating tarball of $f..."
          tar -zcf $f.tar.gz $f
          done


          Results in:



          > ./tar-all.sh
          > Creating tarball of tar-all.sh...
          > Creating tarball of dirA...
          > Creating tarball of dirB...
          > DONE!

          |- dirA.tar.tgz
          |- dirB.tar.tgz
          |- tar-all.sh.tar.gz






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 11 at 8:18









          confetti

          25513




          25513










          answered Aug 10 at 23:36









          Eminent

          112




          112






















              up vote
              0
              down vote













              The following will find and archive all the dir directory's subdirectories (deleting the original subdirectories).



              for subdir in dir/*/; do
              dirname="$( basename "$subdir" )"
              ( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
              done


              The pattern dir/*/ will only match the pathnames of the subdirectories under dir and no non-directories, due to the / at the end.



              Alternatively, using the -C option of tar instead of an explicit cd:



              for subdir in dir/*/; do
              dirname="$( basename "$subdir" )"
              tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
              done


              Testing:



              $ tree
              .
              `-- dir
              |-- a
              | `-- file
              |-- b
              | `-- file
              |-- c
              | `-- file
              `-- complicated name
              `-- file

              5 directories, 4 files


              (run any of the two loops here)



              $ tree
              .
              `-- dir
              |-- a.tgz
              |-- b.tgz
              |-- c.tgz
              `-- complicated name.tgz

              1 directory, 4 files





              share|improve this answer
























                up vote
                0
                down vote













                The following will find and archive all the dir directory's subdirectories (deleting the original subdirectories).



                for subdir in dir/*/; do
                dirname="$( basename "$subdir" )"
                ( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
                done


                The pattern dir/*/ will only match the pathnames of the subdirectories under dir and no non-directories, due to the / at the end.



                Alternatively, using the -C option of tar instead of an explicit cd:



                for subdir in dir/*/; do
                dirname="$( basename "$subdir" )"
                tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
                done


                Testing:



                $ tree
                .
                `-- dir
                |-- a
                | `-- file
                |-- b
                | `-- file
                |-- c
                | `-- file
                `-- complicated name
                `-- file

                5 directories, 4 files


                (run any of the two loops here)



                $ tree
                .
                `-- dir
                |-- a.tgz
                |-- b.tgz
                |-- c.tgz
                `-- complicated name.tgz

                1 directory, 4 files





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  The following will find and archive all the dir directory's subdirectories (deleting the original subdirectories).



                  for subdir in dir/*/; do
                  dirname="$( basename "$subdir" )"
                  ( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
                  done


                  The pattern dir/*/ will only match the pathnames of the subdirectories under dir and no non-directories, due to the / at the end.



                  Alternatively, using the -C option of tar instead of an explicit cd:



                  for subdir in dir/*/; do
                  dirname="$( basename "$subdir" )"
                  tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
                  done


                  Testing:



                  $ tree
                  .
                  `-- dir
                  |-- a
                  | `-- file
                  |-- b
                  | `-- file
                  |-- c
                  | `-- file
                  `-- complicated name
                  `-- file

                  5 directories, 4 files


                  (run any of the two loops here)



                  $ tree
                  .
                  `-- dir
                  |-- a.tgz
                  |-- b.tgz
                  |-- c.tgz
                  `-- complicated name.tgz

                  1 directory, 4 files





                  share|improve this answer












                  The following will find and archive all the dir directory's subdirectories (deleting the original subdirectories).



                  for subdir in dir/*/; do
                  dirname="$( basename "$subdir" )"
                  ( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
                  done


                  The pattern dir/*/ will only match the pathnames of the subdirectories under dir and no non-directories, due to the / at the end.



                  Alternatively, using the -C option of tar instead of an explicit cd:



                  for subdir in dir/*/; do
                  dirname="$( basename "$subdir" )"
                  tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
                  done


                  Testing:



                  $ tree
                  .
                  `-- dir
                  |-- a
                  | `-- file
                  |-- b
                  | `-- file
                  |-- c
                  | `-- file
                  `-- complicated name
                  `-- file

                  5 directories, 4 files


                  (run any of the two loops here)



                  $ tree
                  .
                  `-- dir
                  |-- a.tgz
                  |-- b.tgz
                  |-- c.tgz
                  `-- complicated name.tgz

                  1 directory, 4 files






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 12 at 8:49









                  Kusalananda

                  106k14209327




                  106k14209327



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461918%2fhow-to-tar-all-directories-in-a-directory-to-tar-files-named-after-tared-directo%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?