tar files only, no directories

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








16















I can probably write a shell script to find files only, then pass the list to tar, but I am wondering whether there already is a built-in feature in tar that allows doing just that, in a single command line?



For example, I found the --no-recursion switch, but when I do:



tar --no-recursion -cvf mydir.tar mydir


It only archives the names of the entries in the directory (including subdirectories!), but it doesn't archive any files.



I also tried:



 tar --no-recursion -cvf mydir.tar mydir/*


But while it archives files only, it also archives the names of the subdirectories.



Is there a way to tell tar files only, no directories?










share|improve this question



















  • 5





    Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

    – rozcietrzewiacz
    Nov 18 '11 at 14:47






  • 1





    You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

    – Kevin
    Nov 18 '11 at 15:05











  • @rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

    – ateiob
    Nov 18 '11 at 15:17






  • 1





    OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

    – Kevin
    Nov 18 '11 at 15:30






  • 2





    Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

    – Kevin
    Nov 18 '11 at 15:49

















16















I can probably write a shell script to find files only, then pass the list to tar, but I am wondering whether there already is a built-in feature in tar that allows doing just that, in a single command line?



For example, I found the --no-recursion switch, but when I do:



tar --no-recursion -cvf mydir.tar mydir


It only archives the names of the entries in the directory (including subdirectories!), but it doesn't archive any files.



I also tried:



 tar --no-recursion -cvf mydir.tar mydir/*


But while it archives files only, it also archives the names of the subdirectories.



Is there a way to tell tar files only, no directories?










share|improve this question



















  • 5





    Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

    – rozcietrzewiacz
    Nov 18 '11 at 14:47






  • 1





    You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

    – Kevin
    Nov 18 '11 at 15:05











  • @rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

    – ateiob
    Nov 18 '11 at 15:17






  • 1





    OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

    – Kevin
    Nov 18 '11 at 15:30






  • 2





    Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

    – Kevin
    Nov 18 '11 at 15:49













16












16








16








I can probably write a shell script to find files only, then pass the list to tar, but I am wondering whether there already is a built-in feature in tar that allows doing just that, in a single command line?



For example, I found the --no-recursion switch, but when I do:



tar --no-recursion -cvf mydir.tar mydir


It only archives the names of the entries in the directory (including subdirectories!), but it doesn't archive any files.



I also tried:



 tar --no-recursion -cvf mydir.tar mydir/*


But while it archives files only, it also archives the names of the subdirectories.



Is there a way to tell tar files only, no directories?










share|improve this question
















I can probably write a shell script to find files only, then pass the list to tar, but I am wondering whether there already is a built-in feature in tar that allows doing just that, in a single command line?



For example, I found the --no-recursion switch, but when I do:



tar --no-recursion -cvf mydir.tar mydir


It only archives the names of the entries in the directory (including subdirectories!), but it doesn't archive any files.



I also tried:



 tar --no-recursion -cvf mydir.tar mydir/*


But while it archives files only, it also archives the names of the subdirectories.



Is there a way to tell tar files only, no directories?







tar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 6 '12 at 18:29









Stéphane Gimenez

19.8k25275




19.8k25275










asked Nov 18 '11 at 14:39









ateiobateiob

5973612




5973612







  • 5





    Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

    – rozcietrzewiacz
    Nov 18 '11 at 14:47






  • 1





    You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

    – Kevin
    Nov 18 '11 at 15:05











  • @rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

    – ateiob
    Nov 18 '11 at 15:17






  • 1





    OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

    – Kevin
    Nov 18 '11 at 15:30






  • 2





    Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

    – Kevin
    Nov 18 '11 at 15:49












  • 5





    Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

    – rozcietrzewiacz
    Nov 18 '11 at 14:47






  • 1





    You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

    – Kevin
    Nov 18 '11 at 15:05











  • @rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

    – ateiob
    Nov 18 '11 at 15:17






  • 1





    OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

    – Kevin
    Nov 18 '11 at 15:30






  • 2





    Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

    – Kevin
    Nov 18 '11 at 15:49







5




5





Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

– rozcietrzewiacz
Nov 18 '11 at 14:47





Just to clarify: do you want to create an archive with "flat" structure (i.e. all files mixed up in one directory)?

– rozcietrzewiacz
Nov 18 '11 at 14:47




1




1





You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

– Kevin
Nov 18 '11 at 15:05





You could create a new directory and find mydir -type f |xargs cp -t tempdir and then tar tempdir.

– Kevin
Nov 18 '11 at 15:05













@rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

– ateiob
Nov 18 '11 at 15:17





@rozcietrzewiacz Yes, flat, but only from that directory, not from subdirectories.

– ateiob
Nov 18 '11 at 15:17




1




1





OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

– Kevin
Nov 18 '11 at 15:30





OK, I think I see what you're trying to do. How about find mydir -depth 1 -type f | xargs tar cf mydir.tar

– Kevin
Nov 18 '11 at 15:30




2




2





Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

– Kevin
Nov 18 '11 at 15:49





Ah, spaces. Use find's -exec instead: find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +. The + puts all the files on the same command line like xargs.

– Kevin
Nov 18 '11 at 15:49










8 Answers
8






active

oldest

votes


















11














As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:



find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar +


Iocnarz's answer of using tar's --null and -T options works as well. If you have cpio installed, camh's answer using it is also fine. And if you have zsh and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)) seems the most straightforward.




The key was the -maxdepth option.
Final answer, dealing with spaces appropriately:



find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar


This should also work:



find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +





share|improve this answer




















  • 3





    Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

    – camh
    Apr 6 '12 at 22:30






  • 2





    Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

    – camh
    Apr 6 '12 at 22:49











  • @camh You're right, thanks for pointing that out; I've updated to reflect that.

    – Kevin
    Apr 7 '12 at 3:22






  • 1





    The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

    – eckes
    May 31 '13 at 17:27


















13














When you want to use find with tar, the best way is to use cpio instead of tar. cpio can write tar archives and is designed to take the list of files to archive from stdin.



find mydir -type f -maxdepth 1 -print0 | cpio -o -H ustar -0 > mydir.tar


Using find and cpio is a more unix-y approach in that you let find do the file selection with all the power that it has, and let cpio do the archiving. It is worth learning this simple use of cpio, as you find it easy to solve problems you bang your ahead against when trying tar.






share|improve this answer

























  • Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

    – Ian McGowan
    Feb 23 '16 at 18:07






  • 2





    This worked much better for me than the accepted answer.

    – Dale Anderson
    Sep 22 '16 at 16:47


















3














I'm not sure I understand your requirements. If you want to store the regular files in mydir but not its subdirectories, the easiest way is to use zsh, where matching regular files only is the simple matter of using the . glob qualifier:



tar cf mydir.tar mydir/*(.)





share|improve this answer






























    3














    You may even use find ... -print0 and tar ... --null directly without using xargs at all.



    find . -maxdepth 1 -type f -print0 | tar cvf mydir.tar --null -T -


    In the given example, the --no-recursion option to tar is not necessary because only paths of files (and not directories) will be passed from find to tar.



    Using the --no-recursion option to tar in the following example, however, prevents tar from double archiving directories. find will do the directory tree recursion instead of tar then.



    # compare
    find . -print0 | tar cf mydir.tar --null -T -
    tar -tf mydir.tar | nl

    find . -print0 | tar cf mydir.tar --null --no-recursion -T -
    tar -tf mydir.tar | nl





    share|improve this answer






























      2














      As the introductory paragraph in man tar says (last sentence),




      The use of a directory name always implies that the
      subdirectories below should be included in the archive.




      Which I understand as a "no" answer to your question.






      share|improve this answer























      • Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

        – ateiob
        Nov 18 '11 at 15:21











      • I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

        – Paul Brannan
        May 26 '15 at 19:45


















      1














      star -c -C startdir -find . ! -type d > out.tar


      Omit -C startdir and replace . with startdir if it should appear in the archive.



      This is the most efficient method based on the features of libfind. Libfind also offers primaries -chown -chgrp -chmod that modify struct stat in place and allow to archive different metadata. This also works in list and extract mode and avoids the need to extract the whole archive in many cases.






      share|improve this answer

























      • Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

        – Stéphane Chazelas
        Feb 23 '16 at 17:58












      • I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

        – schily
        Feb 23 '16 at 18:39











      • yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

        – Stéphane Chazelas
        Feb 23 '16 at 20:06











      • There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

        – schily
        Feb 23 '16 at 22:35






      • 1





        A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

        – schily
        Mar 11 '16 at 10:53


















      0














      I might have found a solution.



      find mydir -type f -printf '%P'|tar czvf mydir.tar.gz -C mydir --null -T -



      This might be costly a little, but anyway it will work, because this doesn't depend on xargs.






      share|improve this answer






























        -1














        dir=your_dir ; cd $dir ; tar -cvf file.tar `find . -maxdepth 1 -type f -print`






        share|improve this answer























          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          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%2f24870%2ftar-files-only-no-directories%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          8 Answers
          8






          active

          oldest

          votes








          8 Answers
          8






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          11














          As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
          find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar +


          Iocnarz's answer of using tar's --null and -T options works as well. If you have cpio installed, camh's answer using it is also fine. And if you have zsh and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)) seems the most straightforward.




          The key was the -maxdepth option.
          Final answer, dealing with spaces appropriately:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar


          This should also work:



          find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +





          share|improve this answer




















          • 3





            Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

            – camh
            Apr 6 '12 at 22:30






          • 2





            Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

            – camh
            Apr 6 '12 at 22:49











          • @camh You're right, thanks for pointing that out; I've updated to reflect that.

            – Kevin
            Apr 7 '12 at 3:22






          • 1





            The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

            – eckes
            May 31 '13 at 17:27















          11














          As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
          find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar +


          Iocnarz's answer of using tar's --null and -T options works as well. If you have cpio installed, camh's answer using it is also fine. And if you have zsh and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)) seems the most straightforward.




          The key was the -maxdepth option.
          Final answer, dealing with spaces appropriately:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar


          This should also work:



          find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +





          share|improve this answer




















          • 3





            Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

            – camh
            Apr 6 '12 at 22:30






          • 2





            Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

            – camh
            Apr 6 '12 at 22:49











          • @camh You're right, thanks for pointing that out; I've updated to reflect that.

            – Kevin
            Apr 7 '12 at 3:22






          • 1





            The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

            – eckes
            May 31 '13 at 17:27













          11












          11








          11







          As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
          find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar +


          Iocnarz's answer of using tar's --null and -T options works as well. If you have cpio installed, camh's answer using it is also fine. And if you have zsh and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)) seems the most straightforward.




          The key was the -maxdepth option.
          Final answer, dealing with spaces appropriately:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar


          This should also work:



          find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +





          share|improve this answer















          As camh points out, the previous command had a small problem in that given too many file names, it would execute more than once, with later invocations silently wiping out the previous runs. Since we're not compressing too, we can append instead of overwrite:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar Avf mydir.tar
          find mydir -maxdepth 1 -type f -exec tar Avf mydir.tar +


          Iocnarz's answer of using tar's --null and -T options works as well. If you have cpio installed, camh's answer using it is also fine. And if you have zsh and don't mind using it for a command, Gilles's answer using a zsh glob (*(.)) seems the most straightforward.




          The key was the -maxdepth option.
          Final answer, dealing with spaces appropriately:



          find mydir -maxdepth 1 -type f -print0 | xargs -0 tar cvf mydir.tar


          This should also work:



          find mydir -maxdepth 1 -type f -exec tar cvf mydir.tar +






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 13 '17 at 12:36









          Community

          1




          1










          answered Nov 18 '11 at 15:55









          KevinKevin

          27.9k1066103




          27.9k1066103







          • 3





            Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

            – camh
            Apr 6 '12 at 22:30






          • 2





            Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

            – camh
            Apr 6 '12 at 22:49











          • @camh You're right, thanks for pointing that out; I've updated to reflect that.

            – Kevin
            Apr 7 '12 at 3:22






          • 1





            The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

            – eckes
            May 31 '13 at 17:27












          • 3





            Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

            – camh
            Apr 6 '12 at 22:30






          • 2





            Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

            – camh
            Apr 6 '12 at 22:49











          • @camh You're right, thanks for pointing that out; I've updated to reflect that.

            – Kevin
            Apr 7 '12 at 3:22






          • 1





            The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

            – eckes
            May 31 '13 at 17:27







          3




          3





          Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

          – camh
          Apr 6 '12 at 22:30





          Both of these can result in multiple invocations of tar. Both xargs and find with the + variant has a maximum number of arguments that can be passed to tar. This will result in the second invocation of tar overwriting the output of the first.

          – camh
          Apr 6 '12 at 22:30




          2




          2





          Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

          – camh
          Apr 6 '12 at 22:49





          Expanding on the xargs limit, xargs default to a maximum 128KiB command line. If the file list is larger, you get a second (or more) invocation of the command (tar), leading to silent data loss. You can use -x to force xargs to fail instead of losing data, and while better than a silent data loss bug, it's still not ideal. This sort bug is so dangerous because everything seems OK at first, but as the file list grows over time, you start triggering it and may not notice until you try to restore your backup. Then it's too late.

          – camh
          Apr 6 '12 at 22:49













          @camh You're right, thanks for pointing that out; I've updated to reflect that.

          – Kevin
          Apr 7 '12 at 3:22





          @camh You're right, thanks for pointing that out; I've updated to reflect that.

          – Kevin
          Apr 7 '12 at 3:22




          1




          1





          The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

          – eckes
          May 31 '13 at 17:27





          The "final answer" is not correct, if you use "tar c" without -T you might get partial results.

          – eckes
          May 31 '13 at 17:27













          13














          When you want to use find with tar, the best way is to use cpio instead of tar. cpio can write tar archives and is designed to take the list of files to archive from stdin.



          find mydir -type f -maxdepth 1 -print0 | cpio -o -H ustar -0 > mydir.tar


          Using find and cpio is a more unix-y approach in that you let find do the file selection with all the power that it has, and let cpio do the archiving. It is worth learning this simple use of cpio, as you find it easy to solve problems you bang your ahead against when trying tar.






          share|improve this answer

























          • Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

            – Ian McGowan
            Feb 23 '16 at 18:07






          • 2





            This worked much better for me than the accepted answer.

            – Dale Anderson
            Sep 22 '16 at 16:47















          13














          When you want to use find with tar, the best way is to use cpio instead of tar. cpio can write tar archives and is designed to take the list of files to archive from stdin.



          find mydir -type f -maxdepth 1 -print0 | cpio -o -H ustar -0 > mydir.tar


          Using find and cpio is a more unix-y approach in that you let find do the file selection with all the power that it has, and let cpio do the archiving. It is worth learning this simple use of cpio, as you find it easy to solve problems you bang your ahead against when trying tar.






          share|improve this answer

























          • Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

            – Ian McGowan
            Feb 23 '16 at 18:07






          • 2





            This worked much better for me than the accepted answer.

            – Dale Anderson
            Sep 22 '16 at 16:47













          13












          13








          13







          When you want to use find with tar, the best way is to use cpio instead of tar. cpio can write tar archives and is designed to take the list of files to archive from stdin.



          find mydir -type f -maxdepth 1 -print0 | cpio -o -H ustar -0 > mydir.tar


          Using find and cpio is a more unix-y approach in that you let find do the file selection with all the power that it has, and let cpio do the archiving. It is worth learning this simple use of cpio, as you find it easy to solve problems you bang your ahead against when trying tar.






          share|improve this answer















          When you want to use find with tar, the best way is to use cpio instead of tar. cpio can write tar archives and is designed to take the list of files to archive from stdin.



          find mydir -type f -maxdepth 1 -print0 | cpio -o -H ustar -0 > mydir.tar


          Using find and cpio is a more unix-y approach in that you let find do the file selection with all the power that it has, and let cpio do the archiving. It is worth learning this simple use of cpio, as you find it easy to solve problems you bang your ahead against when trying tar.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 7 '12 at 2:01

























          answered Apr 6 '12 at 22:36









          camhcamh

          25.4k76353




          25.4k76353












          • Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

            – Ian McGowan
            Feb 23 '16 at 18:07






          • 2





            This worked much better for me than the accepted answer.

            – Dale Anderson
            Sep 22 '16 at 16:47

















          • Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

            – Ian McGowan
            Feb 23 '16 at 18:07






          • 2





            This worked much better for me than the accepted answer.

            – Dale Anderson
            Sep 22 '16 at 16:47
















          Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

          – Ian McGowan
          Feb 23 '16 at 18:07





          Brilliant! I've used cpio in the distant past, but never knew of the ustar option.

          – Ian McGowan
          Feb 23 '16 at 18:07




          2




          2





          This worked much better for me than the accepted answer.

          – Dale Anderson
          Sep 22 '16 at 16:47





          This worked much better for me than the accepted answer.

          – Dale Anderson
          Sep 22 '16 at 16:47











          3














          I'm not sure I understand your requirements. If you want to store the regular files in mydir but not its subdirectories, the easiest way is to use zsh, where matching regular files only is the simple matter of using the . glob qualifier:



          tar cf mydir.tar mydir/*(.)





          share|improve this answer



























            3














            I'm not sure I understand your requirements. If you want to store the regular files in mydir but not its subdirectories, the easiest way is to use zsh, where matching regular files only is the simple matter of using the . glob qualifier:



            tar cf mydir.tar mydir/*(.)





            share|improve this answer

























              3












              3








              3







              I'm not sure I understand your requirements. If you want to store the regular files in mydir but not its subdirectories, the easiest way is to use zsh, where matching regular files only is the simple matter of using the . glob qualifier:



              tar cf mydir.tar mydir/*(.)





              share|improve this answer













              I'm not sure I understand your requirements. If you want to store the regular files in mydir but not its subdirectories, the easiest way is to use zsh, where matching regular files only is the simple matter of using the . glob qualifier:



              tar cf mydir.tar mydir/*(.)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 '11 at 1:12









              GillesGilles

              547k13011131629




              547k13011131629





















                  3














                  You may even use find ... -print0 and tar ... --null directly without using xargs at all.



                  find . -maxdepth 1 -type f -print0 | tar cvf mydir.tar --null -T -


                  In the given example, the --no-recursion option to tar is not necessary because only paths of files (and not directories) will be passed from find to tar.



                  Using the --no-recursion option to tar in the following example, however, prevents tar from double archiving directories. find will do the directory tree recursion instead of tar then.



                  # compare
                  find . -print0 | tar cf mydir.tar --null -T -
                  tar -tf mydir.tar | nl

                  find . -print0 | tar cf mydir.tar --null --no-recursion -T -
                  tar -tf mydir.tar | nl





                  share|improve this answer



























                    3














                    You may even use find ... -print0 and tar ... --null directly without using xargs at all.



                    find . -maxdepth 1 -type f -print0 | tar cvf mydir.tar --null -T -


                    In the given example, the --no-recursion option to tar is not necessary because only paths of files (and not directories) will be passed from find to tar.



                    Using the --no-recursion option to tar in the following example, however, prevents tar from double archiving directories. find will do the directory tree recursion instead of tar then.



                    # compare
                    find . -print0 | tar cf mydir.tar --null -T -
                    tar -tf mydir.tar | nl

                    find . -print0 | tar cf mydir.tar --null --no-recursion -T -
                    tar -tf mydir.tar | nl





                    share|improve this answer

























                      3












                      3








                      3







                      You may even use find ... -print0 and tar ... --null directly without using xargs at all.



                      find . -maxdepth 1 -type f -print0 | tar cvf mydir.tar --null -T -


                      In the given example, the --no-recursion option to tar is not necessary because only paths of files (and not directories) will be passed from find to tar.



                      Using the --no-recursion option to tar in the following example, however, prevents tar from double archiving directories. find will do the directory tree recursion instead of tar then.



                      # compare
                      find . -print0 | tar cf mydir.tar --null -T -
                      tar -tf mydir.tar | nl

                      find . -print0 | tar cf mydir.tar --null --no-recursion -T -
                      tar -tf mydir.tar | nl





                      share|improve this answer













                      You may even use find ... -print0 and tar ... --null directly without using xargs at all.



                      find . -maxdepth 1 -type f -print0 | tar cvf mydir.tar --null -T -


                      In the given example, the --no-recursion option to tar is not necessary because only paths of files (and not directories) will be passed from find to tar.



                      Using the --no-recursion option to tar in the following example, however, prevents tar from double archiving directories. find will do the directory tree recursion instead of tar then.



                      # compare
                      find . -print0 | tar cf mydir.tar --null -T -
                      tar -tf mydir.tar | nl

                      find . -print0 | tar cf mydir.tar --null --no-recursion -T -
                      tar -tf mydir.tar | nl






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 8 '12 at 17:37









                      locnarzlocnarz

                      391




                      391





















                          2














                          As the introductory paragraph in man tar says (last sentence),




                          The use of a directory name always implies that the
                          subdirectories below should be included in the archive.




                          Which I understand as a "no" answer to your question.






                          share|improve this answer























                          • Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                            – ateiob
                            Nov 18 '11 at 15:21











                          • I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                            – Paul Brannan
                            May 26 '15 at 19:45















                          2














                          As the introductory paragraph in man tar says (last sentence),




                          The use of a directory name always implies that the
                          subdirectories below should be included in the archive.




                          Which I understand as a "no" answer to your question.






                          share|improve this answer























                          • Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                            – ateiob
                            Nov 18 '11 at 15:21











                          • I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                            – Paul Brannan
                            May 26 '15 at 19:45













                          2












                          2








                          2







                          As the introductory paragraph in man tar says (last sentence),




                          The use of a directory name always implies that the
                          subdirectories below should be included in the archive.




                          Which I understand as a "no" answer to your question.






                          share|improve this answer













                          As the introductory paragraph in man tar says (last sentence),




                          The use of a directory name always implies that the
                          subdirectories below should be included in the archive.




                          Which I understand as a "no" answer to your question.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 18 '11 at 14:52









                          rozcietrzewiaczrozcietrzewiacz

                          29.6k47392




                          29.6k47392












                          • Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                            – ateiob
                            Nov 18 '11 at 15:21











                          • I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                            – Paul Brannan
                            May 26 '15 at 19:45

















                          • Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                            – ateiob
                            Nov 18 '11 at 15:21











                          • I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                            – Paul Brannan
                            May 26 '15 at 19:45
















                          Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                          – ateiob
                          Nov 18 '11 at 15:21





                          Good catch, except that tar now includes numerous exclusion meachisms (e.g. --no-recursion, --exclude-tag, etc.). I am looking into --exclude-tag which looks promising but seems to be the exact opposite of what I am looking for.

                          – ateiob
                          Nov 18 '11 at 15:21













                          I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                          – Paul Brannan
                          May 26 '15 at 19:45





                          I tried this and I saw characters being removed from long pathnames. Using tar with -T and --null as camh proposed avoided this problem.

                          – Paul Brannan
                          May 26 '15 at 19:45











                          1














                          star -c -C startdir -find . ! -type d > out.tar


                          Omit -C startdir and replace . with startdir if it should appear in the archive.



                          This is the most efficient method based on the features of libfind. Libfind also offers primaries -chown -chgrp -chmod that modify struct stat in place and allow to archive different metadata. This also works in list and extract mode and avoids the need to extract the whole archive in many cases.






                          share|improve this answer

























                          • Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                            – Stéphane Chazelas
                            Feb 23 '16 at 17:58












                          • I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                            – schily
                            Feb 23 '16 at 18:39











                          • yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                            – Stéphane Chazelas
                            Feb 23 '16 at 20:06











                          • There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                            – schily
                            Feb 23 '16 at 22:35






                          • 1





                            A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                            – schily
                            Mar 11 '16 at 10:53















                          1














                          star -c -C startdir -find . ! -type d > out.tar


                          Omit -C startdir and replace . with startdir if it should appear in the archive.



                          This is the most efficient method based on the features of libfind. Libfind also offers primaries -chown -chgrp -chmod that modify struct stat in place and allow to archive different metadata. This also works in list and extract mode and avoids the need to extract the whole archive in many cases.






                          share|improve this answer

























                          • Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                            – Stéphane Chazelas
                            Feb 23 '16 at 17:58












                          • I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                            – schily
                            Feb 23 '16 at 18:39











                          • yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                            – Stéphane Chazelas
                            Feb 23 '16 at 20:06











                          • There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                            – schily
                            Feb 23 '16 at 22:35






                          • 1





                            A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                            – schily
                            Mar 11 '16 at 10:53













                          1












                          1








                          1







                          star -c -C startdir -find . ! -type d > out.tar


                          Omit -C startdir and replace . with startdir if it should appear in the archive.



                          This is the most efficient method based on the features of libfind. Libfind also offers primaries -chown -chgrp -chmod that modify struct stat in place and allow to archive different metadata. This also works in list and extract mode and avoids the need to extract the whole archive in many cases.






                          share|improve this answer















                          star -c -C startdir -find . ! -type d > out.tar


                          Omit -C startdir and replace . with startdir if it should appear in the archive.



                          This is the most efficient method based on the features of libfind. Libfind also offers primaries -chown -chgrp -chmod that modify struct stat in place and allow to archive different metadata. This also works in list and extract mode and avoids the need to extract the whole archive in many cases.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 23 '16 at 17:56









                          Stéphane Chazelas

                          314k57594952




                          314k57594952










                          answered Aug 25 '15 at 11:22









                          schilyschily

                          10.9k31744




                          10.9k31744












                          • Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                            – Stéphane Chazelas
                            Feb 23 '16 at 17:58












                          • I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                            – schily
                            Feb 23 '16 at 18:39











                          • yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                            – Stéphane Chazelas
                            Feb 23 '16 at 20:06











                          • There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                            – schily
                            Feb 23 '16 at 22:35






                          • 1





                            A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                            – schily
                            Mar 11 '16 at 10:53

















                          • Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                            – Stéphane Chazelas
                            Feb 23 '16 at 17:58












                          • I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                            – schily
                            Feb 23 '16 at 18:39











                          • yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                            – Stéphane Chazelas
                            Feb 23 '16 at 20:06











                          • There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                            – schily
                            Feb 23 '16 at 22:35






                          • 1





                            A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                            – schily
                            Mar 11 '16 at 10:53
















                          Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                          – Stéphane Chazelas
                          Feb 23 '16 at 17:58






                          Can you use -find in -copy mode. The man page synopsis suggests you can but I've not been able to make it work (while trying to answer How to copy modified files while preserving folder structure) (with schily-2016-02-10)

                          – Stéphane Chazelas
                          Feb 23 '16 at 17:58














                          I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                          – schily
                          Feb 23 '16 at 18:39





                          I did not test this for a longer time. It may be that there currently is a bug. The man page says that the last argument would be the extraction directory, but when I try this, I get: "Path arguments not yet supported in extract mode". Is this what you get when you try?

                          – schily
                          Feb 23 '16 at 18:39













                          yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                          – Stéphane Chazelas
                          Feb 23 '16 at 20:06





                          yes. The man page also says you can only have find arguments after -find but if I put the destination directory before -find I also get an error

                          – Stéphane Chazelas
                          Feb 23 '16 at 20:06













                          There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                          – schily
                          Feb 23 '16 at 22:35





                          There is code to switch off the find code in the extract process and the error message is from an incorrect check that expects -c but should also permit -copy. In -copy mode, the find parser should not include the last argument.

                          – schily
                          Feb 23 '16 at 22:35




                          1




                          1





                          A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                          – schily
                          Mar 11 '16 at 10:53





                          A final solution now has been published in the schily tools at sourceforge.net/projects/schilytools/files/schily-2016-03-11.tar.bz2 this also fixes a problem from a missing setlocale() in star and this enables -C directrory for star -find.

                          – schily
                          Mar 11 '16 at 10:53











                          0














                          I might have found a solution.



                          find mydir -type f -printf '%P'|tar czvf mydir.tar.gz -C mydir --null -T -



                          This might be costly a little, but anyway it will work, because this doesn't depend on xargs.






                          share|improve this answer



























                            0














                            I might have found a solution.



                            find mydir -type f -printf '%P'|tar czvf mydir.tar.gz -C mydir --null -T -



                            This might be costly a little, but anyway it will work, because this doesn't depend on xargs.






                            share|improve this answer

























                              0












                              0








                              0







                              I might have found a solution.



                              find mydir -type f -printf '%P'|tar czvf mydir.tar.gz -C mydir --null -T -



                              This might be costly a little, but anyway it will work, because this doesn't depend on xargs.






                              share|improve this answer













                              I might have found a solution.



                              find mydir -type f -printf '%P'|tar czvf mydir.tar.gz -C mydir --null -T -



                              This might be costly a little, but anyway it will work, because this doesn't depend on xargs.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 25 '15 at 5:15









                              cielciel

                              1




                              1





















                                  -1














                                  dir=your_dir ; cd $dir ; tar -cvf file.tar `find . -maxdepth 1 -type f -print`






                                  share|improve this answer



























                                    -1














                                    dir=your_dir ; cd $dir ; tar -cvf file.tar `find . -maxdepth 1 -type f -print`






                                    share|improve this answer

























                                      -1












                                      -1








                                      -1







                                      dir=your_dir ; cd $dir ; tar -cvf file.tar `find . -maxdepth 1 -type f -print`






                                      share|improve this answer













                                      dir=your_dir ; cd $dir ; tar -cvf file.tar `find . -maxdepth 1 -type f -print`







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Sep 19 '17 at 21:46









                                      H BoschH Bosch

                                      1




                                      1



























                                          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%2f24870%2ftar-files-only-no-directories%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 check contact read email or not when send email to Individual?

                                          Bahrain

                                          Postfix configuration issue with fips on centos 7; mailgun relay