How do I programatically get the file or directory name after extracting after a tar archive?

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








up vote
3
down vote

favorite












I have a process in my Dockerfile with the follow steps:



  1. Download the tar file using wget.

  2. untar the file.

  3. Perform some operation with the file . In this case, move content to another place.

The follow code works until ffmpeg version still 3.4.1.
So, today the available version for ffmpeg is 3.4.2 and the code is not working.



I want to find a way to grab back the filename after the untar process and pass it to the next steps



RUN wget http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz
RUN tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv ffmpeg-3.4.1-64bit-static/ffmpeg /usr/local/bin/
RUN mv ffmpeg-3.4.1-64bit-static/ffprobe /usr/local/bin/


It could be something like this:



RUN DIRNAME = tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv $DIRNAME/ffmpeg /usr/local/bin/
RUN mv $DIRNAME/ffprobe /usr/local/bin/


How could I achieve this?










share|improve this question























  • Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
    – steeldriver
    Mar 8 at 14:19










  • @steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
    – zwitterion
    Mar 8 at 15:28














up vote
3
down vote

favorite












I have a process in my Dockerfile with the follow steps:



  1. Download the tar file using wget.

  2. untar the file.

  3. Perform some operation with the file . In this case, move content to another place.

The follow code works until ffmpeg version still 3.4.1.
So, today the available version for ffmpeg is 3.4.2 and the code is not working.



I want to find a way to grab back the filename after the untar process and pass it to the next steps



RUN wget http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz
RUN tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv ffmpeg-3.4.1-64bit-static/ffmpeg /usr/local/bin/
RUN mv ffmpeg-3.4.1-64bit-static/ffprobe /usr/local/bin/


It could be something like this:



RUN DIRNAME = tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv $DIRNAME/ffmpeg /usr/local/bin/
RUN mv $DIRNAME/ffprobe /usr/local/bin/


How could I achieve this?










share|improve this question























  • Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
    – steeldriver
    Mar 8 at 14:19










  • @steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
    – zwitterion
    Mar 8 at 15:28












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a process in my Dockerfile with the follow steps:



  1. Download the tar file using wget.

  2. untar the file.

  3. Perform some operation with the file . In this case, move content to another place.

The follow code works until ffmpeg version still 3.4.1.
So, today the available version for ffmpeg is 3.4.2 and the code is not working.



I want to find a way to grab back the filename after the untar process and pass it to the next steps



RUN wget http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz
RUN tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv ffmpeg-3.4.1-64bit-static/ffmpeg /usr/local/bin/
RUN mv ffmpeg-3.4.1-64bit-static/ffprobe /usr/local/bin/


It could be something like this:



RUN DIRNAME = tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv $DIRNAME/ffmpeg /usr/local/bin/
RUN mv $DIRNAME/ffprobe /usr/local/bin/


How could I achieve this?










share|improve this question















I have a process in my Dockerfile with the follow steps:



  1. Download the tar file using wget.

  2. untar the file.

  3. Perform some operation with the file . In this case, move content to another place.

The follow code works until ffmpeg version still 3.4.1.
So, today the available version for ffmpeg is 3.4.2 and the code is not working.



I want to find a way to grab back the filename after the untar process and pass it to the next steps



RUN wget http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz
RUN tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv ffmpeg-3.4.1-64bit-static/ffmpeg /usr/local/bin/
RUN mv ffmpeg-3.4.1-64bit-static/ffprobe /usr/local/bin/


It could be something like this:



RUN DIRNAME = tar xf ffmpeg-release-64bit-static.tar.xz
RUN mv $DIRNAME/ffmpeg /usr/local/bin/
RUN mv $DIRNAME/ffprobe /usr/local/bin/


How could I achieve this?







command-line tar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 14:41









muru

130k19274467




130k19274467










asked Mar 8 at 13:39









zwitterion

3191415




3191415











  • Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
    – steeldriver
    Mar 8 at 14:19










  • @steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
    – zwitterion
    Mar 8 at 15:28
















  • Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
    – steeldriver
    Mar 8 at 14:19










  • @steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
    – zwitterion
    Mar 8 at 15:28















Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
– steeldriver
Mar 8 at 14:19




Are you retaining multiple versions of the extracted tarball in the directory? if not, you may be able to use a simple filename glob e.g. mv ffmpeg-*-static/ffmpeg /usr/local/bin/
– steeldriver
Mar 8 at 14:19












@steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
– zwitterion
Mar 8 at 15:28




