- How to Tar Untar and Zip Files
- How to Install the Needed Tools
- Working with TAR Files
- Put a Directory into a TAR File
- Command Flags Explanation
- Put a Directory into a TAR file and Compress it with GZIP
- Command Flags Explanation
- Put a Directory into a TAR file and Compress it with BZIP2
- Command Flags Explanation
- Extract Items from TAR Files
- Command Flags Explanation
- Extract Items from GZIPPED Tarball File
- Command Flags Explanation
- Extract Items from BZIPPED Tarball File
- Command Flags Explanation
- Working with Zip Files
- Compress a Directory Full of Files into a ZIP File
- Command Flags Explanation
- Uncompress a ZIP file Into the Current Directory
- How to Compress and Extract Files Using the Tar, Zip Command on Linux
- Installation
- Some Examples of Tar Commands
- Compressing Files Using gzip and bzip2
- Extracting Files From Compressed File
- Extracting Specific Files From Compressed File
- Some Examples of Zip and Unzip Command.
- Compressing Files Using Zip
- Unzipping File to Different Directory
- Unzipping Specific Files From Compress File
- Conclusion
How to Tar Untar and Zip Files
This article will help you understand how you can handle the common file formats TAR, GZIP, BZIP and ZIP on Linux operating systems (including CentOS, Ubuntu) and even some Unix-based OSes like MacOS (OS X) via the command line either via SSH or a local terminal session.
How to Install the Needed Tools
On many Linux-like operating systems the command line tools for working with TAR, GZIP, BZIP and ZIP files are already installed, so you almost certainly don’t need to install anything, but if you are running a minimal installation of your OS or if you’ve removed the tools in the past, follow the directions below to install what you need. Select the tab for the OS you are running:
The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.
- Pull up a terminal session or log into your server/computer via SSH.
- The base repositories for these OSes have the packages we need. Execute the following command:
yum install tar gzip zip unzip bzip2
Now you should be able to follow the rest of the directions in this article.
The directions below assume you are running as the root user. If you are running as an another user, you may need to prepend the commands with sudo.
Example: sudo apt-get install .
- Pull up a terminal session or log into your server/computer via SSH.
- The base repositories for these operating systems have the packages we need. Execute the following command:
apt-get install tar gzip zip unzip bzip2
Now you should be able to follow the rest of the directions in this article.
The needed command line tools ship with every version of MacOS/OS X since at least 10.6 Snow Leopard (and may also be installed in earlier versions we were unable to test).
To work with files via the command line, open the Terminal application located in /Applications/Utilities/Terminal(.app).
Once the terminal is open, you will be able to follow the rest of the directions in this article.
Remember, you can drag and drop files or folders into the terminal application and the full path to those items will be pasted automatically into the command line.
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:
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:
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:
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.
Command Flags Explanation
-r: Recursively compress 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.
How to Compress and Extract Files Using the Tar, Zip Command on Linux
Zip and tar are popular command-line utility for compressing and archiving multiple directories and files into one archive file in a Linux system. By default, the tar command doesn’t compress files but only collects files and their metadata to generate a single tar file but we can archive files using gzip/bzip2. Whereas, zip provides lossless data compression which is a data compression algorithm class that allows compressed data is reconstructed to its original form. The gzip and zip uses DEFLATE algorithm for compressing files whereas bzip2 uses the Burrows-Wheeler algorithm that produces highly compressed file but consumes more time as compare to gzip and zip.
Installation
By default, the tar command is integrated with the Linux system so let’s begin with installing zip and unzip. The unzip command won’t be installed while installing zip command so we need to install it separately.
Before continue with installation don’t forget to update the package info.
$ sudo apt-get install unzip
For showing the following example, I have used Ubuntu 20.4 and has the following files in the home directory.
Some Examples of Tar Commands
Before continuing with the example you must know the syntax of the tar so the syntax is like this:
$ tar -c [option] [archive name] [file] | [directory] |
The following options are the main and compulsory options that lead the command to what task should be performed. The following options can be used once in the command without it the command won’t be run.
-c: To generate archive file.
-r: To add more files to the existing archive file.
-t: View all files inside the archive.
-x: To extract the archive files.
Compressing Files Using gzip and bzip2
Tar command produces a compressed file using gzip and bzip2 which can be accessed by providing the -z and -j options to the command respectively. For archiving and compressing tar files we need to execute the following command.
$ tar -cvzf compress_sample.tar.gz *.txt
Compressing file using gzip.
$ tar -cvjf compress_sample.tar.bz2 *.txt
Compressing file using bz2.
In the above example, the -c option refers to create the archive, the -v option displays the verbose of the archived files, and -f denotes the name of the archive.
Note: -f option always must be before the filename.
Extracting Files From Compressed File
For extracting the compressed file you need to run:
$ tar -xvf compress_sample.tar.gz
The above command will extract file in the current working directory but if want to extract files in a different directory you can do so by using -C to the command.
$ tar -xvf compress_sample.tar.gz -C Documents/
Extracting files to different dir using tar.
Extracting Specific Files From Compressed File
We can extract specific files from the compressed file for that we need to specify the file name along with the directory name if it is inside the directory.
$ tar -xvf compress_sample.tar sample.txt student.txt
Extracting specific files using tar.
Some Examples of Zip and Unzip Command.
Syntax for zip and unzip are as follows:
$ zip [option] [archive_name] [Files]
$ unzip [option] [archive_name]
Compressing Files Using Zip
Zip is the archive compressor so we don’t need to provide any extra parameter to compress the files. For compression we need to run the command like this:
Unzipping File to Different Directory
We can unzip the zip files to the different directories using the -d option and specifying the directory in the command.
$ unzip zip_demo.zip -d Documents/
Unzip the file to a different directory.
Unzipping Specific Files From Compress File
To unzip a specific file from the archive the command must be executed in the following way.
$ unzip zip_demo.zip student.txt sample.txt
Unzip specific files from the zip file.
Conclusion
In this article, we learn how to archive and compress files using tar and zip commands. I have included the most common example in the article hope this will help you how to compress files.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.