unpack 'tar' but change directory name to extract to












25















tar -tf filename.tar
folder1/file
folder1/name
[...]


I'd like to extract file and name to, folder2. Can this be done as a one-liner?










share|improve this question



























    25















    tar -tf filename.tar
    folder1/file
    folder1/name
    [...]


    I'd like to extract file and name to, folder2. Can this be done as a one-liner?










    share|improve this question

























      25












      25








      25


      9






      tar -tf filename.tar
      folder1/file
      folder1/name
      [...]


      I'd like to extract file and name to, folder2. Can this be done as a one-liner?










      share|improve this question














      tar -tf filename.tar
      folder1/file
      folder1/name
      [...]


      I'd like to extract file and name to, folder2. Can this be done as a one-liner?







      tar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 30 '10 at 0:32









      Felipe AlvarezFelipe Alvarez

      1,11531832




      1,11531832






















          2 Answers
          2






          active

          oldest

          votes


















          31














          Use -C and --strip-components (See man tar).



          Example:



          mkdir FOLDER
          # for remote tar file
          curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

          # for local tar file
          tar -xzf FILENAME -C FOLDER --strip-components=1


          Explanation:



          The -C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.



          The --strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so --strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.






          share|improve this answer


























          • I read man tar. Didn't spot --strip-components. nice one

            – Felipe Alvarez
            May 30 '10 at 6:25






          • 1





            tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

            – Mikhail Moskalev
            Jun 26 '11 at 7:42













          • I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

            – Iain
            Jul 22 '14 at 14:45











          • Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

            – John Carrell
            Dec 4 '18 at 20:14



















          16














          You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.



          For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:



          tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fsuperuser.com%2fquestions%2f146814%2funpack-tar-but-change-directory-name-to-extract-to%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









            31














            Use -C and --strip-components (See man tar).



            Example:



            mkdir FOLDER
            # for remote tar file
            curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

            # for local tar file
            tar -xzf FILENAME -C FOLDER --strip-components=1


            Explanation:



            The -C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.



            The --strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so --strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.






            share|improve this answer


























            • I read man tar. Didn't spot --strip-components. nice one

              – Felipe Alvarez
              May 30 '10 at 6:25






            • 1





              tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

              – Mikhail Moskalev
              Jun 26 '11 at 7:42













            • I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

              – Iain
              Jul 22 '14 at 14:45











            • Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

              – John Carrell
              Dec 4 '18 at 20:14
















            31














            Use -C and --strip-components (See man tar).



            Example:



            mkdir FOLDER
            # for remote tar file
            curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

            # for local tar file
            tar -xzf FILENAME -C FOLDER --strip-components=1


            Explanation:



            The -C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.



            The --strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so --strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.






            share|improve this answer


























            • I read man tar. Didn't spot --strip-components. nice one

              – Felipe Alvarez
              May 30 '10 at 6:25






            • 1





              tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

              – Mikhail Moskalev
              Jun 26 '11 at 7:42













            • I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

              – Iain
              Jul 22 '14 at 14:45











            • Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

              – John Carrell
              Dec 4 '18 at 20:14














            31












            31








            31







            Use -C and --strip-components (See man tar).



            Example:



            mkdir FOLDER
            # for remote tar file
            curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

            # for local tar file
            tar -xzf FILENAME -C FOLDER --strip-components=1


            Explanation:



            The -C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.



            The --strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so --strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.






            share|improve this answer















            Use -C and --strip-components (See man tar).



            Example:



            mkdir FOLDER
            # for remote tar file
            curl -L ’remote_tar_file' | tar -xz - -C FOLDER --strip-components=1

            # for local tar file
            tar -xzf FILENAME -C FOLDER --strip-components=1


            Explanation:



            The -C flag assumes a directory is already in place so the contents of the tar file can be expanded into it. hence the mkdir FOLDER.



            The --strip-components flag is used when a tar file would naturally expand itself into a folder, let say, like github where it examples to repo-name-master folder. Of course you wouldn’t need the first level folder generated here so --strip-components set to 1 would automatically remove that first folder for you. The larger the number is set the deeper nested folders are removed.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 26 at 14:59









            Felipe Alvarez

            1,11531832




            1,11531832










            answered May 30 '10 at 0:52









            MarianMarian

            81369




            81369













            • I read man tar. Didn't spot --strip-components. nice one

              – Felipe Alvarez
              May 30 '10 at 6:25






            • 1





              tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

              – Mikhail Moskalev
              Jun 26 '11 at 7:42













            • I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

              – Iain
              Jul 22 '14 at 14:45











            • Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

              – John Carrell
              Dec 4 '18 at 20:14



















            • I read man tar. Didn't spot --strip-components. nice one

              – Felipe Alvarez
              May 30 '10 at 6:25






            • 1





              tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

              – Mikhail Moskalev
              Jun 26 '11 at 7:42













            • I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

              – Iain
              Jul 22 '14 at 14:45











            • Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

              – John Carrell
              Dec 4 '18 at 20:14

















            I read man tar. Didn't spot --strip-components. nice one

            – Felipe Alvarez
            May 30 '10 at 6:25





            I read man tar. Didn't spot --strip-components. nice one

            – Felipe Alvarez
            May 30 '10 at 6:25




            1




            1





            tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

            – Mikhail Moskalev
            Jun 26 '11 at 7:42







            tar-1.14 uses --strip-path, tar-1.14.90+ uses --strip-components. Maybe problem here?

            – Mikhail Moskalev
            Jun 26 '11 at 7:42















            I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

            – Iain
            Jul 22 '14 at 14:45





            I'd upvote this if it gave an example and not just the switches, as the online manual states, 3 argument styles give rise to confusion.

            – Iain
            Jul 22 '14 at 14:45













            Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

            – John Carrell
            Dec 4 '18 at 20:14





            Forgot the -f on tar -xz, me thinks. Failed for me. I thought I'd been using that for no reason all this time...

            – John Carrell
            Dec 4 '18 at 20:14













            16














            You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.



            For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:



            tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'





            share|improve this answer




























              16














              You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.



              For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:



              tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'





              share|improve this answer


























                16












                16








                16







                You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.



                For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:



                tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'





                share|improve this answer













                You can also use the --transform option for a bit more flexibility. It accepts any sed replacement (s) operation.



                For example, this is how I extract a Linux tarball to a new directory so I can apply a patch:



                tar -xjf linux-2.6.38.tar.bz2 --transform 's/linux-2.6.38/linux-2.6.38.1/'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 24 '11 at 22:30









                TomTom

                16015




                16015






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f146814%2funpack-tar-but-change-directory-name-to-extract-to%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?

                    第一次世界大戦

                    Touch on Surface Book