File compression and decompression is the one of the most common task. The main goal of this post is to demonstrate how to work with compression commands in Linux terminal (console). Therefore, four commands will be discussed. In fact, more commands are available for doing compression and decompression but Tar, Gzip, Rar and Unrar commands can do almost all types of compression which is discussed in the next sections.
Tar
The first and basic command for compressing files or extracting the content of compressed files is tar command. It is very powerful command and genuinely supports three common compression extensions which are .tar
, .tar.gz
, .tar.bz2
. Recently, the support for tar.xz
is added, for that see this article.
For extracting a .tar
archive, use the following command,
$ tar -xf tar_file_name.tar
In order to see the extracted files, add v
switch after -xf
. For extracting files in a specific directory, use -C
switch like below,
$ tar -xvf tar_file_name.tar -C ~/directory_name
If you want to compress files, use the following command,
$ tar -cf tar_file_name.tar file_names
For knowing which files are compressed use v
after -c
like before.
As mentioned earlier tar
command supports .tar.gz
files extension as well. Now for that, you can use it like this,
$ tar -xzf tar_gz_file_name.tar.gz
As you can see above command just has one more switch in comparison with the command that was utilized to extract .tar
files.
The other switches that are used to display extracted files and extract them in a specific directory are same as .tar
.
For creating a .tar.gz
archive,
$ tar -czf tar_gz_file_name.tar.gz file_names
For extracting files from .tar.bz2
archive just replace z
switch with j
,
$ tar -xjf tar_bz2_file_name.tar.bz2
The rest of the parameters are the same as .tar
and .gz
.
In order to make .tar.bz2
use the following command,
$ tar -cjf tar_bz2_file_name.tar.bz2 file_names
To view the content of a .tar
or .tar.gz
or .tar.bz2
file use this command,
$ tar -tvf tar_file_name.tar
Zip/Unzip
Unzip command is used to extract files from “.zip” archive and zip
command is used to create .zip
archives.
To extract files from the archive use,
$ unzip zip_file_name.zip
For extracting files into specific directory add -d
switch like below,
$ unzip zip_file_name.zip -d ~/directory_name
In order to see the contents of the extracted files (use for text files only) add -c
switch before the name of zip file.
With zip
command you are can also create .zip
archives like below,
$ zip -r zip_file_name.zip file_names
Rar/Unrar
Undoubtedly, rar compression algorithm is one the best and the most used compression type. Fortunately, this compression algorithm is fully supported in Linux with either the official utility from Winrar or the counterparts.
To extract files from a .rar
archive run,
$ unrar e rar_file_name.rar
Keep in your mind that e
switch does not have dash -
.
To list an archive contents use l
switch like below,
$ unrar l rar_file_name.rar
Additionally, for testing the health of your archive file use t
switch.
For printing the content of files (use for text files only) use p
switch such as below,
$ unrar p rar_file_name.rar
To extract files into a specific directory, use the command like this,
$ unrar x rar_file_name.rar ~/directory_name
Now, for creating .rar
archive you need to use the rar
command like below,
$ rar a rar_file_name.rar file_names
For making your rar file password protected add the following switch,
$ rar a rar_file_name.rar file_names -p
After entering the above command, you will be prompted and asked to enter the password twice to confirm it is typo free.
The rar
command can also be used instead of unrar
command, therefore, having installing rar
command is enough and cater for both purposes.
More information can be found in the man pages of the commands or via online man pages.