I need to create a tarball of a given directory. However, I need to make sure hidden files are included too (such as those beginning with .).

Will the following command automatically take the hidden files into account?

tar -cvzf packed.tar.gz mydir

If not, how can I make sure I include hidden files?

  • 2
    use tar -cvzf packed.tar.gz mydir/. Sep 10, 2019 at 10:54
  • 1
    NOTICE: In my case, I've realized(by experiment) that tar czvf something.tar.gz path contains them, but tar czvf something.tar.gz path/* doesn't!
    – aderchox
    Nov 16, 2020 at 8:35
  • 2
    @aderchox Yes because the file glob will not expand dot files.
    – wcochran
    Dec 28, 2020 at 17:29

1 Answer 1


Yes, it will.

Files starting with . are not "hidden" in all contexts. They aren't expanded by *, and ls doesn't list them by default, but tar doesn't care about the leading .. (find doesn't care either.)

(Of course, this is one of those things that's easy to find out by experiment.)

  • 3
    Thanks. Yes experiment is sometimes a solution to find an answer, but in this case, I am not a unix expert and I know behaviors - in general - can be modified with configuration. What if two systems are not configured the same way? The belief I might build from one system might not be true for another. Feb 17, 2012 at 17:55
  • 1
    @JVerstry: Good point. A suggestion, though; a question like "I tried this, and it worked; is it guaranteed?" would have been even better. Feb 17, 2012 at 18:20
  • tar --help lists several different options, all beginning with --exclude that allow one to exclude various files based on name, pattern, etc. Agreed, experimentation is a great way to test and verify if something works as expected. Make backups regularly!
    – bsd
    Feb 17, 2012 at 20:38
  • 1
    FWIW, this didn't work for me - no hidden files were included in my tar.gz.
    – zigojacko
    Jan 8, 2021 at 11:01
  • 1
    @RiccardoManfrin There was no * in the question. If you do tar cvzf data.tar.gz *, the * is expanded by the shell; tar never sees it. If you do tar cvzf data.tar.gz '*', tar will see the * and will interpret as a file literally named *. tar (at least GNU tar) does have some support for wildcards, but as far as I know only for files in an archive. gnu.org/software/tar/manual/html_node/wildcards.html Jan 24 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.