Create target directory when extracting tarball

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











up vote
14
down vote

favorite
6












Is it possible to create a target directory, similar to mkdir -p, where I can define a non-existent target directory within my tar command, and tar will create the directory for me?



I know I can redirect the output to a directory using tar -C /target/dir, but this doesn't work if the target directory is non-existent.










share|improve this question



















  • 3




    If it isn't possible, you could always create a shell alias.
    – n0pe
    Nov 25 '11 at 4:02














up vote
14
down vote

favorite
6












Is it possible to create a target directory, similar to mkdir -p, where I can define a non-existent target directory within my tar command, and tar will create the directory for me?



I know I can redirect the output to a directory using tar -C /target/dir, but this doesn't work if the target directory is non-existent.










share|improve this question



















  • 3




    If it isn't possible, you could always create a shell alias.
    – n0pe
    Nov 25 '11 at 4:02












up vote
14
down vote

favorite
6









up vote
14
down vote

favorite
6






6





Is it possible to create a target directory, similar to mkdir -p, where I can define a non-existent target directory within my tar command, and tar will create the directory for me?



I know I can redirect the output to a directory using tar -C /target/dir, but this doesn't work if the target directory is non-existent.










share|improve this question















Is it possible to create a target directory, similar to mkdir -p, where I can define a non-existent target directory within my tar command, and tar will create the directory for me?



I know I can redirect the output to a directory using tar -C /target/dir, but this doesn't work if the target directory is non-existent.







directory tar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 28 '15 at 2:49

























asked Nov 25 '11 at 3:49









wulfgarpro

188128




188128







  • 3




    If it isn't possible, you could always create a shell alias.
    – n0pe
    Nov 25 '11 at 4:02












  • 3




    If it isn't possible, you could always create a shell alias.
    – n0pe
    Nov 25 '11 at 4:02







3




3




If it isn't possible, you could always create a shell alias.
– n0pe
Nov 25 '11 at 4:02




If it isn't possible, you could always create a shell alias.
– n0pe
Nov 25 '11 at 4:02










4 Answers
4






active

oldest

votes

















up vote
27
down vote



accepted










mkdir -p /target/dir && tar -C /target/dir





share|improve this answer
















  • 2




    No spoonfeeding, straight and to the point.
    – Amado Martinez
    Oct 31 '16 at 6:56






  • 5




    Minor suggested change: mkdir -p /target/dir && tar -C $_
    – Mark Melville
    Jan 13 '17 at 22:41


















up vote
5
down vote













This made more sense to me mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder



mkdir makes the folder although i dont quite understand the -p switch. && lets you execute a second command. i used typical tar -switches but at the end -C is used to change directories and extract to that location needed.



reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive






share|improve this answer


















  • 2




    The -p switch will not throw an error if the directory already exists.
    – David
    Sep 3 '15 at 21:10










  • @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
    – Antonio Pérez
    Oct 21 '15 at 13:40

















up vote
1
down vote













If you don't know the paths to create...

Get the needed paths from the tar file, then create them...



for P in `tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6`; do 
C=`echo "$P: -1"` #get the last character
if [ $C = '/' ]; then
echo "Found directory: $P"
mkdir -p $P
fi
done





