Skip to content

Instantly share code, notes, and snippets.

@DavMorr
Last active March 9, 2024 11:43
Show Gist options
  • Save DavMorr/53e3ad5c26d16f4f6be3220b078b19c9 to your computer and use it in GitHub Desktop.
Save DavMorr/53e3ad5c26d16f4f6be3220b078b19c9 to your computer and use it in GitHub Desktop.
Include hidden files in tar archive
When you create a tar archive of a directory tree the hidden files are normally not included. Here’s how to include the hidden files.
Say you have a web directory called “/var/www/html/mysite/” that contains the following tree:
.htaccess
index.php
logo.jpg
style.css
admin_dir/.htaccess
admin_dir/includes.php
admin_dir/index.php
Normally you would use the tar command like this:
tar czf mysite.tar.gz /var/www/html/mysite/*
This way all files are tarred except the .htaccess files.
The solution to include hidden files: replace the asterisk (*) by a dot (.):
tar czf mysite.tar.gz /var/www/html/mysite/.
This way the .htaccess files are included in the tarfile.
* Borrowed from: http://www.digitesters.com/linux-include-hidden-files-in-tar-archive/
@seidler2547
Copy link

What's the difference to tar czf mysite.tar.gz /var/www/html/mysite?

@growtopiajaw
Copy link

@seidler2547 the difference is that your command will include the directory "mysite" in the tar archive whereas @DavMorr's command will only include the files inside of "mysite" (without the "mysite" directory)

@SIMULATAN
Copy link

Thanks for teaching me the exact opposite of what you actually planned on teaching LOL
I wanted to zip all files that are visible (and exclude hidden ones), but used ., and thus, hidden files were included as well. Thankfully you included the wrong way for your goal, which was the right one for mine 😁

@SDClass
Copy link

SDClass commented Apr 27, 2023

Great explanation. Just what I was looking for to demystify some of behavior of tar. THANK YOU!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment