Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make tar exclude hidden directories

Tags:

tar

hidden

When I want to exclude directories when taring, I typically use syntax like this:

tar -zcf /backup/backup.tar.gz --exclude="/home/someuser/.ssh" /home/someuser 

How can I modify this to exclude all hidden directories, for example, in addition to .ssh/, I also want to exclude .vnc/, .wine/, etc.

like image 332
user788171 Avatar asked Dec 10 '13 06:12

user788171


People also ask

How do I exclude a directory in tar?

Sometimes it's nice if you are tar zipping a directory (let's say for a backup) and want to exclude a directory. For instance, if you are backing up a project and want to exclude the images directory (for size) or exclude the build or dist directory you can run the linux tar command with the --exclude option.

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.


1 Answers

You can use --exclude=".*"

$ tar -czvf test.tgz test/ test/ test/seen test/.hidden $ tar --exclude=".*" -czvf test.tgz test/ test/ test/seen 

Be careful if you are taring the current directory, since it will also be excluded by this pattern matching.

$ cd test $ tar --exclude=".*" -czvf test.tgz ./ $ tar -czvf test.tgz ./ ./ ./seen ./.hidden 

Then you need to use --exclude='.[^/]*' as described elsewhere

$ tar --exclude='.[^/]*' -czvf test.tgz ./ ./ ./seen 
like image 67
joelostblom Avatar answered Oct 08 '22 20:10

joelostblom