This Guide is about Archiving and Unarchiving with tar command and compressing and Uncompressing with gzip, bzip2 and zip commands. Other Than New Linux learners This Article will be useful for people looking for Zip Files , archive, Unzip Files or anything similar.
People go searching for the best compression software or file compression software but with Linux, you already have some really powerful tools and utilities preloaded and Archiving and Compression are one of the most important things you should learn if you getting started with Linux , with the variety of archiving and compressing utilities that Linux provides some commands may seem overwhelming to a newcomer.
People go searching for the best compression software or file compression software but with Linux, you already have some really powerful tools and utilities preloaded and Archiving and Compression are one of the most important things you should learn if you getting started with Linux , with the variety of archiving and compressing utilities that Linux provides some commands may seem overwhelming to a newcomer.
As This is a complete guide to Archiving and Compressing in Linux, I don't want to leave out other lesser known utilities. Before that, I would like to explain Archiving and compressing in Layman's terms.
Archiving : An archive is a single file that contains any number of individual files plus information to allow them to be restored to their original form by one or more extraction programs.
Compression : Compression is simply compressing your file's size using different techniques.
So we begin by listing all the lesser and popularly used utilities, apart from these utilities there are many more that I haven't listed. One of my favorite is the pigz compression utility for multi-core CPUs.
So Let's Get Started with Compression first.
Compression in the Linux Shell
Compressed files use less disk space and download faster than large, uncompressed files. You can compress Linux files with the open-source compression tool gzip or bzip2 or with Zip.Here's a self explanatory table for these three.Let's Drill into them one by one.
gzip and gunzip
gzip compress the size of the given files using Lempel-Ziv coding (LZ77). Whenever possible, each file is replaced by one with the extension .gz.Compression using gzip
$ gzip filename.txt #1 $ gzip -c filename.txt > filename.txt.gz #2 $ gzip < filename.txt > filename.txt.gz #3 $ gzip -k filename.txt #4 |
Terminal Snippet: #1 Modifies the original file and #2,#3,#4 are three different ways of doing the same thing that is Keeping the original file and making another compressed file using gzip. You should look up the gzip man page for more options.
Side note: If you are from windows world, you might be like, "Hey! I would like to password protect my file along with the compression!" But here, a Unix-philosophy to this task will be applied: one tool for each task. Checkout this discussion on SuperUser for more on it.
You can use gzip to compress multiple files at the same time by listing them with a space between each one. Using,
$ gzip -r file1 file2 file3 /usr/home/sample_dir.tar |
Uncompressing using gunzip
Uncompressing is pretty smooth.In gunzip, The filename.gz compressed file is deleted and replaced with filename. The Syntax is as follows.$ gunzip filename.gz
TIP: There are different manual/man pages for gzip and gunzip , and you want to check both of them.
Sometimes you may come across compressed files with ".z" extension. These are compressed through the legacy compress utility, you'll need to use the uncompress command for that.
Now that You Know gzip, let's talk about the bzip2 compression utility.
bzip2 and bunzip2
Whenever I think bzip2, first question comes to my mind is "What about bzip1?" Here's a history lesson on unix stackexchange on it. :) Anyways, bzip2 compresses files using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding.Compression Using bzip2
$ bzip2 filename #1 $ bzip2 filename.bz2 file1 file2 file3 /tmp/example_dir.tar #2The examples are pretty much same as gzip, #1 is about compressing a single file with bzip2 and #2 is about compressing multiple files with bzip2.
Uncompressing Using bzip2
Uncompressing is also straight forward, the syntax is,$ bunzip2 archive4.tar.bz2
Now let's talk about compressing with zip in linux,
zip and unzip
Cross-platform file archiving and compression utility compatible with Windows. "Zipped" archives seem to be a more common medium of file exchange on the Internet than "tarballs." Syntax for zip and unzip goes like,$ zip -r filename.zip filesdir $ unzip filename.zip $ zip -r filename.zip file1 file2 file3 /usr/work/fun
Now Let's talk about archiving with popular archiving utility tar.
Archiving Using Tar in Linux
The standard UNIX archiving utility. [1] Originally a Tape Archiving program, it has developed into a general purpose package that can handle all manner of archiving. GNU tar has been patched to accept various compression filters, for example: tar czvf archive_name.tar.gz *, which recursively archives and gzips all files in a directory tree except dotfiles in the current working directory. Here are some popular options used with tar,- -c create (a new archive)
- -x extract (files from existing archive)
- -r append (files to existing archive)
- -A append (tar files to existing archive)
- -t list (contents of existing archive)
- -u update archive
- -z gzip the archive
- -j bzip2 the archive
(compress or uncompress, depending on whether combined with the -c or -x)
Here's the syntax for tar,
$ tar -cvf archive1.tar test-dir #1 $ tar -tvf /tmp/archive1.tar #2 $ tar -xvf /tmp/archive1.tar #3 $ tar -cvzf archive3.tar.gz test-dir #4 $ tar -xvzf /tmp/archive3.tar.gz #5 $ tar -cvjf archive4.tar.bz2 test-dir #6 $ tar -xvjf /tmp/archive4.tar.bz2 #7
#1 is for creating an archive
#2 is for listing an archive
#3 is for extracting an archive
#4 is for creating an archive followed by a gzip compression
#5 is for uncompressing a gzip file followed by a extracting process
#6 is for creating an archive followed by bz2 compression.
#7 is for uncompressing a bz2 file followed by a extracting process
Hope This Post Was Helpful.


An operating system is the most important software that runs on a computer(UNIX Training in Chennai). It manages the computer's memory, processes, and all of its software and hardware. Using a highly secured and effective operating systems are always wanted by the consumers(Unix Shell Scripting Training in Chennai). You hae said that crystal clear in your content above. Thanks for sharing this in here.
ReplyDeleteUNIX Training in Chennai | UNIX Certification