@steeldriver this is an excelent solution to my process. And it works. But I will accept the answer related to the question. Thanks to show me another way to do.
– zwitterion
Mar 8 at 15:28










3 Answers
3






active

oldest

votes

















up vote
3
down vote













Don't. Instead, get tar to put the contents in a directory of your choosing.




  1. tar can remove n components of the path of each file (--strip-components)


  2. tar can apply arbitrary sed expressions to change the path of each file (--transform)

So I'd do something like:



mkdir ffmpeg
wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
tar --xz -x -C ffmpeg --strip-components=1


Here, I made the directory myself, and told tar to extract there (the -C option), while removing the first component from the path of the extracted files. So ffmpeg-3.4.1-64bit-static/ffprobe becomes ffprobe inside the directory ffmpeg.



Or:



wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
tar --xz -x --transform='s:[^/]*/:ffmpeg/'


Here, I simply changed the first component of the path to ffmpeg, so I didn't need to make the directory myself. So, ffmpeg-3.4.1-64bit-static/ffmpeg becomes ffmpeg/ffmpeg.



Or, knowing that you specifically want to dump those particular files in /usr/local/bin, use -C to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:



wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
tar Jx -C /usr/local/bin --transform='s:.*/::' --wildcards '*/ffmpeg' '*/ffprobe'


With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.






