# help with tar



## michaelrmgreen (Jul 23, 2009)

I'm having severe brain fail today.

I want to use tar to create a compressed archive in a file (say backup.gz) starting from a particular directory (say . - the current directory)and storing all files that match a set of patters, recursively. I can do this with a shell script, but I feel as though there should be a simple command line way. I have looked at man tar but can't work it out.

For example this:

tar -cvzf backup.gz *.dat *.glp *.txt

only works with the current dir.


Thanks for looking.


----------



## SirDice (Jul 23, 2009)

michaelrmgreen said:
			
		

> For example this:
> 
> tar -cvzf backup.gz *.dat *.glp *.txt
> 
> only works with the current dir.


Add the -C option: `$ tar -C /dir/to/backup -cvzf backup.gz *.dat *.glp *.txt`

That will backup all the .dat .glp .txt files in /dir/to/backup.


----------



## DutchDaemon (Jul 23, 2009)

But he wants recursion ..

A dirty way would be to create a tarball of the whole directory tree, and then use --include= switches to drag the wanted files out of that archive, to another archive.


```
tar -c -f new.tar --include='*foo*' @old.tgz
             creates a new archive new.tar containing only the entries from
             old.tgz containing the string `foo'.
```

I don't think tar has a recursion function for files. It will recurse into directories and tar them completely, but not files from them, I think. Maybe with weird mtree structures, which are also possible. Or just a plain 'find [something] | tar [somewhere]'.


----------



## michaelrmgreen (Jul 23, 2009)

Thank you gentlemen. For now I will continue with the shell script, but I think I may try some other tools. Please suggest a better tool for the job. Again, sincere thanks.


----------

