Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

archiving hidden directories with tar

Tags:

tar on a directory mydir will archive hidden files and hidden subdirectories, but tar from within mydir with a * wildcard will not. Is this a known inconsistency or bug?

Edit: Additional information. tar from within mydir with a * wildcard will not "see" nor archive hidden files and hidden subdirectories in the immediate directory. However, in the non-hidden subdirectories of mydir hidden files and hidden subdirectories will be archived. In other words, deeper in the directory tree the hidden objects will be archived.

like image 480
H2ONaCl Avatar asked Jan 01 '11 02:01

H2ONaCl


People also ask

Does tar archive hidden files?

Bookmark this question. Show activity on this post. tar on a directory mydir will archive hidden files and hidden subdirectories, but tar from within mydir with a * wildcard will not.

How do I archive a directory in tar?

To create a tar archive, use the -c option followed by -f and the name of the archive. You can create archives from the contents of one or more directories or files. By default, directories are archived recursively unless --no-recursion option is specified.

Can we tar a directory?

Tape Archive or tar is a file format for creating files and directories into an archive while preserving filesystem information such as permissions. We can use the tar command to create tar archives, extract the archives, view files and directories stored in the archives, and append files to an existing archive.


2 Answers

With wildcard it will not work. You have to specify . (current directory) if you mean full directory including hidden files. You can do

tar -cvpzf test.tgz . 
like image 156
Madhur Ahuja Avatar answered Oct 21 '22 05:10

Madhur Ahuja


The answer is that the * wildcard is handled by the shell and it just does not expand to things that start with a dot. The other wildcard ? also does not expand to things that start with a dot. Thanks to Keith for pointing out it is the shell that does the expansion and so it has nothing to do with tar.

If you use shopt -s dotglob then expansion will include things like .filename. Thanks to Andy.

Use shopt -u dotglob to turn it off.

Switching the dotglob option does not change ls itself. Rather it just changes expansion behaviour as exhibited in something like ls *.

Edit: My recommendations are in a comment below.

like image 20
H2ONaCl Avatar answered Oct 21 '22 05:10

H2ONaCl