Using Tar, Zip, Unzip, Rar and Unrar commands in Linux

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.

Tar

The first and basic command for compressing files or extracting the content of compressed file is “tar” command. It is very powerful commands and genuinely it supports three common compression extensions which are “.tar”, “.tar.gz”, “.tar.bz2”.
For extracting “.tar” archive type the following command,

$ tar -xf MyFile.tar

In order to see the extracted files add ‘v’ switch after ‘-xf’.
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 -c FileNames -f YourCompressFileName.tar

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 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 “.tar” files.
The other switches that are used to display extracted files and extract files in the specific directory are same as “.tar”.
For creating “.tar.gz” archive look at example,

$ tar -cz FileNames -f YourCompressFileName.tar.gz

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 -cj FileNames -f YourCompressFileName.tar.bz2

Zip/Unzip

Unzip command is used to extract files from “.zip” archive and “zip” command is used to create “.zip” archive.
For extracting files from “.zip” archive type below command,

$ unzip ZipFile.zip

For extracting files into specific directory add ‘-d’ switch like below,

$ unzip ZipFile.zip -d /home/kixz/

In order to see the contain of extracted files (use for text files only) add ‘-c’ switch before the name of zip file.
With “zip” command you are able to create “.zip” archive file like below,

$ zip -r YourZipFileName.zip FileNames 

Rar/Unrar

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 ‘t’ switch.
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.

send your idea and information to kasra.madadipouya@geeksweb.tk

Leave a Reply