Working with TAR Files

This post is mostly for personal reference because I have to keep looking up how to use the tar utility. Here, I’ll cover what tar files are and how to read and write them. The name “tar” is short for Tape ARchive. The tar utility is used to write a set of files and/or directories to a single file that can be easily transported and then un-tar-ed.

Often you will see .tar.gz or .tgz files. These are simply tar files that have been compressed using gzip. Below, we will demonstrate how to read and write regular and compressed tar files.

Unpacking tar Files

You can unpack tar files using the -xvf flag,

tar -xvf file.tar

The -xvf flag means extract, verbosely the following file. If the tar file has been compressed, use the follwing,

tar -xvzf file.tgz

The added -z flag is short for zipped.

Packing tar Files

In these commands, the -x flag is replaced with a -c flag, for compress. Here, we specify the name of the tar file we would like to create, and then list the files we would like to archive.

tar -cvf file.tar file0 file1

We have several options for compressing the archive, we may either use the -z flag mentioned above,

tar -cvzf file.tgz

Alternatively, we can tar the archive almost like normal and pipe the output to the zip or gzip utility

tar -cvf - file0 file1 file2 | gzip > file.tar.gz

References

For more information, check out https://kb.iu.edu/d/acfi.

Leave a Reply

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