share|improve this answer



























    up vote
    0
    down vote













    The command to get the directory name from the tar.xz file is:



    tar tJvf tar-file-name.tar.xz | head -1 | awk 'print $6'


    You'll need to:



    1. Get the directory name (e.g. using above solution) and save it in a variable

    2. decompress the tar.xz file (e.g. using tar xf command)

    3. move the files using the directory variable


    Explanation how to get the directory name from a tar file:



    Tar parameters:



    -f file
    -J xz compression
    -t list content
    -v verbose


    head -1 - prints the file line



    awk 'print $6' - print the 6th elements which is the directory name




    Examples:




    1. tar.xz example:



      tar tJvf coreutils-8.22.tar.xz | head -1 | awk 'print $6'



    Results with:



    coreutils-8.22/



    1. tar.bz2 example for ffmpeg-3.4.2.tar.bz2



      tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk 'print $6'



    Results with:



    ffmpeg-3.4.2/



    Note that for bz2 compressed files I used the tar j flag







    share|improve this answer





























      up vote
      0
      down vote













      Like that:



      $ tar tf ffmpeg-release-64bit-static.tar.xz | head -1 | cut -d '/' -f1
      ffmpeg-3.4.2-64bit-static


      Notice that GNU tar does not require -j, -z etc., it can detect archive type automatically.






      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "89"
        ;
        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: true,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2faskubuntu.com%2fquestions%2f1013081%2fhow-do-i-programatically-get-the-file-or-directory-name-after-extracting-after-a%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote













        Don't. Instead, get tar to put the contents in a directory of your choosing.




        1. tar can remove n components of the path of each file (--strip-components)


        2. tar can apply arbitrary sed expressions to change the path of each file (--transform)

        So I'd do something like:



        mkdir ffmpeg
        wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
        tar --xz -x -C ffmpeg --strip-components=1


        Here, I made the directory myself, and told tar to extract there (the -C option), while removing the first component from the path of the extracted files. So ffmpeg-3.4.1-64bit-static/ffprobe becomes ffprobe inside the directory ffmpeg.



        Or:



        wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
        tar --xz -x --transform='s:[^/]*/:ffmpeg/'


        Here, I simply changed the first component of the path to ffmpeg, so I didn't need to make the directory myself. So, ffmpeg-3.4.1-64bit-static/ffmpeg becomes ffmpeg/ffmpeg.



        Or, knowing that you specifically want to dump those particular files in /usr/local/bin, use -C to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:



        wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
        tar Jx -C /usr/local/bin --transform='s:.*/::' --wildcards '*/ffmpeg' '*/ffprobe'


        With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.






        share|improve this answer
























          up vote
          3
          down vote













          Don't. Instead, get tar to put the contents in a directory of your choosing.




          1. tar can remove n components of the path of each file (--strip-components)


          2. tar can apply arbitrary sed expressions to change the path of each file (--transform)

          So I'd do something like:



          mkdir ffmpeg
          wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
          tar --xz -x -C ffmpeg --strip-components=1


          Here, I made the directory myself, and told tar to extract there (the -C option), while removing the first component from the path of the extracted files. So ffmpeg-3.4.1-64bit-static/ffprobe becomes ffprobe inside the directory ffmpeg.



          Or:



          wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
          tar --xz -x --transform='s:[^/]*/:ffmpeg/'


          Here, I simply changed the first component of the path to ffmpeg, so I didn't need to make the directory myself. So, ffmpeg-3.4.1-64bit-static/ffmpeg becomes ffmpeg/ffmpeg.



          Or, knowing that you specifically want to dump those particular files in /usr/local/bin, use -C to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:



          wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
          tar Jx -C /usr/local/bin --transform='s:.*/::' --wildcards '*/ffmpeg' '*/ffprobe'


          With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.






          share|improve this answer






















            up vote
            3
            down vote










            up vote
            3
            down vote









            Don't. Instead, get tar to put the contents in a directory of your choosing.




            1. tar can remove n components of the path of each file (--strip-components)


            2. tar can apply arbitrary sed expressions to change the path of each file (--transform)

            So I'd do something like:



            mkdir ffmpeg
            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar --xz -x -C ffmpeg --strip-components=1


            Here, I made the directory myself, and told tar to extract there (the -C option), while removing the first component from the path of the extracted files. So ffmpeg-3.4.1-64bit-static/ffprobe becomes ffprobe inside the directory ffmpeg.



            Or:



            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar --xz -x --transform='s:[^/]*/:ffmpeg/'


            Here, I simply changed the first component of the path to ffmpeg, so I didn't need to make the directory myself. So, ffmpeg-3.4.1-64bit-static/ffmpeg becomes ffmpeg/ffmpeg.



            Or, knowing that you specifically want to dump those particular files in /usr/local/bin, use -C to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:



            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar Jx -C /usr/local/bin --transform='s:.*/::' --wildcards '*/ffmpeg' '*/ffprobe'


            With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.






            share|improve this answer












            Don't. Instead, get tar to put the contents in a directory of your choosing.




            1. tar can remove n components of the path of each file (--strip-components)


            2. tar can apply arbitrary sed expressions to change the path of each file (--transform)

            So I'd do something like:



            mkdir ffmpeg
            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar --xz -x -C ffmpeg --strip-components=1


            Here, I made the directory myself, and told tar to extract there (the -C option), while removing the first component from the path of the extracted files. So ffmpeg-3.4.1-64bit-static/ffprobe becomes ffprobe inside the directory ffmpeg.



            Or:



            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar --xz -x --transform='s:[^/]*/:ffmpeg/'


            Here, I simply changed the first component of the path to ffmpeg, so I didn't need to make the directory myself. So, ffmpeg-3.4.1-64bit-static/ffmpeg becomes ffmpeg/ffmpeg.



            Or, knowing that you specifically want to dump those particular files in /usr/local/bin, use -C to directly place the files there, while removing all directory components and selecting exactly those files you want to extract:



            wget -O - http://johnvansickle.com/ffmpeg/releases/ffmpeg-release-64bit-static.tar.xz |
            tar Jx -C /usr/local/bin --transform='s:.*/::' --wildcards '*/ffmpeg' '*/ffprobe'


            With any tar, you can specify the files to be extracted. With GNU tar, you can also use wildcards.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 8 at 14:36









            muru

            130k19274467




            130k19274467






















                up vote
                0
                down vote













                The command to get the directory name from the tar.xz file is:



                tar tJvf tar-file-name.tar.xz | head -1 | awk 'print $6'


                You'll need to:



                1. Get the directory name (e.g. using above solution) and save it in a variable

                2. decompress the tar.xz file (e.g. using tar xf command)

                3. move the files using the directory variable


                Explanation how to get the directory name from a tar file:



                Tar parameters:



                -f file
                -J xz compression
                -t list content
                -v verbose


                head -1 - prints the file line



                awk 'print $6' - print the 6th elements which is the directory name




                Examples:




                1. tar.xz example:



                  tar tJvf coreutils-8.22.tar.xz | head -1 | awk 'print $6'



                Results with:



                coreutils-8.22/



                1. tar.bz2 example for ffmpeg-3.4.2.tar.bz2



                  tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk 'print $6'



                Results with:



                ffmpeg-3.4.2/



                Note that for bz2 compressed files I used the tar j flag







                share|improve this answer


























                  up vote
                  0
                  down vote













                  The command to get the directory name from the tar.xz file is:



                  tar tJvf tar-file-name.tar.xz | head -1 | awk 'print $6'


                  You'll need to:



                  1. Get the directory name (e.g. using above solution) and save it in a variable

                  2. decompress the tar.xz file (e.g. using tar xf command)

                  3. move the files using the directory variable


                  Explanation how to get the directory name from a tar file:



                  Tar parameters:



                  -f file
                  -J xz compression
                  -t list content
                  -v verbose


                  head -1 - prints the file line



                  awk 'print $6' - print the 6th elements which is the directory name




                  Examples:




                  1. tar.xz example:



                    tar tJvf coreutils-8.22.tar.xz | head -1 | awk 'print $6'



                  Results with:



                  coreutils-8.22/



                  1. tar.bz2 example for ffmpeg-3.4.2.tar.bz2



                    tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk 'print $6'



                  Results with:



                  ffmpeg-3.4.2/



                  Note that for bz2 compressed files I used the tar j flag







                  share|improve this answer
























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    The command to get the directory name from the tar.xz file is:



                    tar tJvf tar-file-name.tar.xz | head -1 | awk 'print $6'


                    You'll need to:



                    1. Get the directory name (e.g. using above solution) and save it in a variable

                    2. decompress the tar.xz file (e.g. using tar xf command)

                    3. move the files using the directory variable


                    Explanation how to get the directory name from a tar file:



                    Tar parameters:



                    -f file
                    -J xz compression
                    -t list content
                    -v verbose


                    head -1 - prints the file line



                    awk 'print $6' - print the 6th elements which is the directory name




                    Examples:




                    1. tar.xz example:



                      tar tJvf coreutils-8.22.tar.xz | head -1 | awk 'print $6'



                    Results with:



                    coreutils-8.22/



                    1. tar.bz2 example for ffmpeg-3.4.2.tar.bz2



                      tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk 'print $6'



                    Results with:



                    ffmpeg-3.4.2/



                    Note that for bz2 compressed files I used the tar j flag







                    share|improve this answer














                    The command to get the directory name from the tar.xz file is:



                    tar tJvf tar-file-name.tar.xz | head -1 | awk 'print $6'


                    You'll need to:



                    1. Get the directory name (e.g. using above solution) and save it in a variable

                    2. decompress the tar.xz file (e.g. using tar xf command)

                    3. move the files using the directory variable


                    Explanation how to get the directory name from a tar file:



                    Tar parameters:



                    -f file
                    -J xz compression
                    -t list content
                    -v verbose


                    head -1 - prints the file line



                    awk 'print $6' - print the 6th elements which is the directory name




                    Examples:




                    1. tar.xz example:



                      tar tJvf coreutils-8.22.tar.xz | head -1 | awk 'print $6'



                    Results with:



                    coreutils-8.22/



                    1. tar.bz2 example for ffmpeg-3.4.2.tar.bz2



                      tar tjvf ffmpeg-3.4.2.tar.bz2 | head -1 | awk 'print $6'



                    Results with:



                    ffmpeg-3.4.2/



                    Note that for bz2 compressed files I used the tar j flag








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 8 at 13:56

























                    answered Mar 8 at 13:48









                    Yaron

                    8,52271838




                    8,52271838




















                        up vote
                        0
                        down vote













                        Like that:



                        $ tar tf ffmpeg-release-64bit-static.tar.xz | head -1 | cut -d '/' -f1
                        ffmpeg-3.4.2-64bit-static


                        Notice that GNU tar does not require -j, -z etc., it can detect archive type automatically.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Like that:



                          $ tar tf ffmpeg-release-64bit-static.tar.xz | head -1 | cut -d '/' -f1
                          ffmpeg-3.4.2-64bit-static


                          Notice that GNU tar does not require -j, -z etc., it can detect archive type automatically.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Like that:



                            $ tar tf ffmpeg-release-64bit-static.tar.xz | head -1 | cut -d '/' -f1
                            ffmpeg-3.4.2-64bit-static


                            Notice that GNU tar does not require -j, -z etc., it can detect archive type automatically.






                            share|improve this answer












                            Like that:



                            $ tar tf ffmpeg-release-64bit-static.tar.xz | head -1 | cut -d '/' -f1
                            ffmpeg-3.4.2-64bit-static


                            Notice that GNU tar does not require -j, -z etc., it can detect archive type automatically.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 8 at 13:59









                            Arkadiusz Drabczyk

                            96949




                            96949



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1013081%2fhow-do-i-programatically-get-the-file-or-directory-name-after-extracting-after-a%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                pylint3 and pip3 broken

                                Missing snmpget and snmpwalk

                                How to enroll fingerprints to Ubuntu 17.10 with VFS491