Create tar with certain file types

Here's a simple example of how to use bash to create tar files with a given date and a sub-set of files

bash, Linux, Windows, XML

Let's say you have a deep folder structure and you want to retain that structure but only include a certain subset of file types, how you gonna do that?

Here's a really simple example of how to create a tar that :-

  1. Only contains xml files

  2. Retains the folder structure of the source data

  3. The generated tar.gz Filename must contain the date

  4. Must be a re-usable bash command that could be used via cron

Here's the Command Well one way to skin this cat!

now=$(date +"%m_%d_%Y"); find . -name "*.xml"  | tar -cvzf ../../data-v1.03.rc1-$now.tar.gz -T -

Breaking it down there are 3 commands used :

  • date
  • find
  • tar

date & bash var

First of all we store the date formatted into a var now

now=$(date +"%m_%d_%Y"); 

You can check the format within the now varible via :

$ echo $now
$ 05_08_2014 

find & pipe the files

The using find we print, via stdOut, the files and paths that match the search term.

find . -name "*.xml"

These are then piped via the | (pipe) char to the tar command

tar the stdIn

Now we take the list of files found via find and using the -T - switches of tar via stdOut of find to stdIn of tar.

Using the var $now generated earlier we create a filename that contains todays date.

tar -cvzf ../../data-v1.03.rc1-$now.tar.gz -T -

Please note the importantace of the trailing -, a few times I've been confused by ommiting it. Andrew Scott

Simple Backups

You can see how easy it is to generate a command that will generate a tar.gz file with only the data we're interested in.

Combine this with a cron / crontab setup you have, albeit quick crude and simple, a very resiliant backup solution.

More File Types

If you wanted to included, say, .jpg or .png file type too simply modify the find command to :

find . -name "*.xml" -o -name '*.jpg' -o -name '*.png'

What I'm using

now=$(date +"%m_%d_%Y"); 
find . -name "*.xml" -o -name '*.jpg' -o -name '*.png' -o -name '.ico' -o -name '.pdf' | \
tar -cvzf ../data-v1.03.LIVE-$now.tar.gz -T -