Loose Zips Sink Ships

Tar(tape archive) is a tool that allows us to bundle files together in a single file. It also provides us a few different ways to compress files for easier transport.


##
Tar without compression

Create a tar file
tar -cvf .tar /<directory>/<to>/<include>
–This will maintain directory structure when you extract the tar.

tar -cvf .tar /<directory>/*.txt
–This will create a tar only with the files you requested.

tar -cvf .tar
–This will allow you to choose individual files.

Exclude file from tar
tar --exclude='/<directory>/<excluded>' --exclude='<directory>/<excluded2>/' -cvf .tar /<directory>/<to>/<include>
–Be sure to place all exclude’s before source and destination.

Show tar file contents
tar --list --file=.tar

Extract a tar file contents
tar -xfv .tar .
–This will extract to the current directory.

tar -xfv .tar -C /<directory>/<to>/<unzip-to>
–This will specify where to extract the contents to.

###
Tar also provides a number of ways to compress your tar file contents, gunzip and bz2 are the most common. They are distiguished from a plain tar file by the file extensions, .tar.gz or .tar.bz2 respectively. All the above methods to read and save files apply to compressed files as well.

Create a gunzip (gz) file
tar -zcvf .tar.gz /<directory>/<to>/<include>

Unzip a gunzip (gz) file
tar -zxvf .tar.gz -C /<directory>/<to>/<unzip-to>

Create a BZ2 file
tar -jcvf .tar.gz /<directory>/<to>/<include>

Unzip a BZ2 file
tar -jxvf .tar.gz -C  /<directory>/<to>/<unzip-to>

###
One interesting memory trick for remembering the syntax for unzipping gz files, is to say it (in your head) using a “Hollywood” German accent. xzf becomes “eXtract Zee File”, xzf being the minimum flags necessary to unzip your tar.

Leave a Reply

Your email address will not be published. Required fields are marked *