33

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?

Jérôme Verstrynge
  • 1,699
  • 5
  • 19
  • 20
  • 2
    use `tar -cvzf packed.tar.gz mydir/.` – Mohammad Kholghi Sep 10 '19 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 '20 at 08:35
  • 2
    @aderchox Yes because the file glob will not expand dot files. – wcochran Dec 28 '20 at 17:29
  • For those who would like to archive all items in the current directory, the following worked for me: `$ tar czvf ../archive.tar.gz .` Whereas if the trailing dot is replaced by a `*` then the (hidden, in some sense) dotfiles from the current directory are not included. – Evgeni Sergeev Dec 29 '22 at 23:54

1 Answers1

35

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.)

Keith Thompson
  • 21,782
  • 6
  • 48
  • 55
  • 4
    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. – Jérôme Verstrynge Feb 17 '12 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. – Keith Thompson Feb 17 '12 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 '12 at 20:38
  • 1
    FWIW, this didn't work for me - no hidden files were included in my tar.gz. – zigojacko Jan 08 '21 at 11:01
  • @zigojacko That's very surprising. Can you show the exact command you used? If you used a `*` wildcard in the `tar` command, it was expanded by the shell, not by `tar`. – Keith Thompson Jan 08 '21 at 16:57
  • 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. https://www.gnu.org/software/tar/manual/html_node/wildcards.html – Keith Thompson Jan 24 '22 at 21:39
  • @KeithThompson agreed. but the first sentence was confusing to me: prefixing with the "." commonly stands for hidden files. Besides this being or not the use case for ".", I would emphasize that using the "*" is interpeted by any unix command as "all files but not the ones starting with a dot" and that tar is no exception – Riccardo Manfrin Jan 25 '22 at 22:12
  • @RiccardoManfrin As far as the file system is concerned, `.` is just another character. In some limited contexts (`ls`, the shell's `*` wildcard, and some commands that have wildcard features similar to the shell), files with names starting with `.` are hidden. But it's very important to remember that wildcard expansion is performed *by the shell*. `tar` is no exception because it can't be; it never sees the wildcard. – Keith Thompson Jan 25 '22 at 23:02