Portable way to invoke tar with a list of files from stdin












5















I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content.

I had done this as follows:



cd /path/to/files/ && find . -type f | cut -c 3- | xargs czf /path/to/tgz/myTgz.tgz


I learned recently that using xargs may not be the best way to pull this off because xargs may invoke tar multiple times if the cmdline arg list gets too long, and I was advised to make use of tar's ability to read a list of input files from stdin. I ended up finding this article on how to do this. However, I find that the recommendation...



cd /path/to/files/ && find . -type f | cut -c 3- | tar czf foo.tgz -T -


...seems to not be portable. It runs fine on my dev PC, but on a busybox target, I get the following error from running the same command:



tar: can't open '-': No such file or directory


So, my question: is there a truly portable/global way to invoke tar to create a .tgz by feeding it input files from stdin (as opposed to cmdline arguments)?



(It is not an option available to me to install alternatives to tar such as gnutar/bsdtar/etc.)



(Secondary question: Why does the "-T -" argument to tar denote "read files from stdin"? From the tar man page, all I could find was that "-T" means:




get names to extract or create from FILE




... but I couldn't see any reference to a plain "-")










share|improve this question




















  • 3





    See Usage of dash (-) in place of a filename.

    – Michael Homer
    Jan 3 at 19:03






  • 2





    Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

    – Kusalananda
    Jan 3 at 19:31











  • This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

    – JdeBP
    Jan 3 at 19:40











  • @JdeBP - removed tertiary question

    – StoneThrow
    Jan 3 at 19:43











  • @Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

    – StoneThrow
    Jan 3 at 19:47
















5















I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content.

I had done this as follows:



cd /path/to/files/ && find . -type f | cut -c 3- | xargs czf /path/to/tgz/myTgz.tgz


I learned recently that using xargs may not be the best way to pull this off because xargs may invoke tar multiple times if the cmdline arg list gets too long, and I was advised to make use of tar's ability to read a list of input files from stdin. I ended up finding this article on how to do this. However, I find that the recommendation...



cd /path/to/files/ && find . -type f | cut -c 3- | tar czf foo.tgz -T -


...seems to not be portable. It runs fine on my dev PC, but on a busybox target, I get the following error from running the same command:



tar: can't open '-': No such file or directory


So, my question: is there a truly portable/global way to invoke tar to create a .tgz by feeding it input files from stdin (as opposed to cmdline arguments)?



(It is not an option available to me to install alternatives to tar such as gnutar/bsdtar/etc.)



(Secondary question: Why does the "-T -" argument to tar denote "read files from stdin"? From the tar man page, all I could find was that "-T" means:




get names to extract or create from FILE




... but I couldn't see any reference to a plain "-")










share|improve this question




















  • 3





    See Usage of dash (-) in place of a filename.

    – Michael Homer
    Jan 3 at 19:03






  • 2





    Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

    – Kusalananda
    Jan 3 at 19:31











  • This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

    – JdeBP
    Jan 3 at 19:40











  • @JdeBP - removed tertiary question

    – StoneThrow
    Jan 3 at 19:43











  • @Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

    – StoneThrow
    Jan 3 at 19:47














5












5








5








I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content.

I had done this as follows:



cd /path/to/files/ && find . -type f | cut -c 3- | xargs czf /path/to/tgz/myTgz.tgz


I learned recently that using xargs may not be the best way to pull this off because xargs may invoke tar multiple times if the cmdline arg list gets too long, and I was advised to make use of tar's ability to read a list of input files from stdin. I ended up finding this article on how to do this. However, I find that the recommendation...



cd /path/to/files/ && find . -type f | cut -c 3- | tar czf foo.tgz -T -


...seems to not be portable. It runs fine on my dev PC, but on a busybox target, I get the following error from running the same command:



tar: can't open '-': No such file or directory


So, my question: is there a truly portable/global way to invoke tar to create a .tgz by feeding it input files from stdin (as opposed to cmdline arguments)?



(It is not an option available to me to install alternatives to tar such as gnutar/bsdtar/etc.)



(Secondary question: Why does the "-T -" argument to tar denote "read files from stdin"? From the tar man page, all I could find was that "-T" means:




get names to extract or create from FILE




... but I couldn't see any reference to a plain "-")










share|improve this question
















I want to create a .tgz from the content of a directory. I also want to strip the leading "./" from the tar'ed content.

I had done this as follows:



cd /path/to/files/ && find . -type f | cut -c 3- | xargs czf /path/to/tgz/myTgz.tgz


I learned recently that using xargs may not be the best way to pull this off because xargs may invoke tar multiple times if the cmdline arg list gets too long, and I was advised to make use of tar's ability to read a list of input files from stdin. I ended up finding this article on how to do this. However, I find that the recommendation...



cd /path/to/files/ && find . -type f | cut -c 3- | tar czf foo.tgz -T -


...seems to not be portable. It runs fine on my dev PC, but on a busybox target, I get the following error from running the same command:



tar: can't open '-': No such file or directory


So, my question: is there a truly portable/global way to invoke tar to create a .tgz by feeding it input files from stdin (as opposed to cmdline arguments)?



(It is not an option available to me to install alternatives to tar such as gnutar/bsdtar/etc.)



(Secondary question: Why does the "-T -" argument to tar denote "read files from stdin"? From the tar man page, all I could find was that "-T" means:




get names to extract or create from FILE




... but I couldn't see any reference to a plain "-")







io-redirection tar busybox stdin options






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 19:49









Gilles

542k12810961615




542k12810961615










asked Jan 3 at 18:57









StoneThrowStoneThrow

469515




469515








  • 3





    See Usage of dash (-) in place of a filename.

    – Michael Homer
    Jan 3 at 19:03






  • 2





    Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

    – Kusalananda
    Jan 3 at 19:31











  • This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

    – JdeBP
    Jan 3 at 19:40











  • @JdeBP - removed tertiary question

    – StoneThrow
    Jan 3 at 19:43











  • @Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

    – StoneThrow
    Jan 3 at 19:47














  • 3





    See Usage of dash (-) in place of a filename.

    – Michael Homer
    Jan 3 at 19:03






  • 2





    Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

    – Kusalananda
    Jan 3 at 19:31











  • This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

    – JdeBP
    Jan 3 at 19:40











  • @JdeBP - removed tertiary question

    – StoneThrow
    Jan 3 at 19:43











  • @Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

    – StoneThrow
    Jan 3 at 19:47








3




3





See Usage of dash (-) in place of a filename.

– Michael Homer
Jan 3 at 19:03





See Usage of dash (-) in place of a filename.

– Michael Homer
Jan 3 at 19:03




2




2





Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

– Kusalananda
Jan 3 at 19:31





Since tar is not a POSIX utility, a truly portable (as in "standard") implementation may not be found. pax, on the other hand, is a POSIX utility.

– Kusalananda
Jan 3 at 19:31













This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

– JdeBP
Jan 3 at 19:40





This site does one question per question, and that tertiary question is a massive distraction that is a question in its own right, most likely already long since covered.

– JdeBP
Jan 3 at 19:40













@JdeBP - removed tertiary question

– StoneThrow
Jan 3 at 19:43





@JdeBP - removed tertiary question

– StoneThrow
Jan 3 at 19:43













@Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

– StoneThrow
Jan 3 at 19:47





@Kusalananda - that's an informative comment; thank you. However, it looks like pax isn't available in Busybox (at least in the version of Busybox I have to work with). I think I may have to resign myself to using the sub-optimal approach using xargs.

– StoneThrow
Jan 3 at 19:47










3 Answers
3






active

oldest

votes


















6














It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).



BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.



BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.



I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.



It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.






share|improve this answer































    4














    I know that is a stupid answer and maybe not good for you but how bad is if you just...



    cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
    tar czf foo.tgz -T /tmp/temp.txt





    share|improve this answer
























    • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

      – StoneThrow
      Jan 3 at 20:13











    • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

      – Luciano Andress Martini
      Jan 4 at 10:34



















    3














    You can use the -u/--update option in conjunction with xargs.
    I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:



    find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar



    Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:



    find top_of_tree/ -type f | xargs tar -uf archive.tar



    The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.



    EDIT: the -u/--update flags allow tar to create as well as append to an archive.



    I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.



    Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.



    As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files.
    - is usually a special argument for filters and names the standard input stream as a file.






    share|improve this answer


























    • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

      – StoneThrow
      Jan 3 at 20:15











    • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

      – Larry
      Jan 3 at 20:23











    • -u wouldn't work with a gzipped archive anyway.

      – Gilles
      Jan 3 at 20:28











    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%2f492308%2fportable-way-to-invoke-tar-with-a-list-of-files-from-stdin%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).



    BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.



    BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.



    I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.



    It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.






    share|improve this answer




























      6














      It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).



      BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.



      BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.



      I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.



      It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.






      share|improve this answer


























        6












        6








        6







        It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).



        BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.



        BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.



        I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.



        It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.






        share|improve this answer













        It is a common convention to interpret - to mean standard input where an input file name is expected, and to mean standard output where an output file name is expected. Because this is a common convention, the short help summary in the GNU tar man page does not mention it, but the complete manual (usually available locally through info tar) does. The POSIX command line utility syntax guidelines includes this convention, so it's pretty widespread (but it's always a choice on the part of the author of the program).



        BusyBox utilities do follow this convention. But the manual does not mention tar as supporting the option -T, and neither does the version on the machine I'm posting this (1.27.2 on Ubuntu). I don't know why you're getting the error “: No such file or directory” rather than “invalid option -- 'T'”. It seems that your tar interprets -T as an option that does not take an argument, then sees - as a file name. Since in this context tar needs a file name to put in the archive, and not just some content that comes from a file, it would not make sense to use the stdin/stdout interpretation for -.



        BusyBox utilities support a restricted set of functionality by design, because they're intended for embedded systems where the fancier features of GNU utilities wouldn't fit. Apparently -T is not a feature that the BusyBox designers considered useful.



        I don't think BusyBox tar has any way to read file names from stdin. If you need to archive a subset of the files in a directory and you don't need any symbolic links in the archive, a workaround is to create a forest of symbolic links in a temporary directory and archive this temporary directory.



        It's not clear exactly why you're using find. If you only want the files in the current directory, why not tar czf /path/to/archive.tgz -- * ? Your command does make sense if there are subdirectories and you want to archive the files in these subdirectories, but not the directories themselves (presumably to restore them in a place where the directory structure must exist but may have different permissions). In this case a leading ./ wouldn't do any harm.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 20:26









        GillesGilles

        542k12810961615




        542k12810961615

























            4














            I know that is a stupid answer and maybe not good for you but how bad is if you just...



            cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
            tar czf foo.tgz -T /tmp/temp.txt





            share|improve this answer
























            • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

              – StoneThrow
              Jan 3 at 20:13











            • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

              – Luciano Andress Martini
              Jan 4 at 10:34
















            4














            I know that is a stupid answer and maybe not good for you but how bad is if you just...



            cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
            tar czf foo.tgz -T /tmp/temp.txt





            share|improve this answer
























            • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

              – StoneThrow
              Jan 3 at 20:13











            • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

              – Luciano Andress Martini
              Jan 4 at 10:34














            4












            4








            4







            I know that is a stupid answer and maybe not good for you but how bad is if you just...



            cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
            tar czf foo.tgz -T /tmp/temp.txt





            share|improve this answer













            I know that is a stupid answer and maybe not good for you but how bad is if you just...



            cd /path/to/files/ && find . -type f | cut -c 3- >/tmp/temp.txt 
            tar czf foo.tgz -T /tmp/temp.txt






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 19:56









            Luciano Andress MartiniLuciano Andress Martini

            3,9531036




            3,9531036













            • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

              – StoneThrow
              Jan 3 at 20:13











            • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

              – Luciano Andress Martini
              Jan 4 at 10:34



















            • Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

              – StoneThrow
              Jan 3 at 20:13











            • The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

              – Luciano Andress Martini
              Jan 4 at 10:34

















            Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

            – StoneThrow
            Jan 3 at 20:13





            Not at all a stupid answer. The original question (and the reason why I asked if this was possible to do this from stdin) was because of the problem I noted RE: xargs calling tar multiple times if the input list is too long...does this solution also bypass that problem? (it seems like it does because it's also using "-T", not explicitly putting the source files as command-line arguments)

            – StoneThrow
            Jan 3 at 20:13













            The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

            – Luciano Andress Martini
            Jan 4 at 10:34





            The one problem with this script that I see is if you run two of them in the same time, in this case you will need to use something to generate random names for temp.txt....

            – Luciano Andress Martini
            Jan 4 at 10:34











            3














            You can use the -u/--update option in conjunction with xargs.
            I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:



            find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar



            Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:



            find top_of_tree/ -type f | xargs tar -uf archive.tar



            The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.



            EDIT: the -u/--update flags allow tar to create as well as append to an archive.



            I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.



            Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.



            As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files.
            - is usually a special argument for filters and names the standard input stream as a file.






            share|improve this answer


























            • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

              – StoneThrow
              Jan 3 at 20:15











            • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

              – Larry
              Jan 3 at 20:23











            • -u wouldn't work with a gzipped archive anyway.

              – Gilles
              Jan 3 at 20:28
















            3














            You can use the -u/--update option in conjunction with xargs.
            I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:



            find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar



            Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:



            find top_of_tree/ -type f | xargs tar -uf archive.tar



            The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.



            EDIT: the -u/--update flags allow tar to create as well as append to an archive.



            I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.



            Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.



            As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files.
            - is usually a special argument for filters and names the standard input stream as a file.






            share|improve this answer


























            • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

              – StoneThrow
              Jan 3 at 20:15











            • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

              – Larry
              Jan 3 at 20:23











            • -u wouldn't work with a gzipped archive anyway.

              – Gilles
              Jan 3 at 20:28














            3












            3








            3







            You can use the -u/--update option in conjunction with xargs.
            I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:



            find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar



            Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:



            find top_of_tree/ -type f | xargs tar -uf archive.tar



            The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.



            EDIT: the -u/--update flags allow tar to create as well as append to an archive.



            I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.



            Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.



            As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files.
            - is usually a special argument for filters and names the standard input stream as a file.






            share|improve this answer















            You can use the -u/--update option in conjunction with xargs.
            I just tried and to prove that it works, I gave the filename arguments to tar one-by-one using xargs -n 1:



            find top_of_tree/ -type f | xargs -n 1 tar -uf archive.tar



            Of course, this is just a proof of concept. In your case, it is probably more practical to allow xargs to separate the argument list into larger parts that do not exceed the argument length limit, so as to avoid having a lot of calls to tar:



            find top_of_tree/ -type f | xargs tar -uf archive.tar



            The prerequisite is that all implementations of tar on your platforms support at least one of the -u/--update flags.



            EDIT: the -u/--update flags allow tar to create as well as append to an archive.



            I just noticed that unfortunately, you cannot update compressed archives (tgz, created with the -c flag). However, you can, by all means create a non-compressed archive first and then compress the results.



            Also, it should not confuse you that I'm using flags to tar with a hyphen ('-' character). It accepts both forms. In my example, -uf would be equivalent to uf in yours.



            As for the - option, it is probalby not documented in the man page of tar because it is a very common Unix convention for command line tools that deal with streams as well as named files.
            - is usually a special argument for filters and names the standard input stream as a file.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 3 at 20:59

























            answered Jan 3 at 20:02









            LarryLarry

            1265




            1265













            • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

              – StoneThrow
              Jan 3 at 20:15











            • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

              – Larry
              Jan 3 at 20:23











            • -u wouldn't work with a gzipped archive anyway.

              – Gilles
              Jan 3 at 20:28



















            • Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

              – StoneThrow
              Jan 3 at 20:15











            • I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

              – Larry
              Jan 3 at 20:23











            • -u wouldn't work with a gzipped archive anyway.

              – Gilles
              Jan 3 at 20:28

















            Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

            – StoneThrow
            Jan 3 at 20:15





            Good idea, thank you, but unfortunately, looks like Busybox version of tar does not support -u/-update.

            – StoneThrow
            Jan 3 at 20:15













            I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

            – Larry
            Jan 3 at 20:23





            I worked a lot on embedded machines with BusyBox as well. Unfortunately, my only solution was to cross-compile 7Zip for that system.

            – Larry
            Jan 3 at 20:23













            -u wouldn't work with a gzipped archive anyway.

            – Gilles
            Jan 3 at 20:28





            -u wouldn't work with a gzipped archive anyway.

            – Gilles
            Jan 3 at 20:28


















            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%2f492308%2fportable-way-to-invoke-tar-with-a-list-of-files-from-stdin%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

            Bressuire

            Cabo Verde

            Gyllenstierna