share|improve this answer





























    up vote
    0
    down vote













    tar -xvf bash.html_node.tar.gz --one-top-level



    from man page of tar command



    --one-top-level[=DIR]
    Extract all files into DIR, or, if used without argument, into a
    subdirectory named by the base name of the archive (minus stan‐
    dard compression suffixes recognizable by --auto-compress).





    share








    New contributor




    Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      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%2f25311%2fcreate-target-directory-when-extracting-tarball%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      27
      down vote



      accepted










      mkdir -p /target/dir && tar -C /target/dir





      share|improve this answer
















      • 2




        No spoonfeeding, straight and to the point.
        – Amado Martinez
        Oct 31 '16 at 6:56






      • 5




        Minor suggested change: mkdir -p /target/dir && tar -C $_
        – Mark Melville
        Jan 13 '17 at 22:41















      up vote
      27
      down vote



      accepted










      mkdir -p /target/dir && tar -C /target/dir





      share|improve this answer
















      • 2




        No spoonfeeding, straight and to the point.
        – Amado Martinez
        Oct 31 '16 at 6:56






      • 5




        Minor suggested change: mkdir -p /target/dir && tar -C $_
        – Mark Melville
        Jan 13 '17 at 22:41













      up vote
      27
      down vote



      accepted







      up vote
      27
      down vote



      accepted






      mkdir -p /target/dir && tar -C /target/dir





      share|improve this answer












      mkdir -p /target/dir && tar -C /target/dir






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 25 '11 at 4:21









      V for V

      28632




      28632







      • 2




        No spoonfeeding, straight and to the point.
        – Amado Martinez
        Oct 31 '16 at 6:56






      • 5




        Minor suggested change: mkdir -p /target/dir && tar -C $_
        – Mark Melville
        Jan 13 '17 at 22:41













      • 2




        No spoonfeeding, straight and to the point.
        – Amado Martinez
        Oct 31 '16 at 6:56






      • 5




        Minor suggested change: mkdir -p /target/dir && tar -C $_
        – Mark Melville
        Jan 13 '17 at 22:41








      2




      2




      No spoonfeeding, straight and to the point.
      – Amado Martinez
      Oct 31 '16 at 6:56




      No spoonfeeding, straight and to the point.
      – Amado Martinez
      Oct 31 '16 at 6:56




      5




      5




      Minor suggested change: mkdir -p /target/dir && tar -C $_
      – Mark Melville
      Jan 13 '17 at 22:41





      Minor suggested change: mkdir -p /target/dir && tar -C $_
      – Mark Melville
      Jan 13 '17 at 22:41













      up vote
      5
      down vote













      This made more sense to me mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder



      mkdir makes the folder although i dont quite understand the -p switch. && lets you execute a second command. i used typical tar -switches but at the end -C is used to change directories and extract to that location needed.



      reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive






      share|improve this answer


















      • 2




        The -p switch will not throw an error if the directory already exists.
        – David
        Sep 3 '15 at 21:10










      • @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
        – Antonio Pérez
        Oct 21 '15 at 13:40














      up vote
      5
      down vote













      This made more sense to me mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder



      mkdir makes the folder although i dont quite understand the -p switch. && lets you execute a second command. i used typical tar -switches but at the end -C is used to change directories and extract to that location needed.



      reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive






      share|improve this answer


















      • 2




        The -p switch will not throw an error if the directory already exists.
        – David
        Sep 3 '15 at 21:10










      • @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
        – Antonio Pérez
        Oct 21 '15 at 13:40












      up vote
      5
      down vote










      up vote
      5
      down vote









      This made more sense to me mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder



      mkdir makes the folder although i dont quite understand the -p switch. && lets you execute a second command. i used typical tar -switches but at the end -C is used to change directories and extract to that location needed.



      reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive






      share|improve this answer














      This made more sense to me mkdir -p /create/folder && tar -zxf haroopad-v0.13.0_x64.tar.gz -C /create/testfolder



      mkdir makes the folder although i dont quite understand the -p switch. && lets you execute a second command. i used typical tar -switches but at the end -C is used to change directories and extract to that location needed.



      reference: extract-files-contained-in-archive-tar-gz-to-new-directory-named-archive







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 23 '17 at 12:39









      Community♦

      1




      1










      answered Feb 1 '15 at 1:40









      Gr4cchus

      5113




      5113







      • 2




        The -p switch will not throw an error if the directory already exists.
        – David
        Sep 3 '15 at 21:10










      • @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
        – Antonio Pérez
        Oct 21 '15 at 13:40












      • 2




        The -p switch will not throw an error if the directory already exists.
        – David
        Sep 3 '15 at 21:10










      • @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
        – Antonio Pérez
        Oct 21 '15 at 13:40







      2




      2




      The -p switch will not throw an error if the directory already exists.
      – David
      Sep 3 '15 at 21:10




      The -p switch will not throw an error if the directory already exists.
      – David
      Sep 3 '15 at 21:10












      @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
      – Antonio Pérez
      Oct 21 '15 at 13:40




      @David it will also create any leading non-existing directories, i.e. it'll create /create if it doesn't exist.
      – Antonio Pérez
      Oct 21 '15 at 13:40










      up vote
      1
      down vote













      If you don't know the paths to create...

      Get the needed paths from the tar file, then create them...



      for P in `tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6`; do 
      C=`echo "$P: -1"` #get the last character
      if [ $C = '/' ]; then
      echo "Found directory: $P"
      mkdir -p $P
      fi
      done





      share|improve this answer


























        up vote
        1
        down vote













        If you don't know the paths to create...

        Get the needed paths from the tar file, then create them...



        for P in `tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6`; do 
        C=`echo "$P: -1"` #get the last character
        if [ $C = '/' ]; then
        echo "Found directory: $P"
        mkdir -p $P
        fi
        done





        share|improve this answer
























          up vote
          1
          down vote










          up vote
          1
          down vote









          If you don't know the paths to create...

          Get the needed paths from the tar file, then create them...



          for P in `tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6`; do 
          C=`echo "$P: -1"` #get the last character
          if [ $C = '/' ]; then
          echo "Found directory: $P"
          mkdir -p $P
          fi
          done





          share|improve this answer














          If you don't know the paths to create...

          Get the needed paths from the tar file, then create them...



          for P in `tar tvf tarfile.tar |tr -s [:space:] |cut -d' ' -f6`; do 
          C=`echo "$P: -1"` #get the last character
          if [ $C = '/' ]; then
          echo "Found directory: $P"
          mkdir -p $P
          fi
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 30 at 19:26









          Weijun Zhou

          1,444121




          1,444121










          answered Jan 30 at 18:34









          ckroger

          112




          112




















              up vote
              0
              down vote













              tar -xvf bash.html_node.tar.gz --one-top-level



              from man page of tar command



              --one-top-level[=DIR]
              Extract all files into DIR, or, if used without argument, into a
              subdirectory named by the base name of the archive (minus stan‐
              dard compression suffixes recognizable by --auto-compress).





              share








              New contributor




              Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                0
                down vote













                tar -xvf bash.html_node.tar.gz --one-top-level



                from man page of tar command



                --one-top-level[=DIR]
                Extract all files into DIR, or, if used without argument, into a
                subdirectory named by the base name of the archive (minus stan‐
                dard compression suffixes recognizable by --auto-compress).





                share








                New contributor




                Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  tar -xvf bash.html_node.tar.gz --one-top-level



                  from man page of tar command



                  --one-top-level[=DIR]
                  Extract all files into DIR, or, if used without argument, into a
                  subdirectory named by the base name of the archive (minus stan‐
                  dard compression suffixes recognizable by --auto-compress).





                  share








                  New contributor




                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  tar -xvf bash.html_node.tar.gz --one-top-level



                  from man page of tar command



                  --one-top-level[=DIR]
                  Extract all files into DIR, or, if used without argument, into a
                  subdirectory named by the base name of the archive (minus stan‐
                  dard compression suffixes recognizable by --auto-compress).






                  share








                  New contributor




                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 8 mins ago









                  Alaa Ahmad M. Zakaria

                  1




                  1




                  New contributor




                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Alaa Ahmad M. Zakaria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f25311%2fcreate-target-directory-when-extracting-tarball%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?