Today I am going to share an article that deals with Linux archive, compression/decompression with live examples. I have included Tar command, zip compression/decompression, gzip compression/decompression, Bzip2 compression/decompression and the combination of tar command with gzip & bzip2.
Archive, compression, decompression in Linux
Let’s start with Linux archive first. I have made it simple with examples.
How to use TAR COMMAND for Archive in Linux.
Tar is important Linux archive command which is used for archiving multiple files. First of all check all available files with ls-l command.
As you see in above screenshot there are some files with .docx extension. Now we will combine all .docx file with tar command
#tar cvf all.tar *.docx
Output ==> All the .docx files are archived into a new file all.tar
Now check it with command: ls -l
How to Archive files with different extensions with Tar command.
I am going to archive all .docx & .pdf files into a new file all.tar.
#tar cvf all.tar *.pdf *.docx
here * indicates all files are included within the extension.
Check it with ls -l command.
How to view the content of an archive file in Linux.
Now let’s view the content of archived file as mentioned below:
#tar tvf all.tar
tvf option is used to to view the content of a archive file
How to extract the archive file in Linux
Let’s extract the archived file with Tar command using option xvf:
#tar xvf all.tar
xvf option is used to extract the archive file.
Related : SSH server configuration in Rhel5/Rhel6.
File Compression & Decompression in Linux
There are many ways to compress/decompress a file in Linux. Today I will discuss ZIP, GZIP, BZIP2 compression/decompression in Linux. I will also cover the combination of a TAR archive with ZIP, GZIP, BZIP2.
Let’s first learn Zip compression/decompression in Linux:
ZIP COMPRESSION/DECOMPRESSION IN LINUX
Zip is well known compression tool having a good compression rate.
How to compress files using zip compression?
First of all check available files with ls command. Now as you see in the output of ls command there are some files with .docx extension. Let’s compress all .docx files with zip command.
syntax: #zip file-name
#zip all.zip *.docx
here all.zip is the compressed zip file.
How to compress multiple files with zip:
#zip xyz.zip a1.docx a2.docx a3.docx
All the files are compressed into a single xyz.zip file.
How to view the content of compressed zip file.
We have to use zmore or zless command to view the content of a compressed file.
How to decompress a file using zip?
It’s very easy to decompress a file with zip. You just have to use unzip command with the compressed file name.
Related: configure Postfix Server in Linux ?
GZIP COMPRESSION/DECOMPRESSION IN LINUX
The compression rate of the zip is not so good. Here the gzip tool comes. The compression rate of Gzip is high than Zip.
How to compress file using gzip–
syntax: gzip file-name
With this command a1.docx file will be compressed and the output file is a1.docx.gz. In gzip compression the extension of the output file is .gz.
How to compress multiple files with gzip
First list available files with ls command. Now we use gzip command to compress all .docx files.
Now check the output with ls command. Above you will see that all the docx files are compressed into gzip format.
How to compress files with different extension using gzip command.
Let’s compress a .docx and a pdf file at once using gzip compression.
#gzip a1.docx a2.pdf
How to decompress a file using gzip
We have to use the gunzip command to decompress a gzip compressed file.
Alternate method :
#gzip -d a1.docx.gz
here -d option is used to decompress a .gz file.
Related: SQUID SERVER IN LINUX.
BZIP2 COMPRESSION/DECOMPRESSION IN LINUX
Compression rate of Bzip2 tool is more than gzip and zip. It’s a better tool to compress a file in an efficient manner.
How to compress a file using bzip2 tool.
With above command a2.docx file will be compressed and the output file is a2.docx.bz2. In bzip2 compression, the extension of the compressed file is .bz2.
How to compress multiple files using bzip2 command.
here all .docx files are compressed.
How to compress files with different extensions with bzip2.
Let’s compress both docx and pdf files at once with bzip2 command.
#bzip2 a1.docx a2.pdf
How to decompress a compressed file with bzip2.
#bzip2 -d a3.docx.bz2
Related: User and group management.
TAR AND GZIP COMBINATION IN LINUX
How to compress & archive files with the combination of tar and gzip command.
It’s a far better way to compress files in Linux. We are compressing a file plus also making it an archive at the same time. We can compress & archive files at once using the combination of tar and gzip command. The output extension of file with this type of compression is (.tgz) or (.tar.gz)
#tar -zcvf A.tgz *.docx
#tar -zcvf A.tar.gz *.docx
All the docx files are compressed and archived into A.tgz file.
How to view the content of a file with .tgz extension.
#tar -tvzf A.tgz
-tvzp option is used to view the content of a .tgz file.
How to extract the tar+gzip (.tgz file) file in Linux.
We can easily extract a .tgz file to get back all archived & compressed files into their original form.
#tar -xvzf A.tgz
-x option is used to extract a .tgz file
Related: Telnet server guide.
TAR AND BZIP2 COMBINATION IN LINUX
As we know the compression rate of bzip2 is far better than zip and gzip. So we can use bzip2 compression tool with tar command to make use of it high compression rate. The further advantage is that our compressed file is directly archived with Tar command.
How to compress files with the combination of tar and bzip2.
We can compress & archive files at once using the combination of tar and bzip2 command. The output extension of the file with this type of compression is (.tbz2) or (.tar.bz2). Let’s compress all docx files:
#tar -jcvf C.tbz2 *.docx
-jcvf option is used to compress & archive a file.
With this command, all Docx files will be compressed & archived into C.tbz2 file.
How to view the content of tar+bzip2 file or (.tbz2) extension file–
#tar -tvjf C.tbz2
-tvjf option is used to view the content.
How to extract tar+bzip2 (.tbz2) file in Linux.
We can easily extract a .tbz2 file to get back all archived & compressed files into their original form.
#tar -xvjf C.tbz2
-xvjf option is used to extract a .tbz2 file.
So finally we have learned Linux archive, zip compression/decompression. gzip compression/decompression & the combination of tar command with gzip and bzip2. I have tried my best to make it simple and easy to understand. Hope you like it.
Do share this article on Facebook, twitter & other popular social networking sites. Follow us on google+ and be updated with latest updates.
If you have any queries or doubts regarding this article, place your precious comments in the comment box.