Linux tar quick reference

by Long Manh Tran

I frequently find myself checking the options available when using the tar function to move areas of our web sites between and internally around servers. Here’s a quick reference guide for using this function.

tar is used to create a Tape Archive and is similar to zip functions minus the compression. The resulting file combines all selected files/directories into a single file known as a tarball.

Create

To tar up the contents of directory ‘mydir’ use the commands:

tar -cvf mydir.tar mydir/

To tar up and compress the contents of directory ‘mydir’ use the commands:

tar -cfz mydir.tar.gz mydir/

Extract

To extract the files out of a tarball ‘mydir’ use the following command

tar -xvf mydir.tar

To extract the files from a compressed (gzipped) tarball ‘mydir’ use the following command

tar -xvfz mydir.tar.gz

View

To view the contents of a tarball ‘mydir’:

tar -tvf mydir.tar

To view the contents of a compressed tarball ‘mydir’:

tar -tzf mydir.tar.gz

BrightLemon