How to ZIP, TAR, and UNTAR files

This article will help you to handle file formats TAR, GZIP, BZIP and ZIP on Linux operating systems (including Ubuntu, and CentOS) and Unix-based operating systems such as MacOS (OS X) VIA command line either via SSH, or a local terminal session.

First, you will need to install the Needed Tools.

On many Linux-based operating systems the command line tools for working with compressed files (ZIP, BZIP, GZIP, AND TAR) are already installed, so in most cases you don't need to install anything, but if you're running a minimal installation of your operating system, or if you've removed tools, follow the below instructions to ensure their re-installed. 

The below instructions assume you're running commands as the root user. if you're running as another user you will need to prepend the commands with SUDO.

Example: sudo yum install ...

Start a terminal session, or login to your server/computer via SSH. The base respositories have the the packages we need.

Execute the following command:

yum install tar gzip zip unzip bzip2.

If any of the above are already installed you'll receive notification. However, if any are missing you will be asked if you want to install them. Answer y if everything looks OK. The system will now download the needed packages and install them. You will now be able to follow the rest of the directions in this article. 

Working with TAR Files

The TAR file format is a very early archiving format that doesn't include any active compression by default. Often on Linux, items are tarred and then gzipped to compress them. TAR files typically end in .tar.

Put a Directory into a TAR File

Execute the following to create a single .tar file containing all of the contents of the specified directory:

tar cvf FILENAME.tar DIRECTORY/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a tarball.

Command Flags Explanation

c: Create a TAR file.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename for the resulting TAR file.

Put a Directory into a TAR file and Compress it with GZIP

Execute the following to create a single .tar.gz file containing all of the contents of the specified directory:

tar cvfz FILENAME.tar.gz DIRECTORY/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

Tarred files compressed with GZIP sometimes use the .tgz file extension.

Command Flags Explanation

c: Create a TAR file.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename for the resulting TAR file.

z: Compress the TAR file with GZIP

Put a Directory into a TAR file and Compress it with BZIP2

Execute the following to create a single .tar.bz2 file containing all of the contents of the specified directory compressed with BZIP. (BZIP typically produces smaller files than GZIP, at the cost of more processing time.):

tar cvfj FILENAME.tar.bz2 DIRECTORY/

Replace FILENAME with whatever filename you want and DIRECTORY with the path to the directory you want to make into a compressed tarball.

Command Flags Explanation

c: Create a TAR file.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename for the resulting TAR file.

j: Compress the TAR file with BZIP2

Extract Items from TAR Files

Execute the following command to extract files and directories from an uncompressed .tar file:

tar xvf FILE.tar

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename to uncompress.

Extract Items from GZIPPED Tarball File

Execute the following command to extract files and directories from a GZIP compressed TAR file:

tar xvfz FILE.tar.gz

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename to uncompress.

z: Uncompress the tarball via GZIP.

Extract Items from BZIPPED Tarball File

Execute the following command to extract files and directories from a BZIP compressed TAR file:

tar xvfj FILE.tar.bz2

Replace FILE with the filename of the file you are trying to uncompress. The file will uncompress into the current directory.

Command Flags Explanation

x: Extract the contents from the file specified.

v: Output verbosely (you'll be told exactly what is happening in detail).

f: Specify a filename to uncompress.

j: Uncompress the tarball via BZIP2.

If you'd rather specify a different directory to extract files to rather than just dumping everything in the current directory add -C /PATH/TO/DIRECTORY/ to the commands above. Replace /PATH/TO/DIRECTORY/ with the actual path to the directory where you want the files to be placed.

Working with Zip Files

Zip is probably the most common compressed archiving format in the world. Zip files usually end in .zip.

Compress a Directory Full of Files into a ZIP File

Execute the following command to place everything inside a specified directory into a compressed ZIP file.

zip -r FILE.zip DIRECTORY/

Command Flags Explanation

-r: Recursively all files and directories contained within DIRECTORY/ in the zip file (otherwise you only get the top level files).

Uncompress a ZIP file Into the Current Directory

Execute the following command to uncompress the items in the ZIP file into the current directory.

unzip FILE.zip

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)