File compression, decompression is the one of the most common and crucial task in every computer. 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 will be discussed in the next section.
The first and basic command for compressing files or extracting the content of compressed file is tar command. It is very powerful command and genuinely supports three common compression extensions which are
.tar archive type the following command,
$ tar -xf MyFile.tar
In order to see the extracted files add
v switch after
For extracting files in specific directory, use
-C switch like below,
$ tar -xvf MyFile.tar -C /home/kasra/comp
If you want to compress files use following command,
$ tar -cf YourCompressFileName.tar FileNames
For knowing which files are compressed use
-c like before.
As mentioned earlier
tar command supports
.tar.gz files extension as well. Now for extracting mentioned type, you can use below command,
$ tar -xzf MyFile.tar.gz
As you can see above command just has one more switch in comparison with the command that was utilized to extract
The other switches that are used to display extracted files and extract files in the specific directory are same as
.tar.gz archive look at example,
$ tar -czf YourCompressFileName.tar.gz FileNames
For extracting files from
.tar.bz2 archive just replace
z switch with
j like following,
$ tar -xjf MyFile.tar.bz2
The rest of the parameters are the same as mentioned parts.
In order to make
.tar.bz2 use following command,
$ tar -cjf YourCompressFileName.tar.bz2 FileNames
To view the content of a
.tar.bz2 file use this command,
$ tar -tvf FileName.tar
Unzip command is used to extract files from “.zip” archive and
zip command is used to create
For extracting files from the archive type below command,
$ unzip ZipFile.zip
For extracting files into specific directory add
-d switch like below,
$ unzip ZipFile.zip -d /home/kasra/
In order to see the contain of extracted files (use for text files only) add
-c switch before the name of zip file.
zip command you are able to create
.zip archive file like below,
$ zip -r YourZipFileName.zip FileNames
Undoubtedly, rar compression algorithm is one the best and most used compression types. Fortunately, this compression algorithm is fully supported in Linux with either use official applications from Winrar company or Open Source version of it.
For extracting files from
.rar archive use following command,
$ unrar e YourRarFile.rar
Keep in your mind that
e switch does not have dash
For listing archive content use
l switch like below,
$ unrar l YouRarFile.rar
Additionally, for testing the health of your archive file use
For printing the content of files (use for text files only) use
p switch such as below,
$ unrar p YourRarFile.rar
For extracting files into specific directory use flowing switch with the command,
$ unrar x YouRarFile.rar [/home/kasra](YourPath)
Now, for creating
.rar archive you need to use
rar command like below,
$ rar a MyArchive.rar FileNames
For making your rar file password protected add following switch,
$ rar a MyArchive.rar FileNames -p
After entering above command, the system prompt you and ask about your password twice since your password is not shown.
Rar command also can be used instead of
unrar command, therefore, having installing
rar command is enough and satisfy both compression and decompression purposes.
More information can be found in the man pages of the commands or following online man pages.
For tar click here.
Man page of Zip and Unzip click here and here.
Unrar and rar manual pages are available at here and here.