Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tar exclude file pattern

Tags:

file

tar

I am trying to create a tar with follwing command:

tar -cvf myFile.tar -X exclude-files.txt myContentDirectory

and my exclude-file has follwing patterns to exclude:

**/*.bak
**/*.db
**/*.html

But i dont see these file types being excluded out in my tar. What am I doing wrong here?

I found that when i have just one pattern in my exclude-files.txt, lets say only

**/*.bak

it does work. But not with multiple file patterns (EACH ON NEW LINE)

like image 892
user620339 Avatar asked Sep 21 '11 16:09

user620339


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.

What is verbose in tar command?

' --verbose ' (' -v ') shows details about the results of running tar . This can be especially useful when the results might not be obvious. For example, if you want to see the progress of tar as it writes files into the archive, you can use the ' --verbose ' option.

Does tar ignore hidden files?

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. This way all files are tarred except the . htaccess files.


2 Answers

I think this:

*.bak
*.db
*.html

is the correct format for the exclude file if you want to exclude a particular directory you could do:

some-dir/*.db

Also your command should look like this:

tar -cvf myFile.tar -X exclude-files.txt myContentDirectory 
like image 60
Mike K. Avatar answered Nov 16 '22 01:11

Mike K.


Sorry if this answer is a little late.

tar -cO --exclude=*.bak myContentDirectory | tar -O --delete '*.db' | tar -O --delete '*.html' > myFile.tar

See, what you're doing here is creating the tar, but sending it to stdout instead of to a file then piping that into tar to delete the stuff you don't want, one or more times and finally writing the output to a file.

You can even test it first like this:

tar -cO --exclude=*.bak myContentDirectory | tar -O --delete '*.db' | tar -O --delete '*.html' | tar -tv

Which will spit out a list of all the files remaining in the archive.

like image 33
Okonomiyaki3000 Avatar answered Nov 16 '22 03:11

Okonomiyaki3000