-w

confirmation at every step.

-z

run it through the gzip program; you must add the .gz manually.

-f tarfile.tar

this option specifies the archive file or device.

Note
The archive file must immediately follow the -f and it’s good form to end the file name with .tar
Note
It is also good form to archive things in a subdirectory so they won’t explode all over your $PWD when you unpack it.

Bundling Up Many Files Into One

Here is an example of making a tar archive of files without a subdirectory. This is just a file comprised of other files with no directory structures. In this example I have several sound files that I want to archive and deal with as only one file. This makes a single file, noise.tar out of any and all .wav files. Make sure there are no directory structures that match *.wav!

tar -cvf noise.tar *.wav

This setup adds automatic compressing and confirmations.

tar -cvwzf noise.tar.gz *.wav

If you have an entire directory structure that you’d like to archive, use something like this from the directory you want to be the top level:

tar -cvzf package.tgz *

Here’s an important factor - if you want your archive to consist of symlinks just as they are in the filesystem, then do nothing different. If, however, you are making an archive of things that borrow from other places and you’d like to have the information that is linked become "solid" you can use the -h parameter to replace symlinks with the actual file they point to. Here’s an example:

tar -chvf coolstuff.tar ./*

In this case, if the current directory contained references to other places, this archive will actually contain the information referenced.

Unpacking Many Files From One

tar -xvf noise.tar

or for prompting (w) and gunzipping (z):

tar -xvwzf noise.tar.gz

Adding Files to a Tar Archive

To add the file cuckoo.au to the noise.tar archive:

tar -rvf noise.tar cuckoo.au

To add a file to a gzipped archive:

gunzip noise.tar.gz ; tar -rvf noise.tar cuckoo.au ; gzip noise.tar

Checking the Contents of a Tar Archive

To see what files are contained in noise.tar use:

tar -tf noise.tar

To see what files are contained in noise.tar.gz use:

tar -tzf noise.tar.gz

Using tar And ssh To Transfer Files

This is handy if rsync isn’t going to be available for some reason.

This takes all files in the current directory (recursive) and puts them under /mnt/clone on the target host.

tar -cjf - * | ssh target.host.com tar -C /mnt/clone -xjf -