Rar for Beginners : How to use Rar utility to Compress Files in Linux/Unix Based Systems

Rar is a command line tool to compress files and directories. It is the Linux port of the command line version of WinRAR. RAR is a proprietary archive file format that supports data compression, error recovery and file spanning. It was developed by a Russian software engineer, Eugene Roshal  and the RAR software is licensed by win.rar GmbH. The name RAR stands for Roshal Archive. The filename extensions used by RAR is .rar.

Now at this post we are going to see some basic examples of how to use rar command to compress and/or decompress files :

To install rar on your system use the below commands :
 $ sudo apt-get update
 $ sudo apt-get install rar unrar
The unrar is used to decompress rar file.

1. Compress a file

with 'a' option we can compress a file in rar :
 $ rar a [compressed-file-name] [file-or-directory-name]
For example, compressing a file :
 $ rar a compressed.rar document.txt
Compressing a folder :
 $ rar a compressed.rar test/
2. Compress Multiple files and folders :

To compress multiple files and folders at once just provide the file or folder names
 $ rar a compressed.rar [file_name1] [file_name2] [file_name3]
3. Compressing files/folders with password protection :

To compress files or folders with password protection, we need to provide '-p' option :
 $ rar a -p compressed.rar [file-name]
Example :
 $ rar a -p compressed.rar document.txt
When you run the above command, it will ask you to enter password twice, then it will create a password protected rar file.

4. Extract/Decompress files/folders from rar :

With 'x' option, we can extract or decompress files from rar archive
 $ rar x [compressed-file-name]
example :
 $ rar x compressed.rar
To extract/decompress files/folders in current directory, use 'e' option :
 $ rar e [compressed-file-name]
example :
 $ rar e compressed.rar
To  extract/decompress files/folders in a particular directory
 $ rar e [compressed-file-name] [directory-path]
example :
 $ rar e compressed.rar /home/usr/documents
5. List all the contents of rar file :

To list all the contents of compressed rar file, use the below command :
 $ rar l [compressed-file-name]
example :
 $ rar l compressed.rar
6. Testing Rar file :

To test compressed rar for any error, use the below command :
 $ rar t [compressed-file-name]
example :
 $ rar t compressed.rar
7. Adding/Appending a file into compressed rar :

To add a new file into an rar archive, use the below command :
 $ rar u [compressed-file-name] [file-name-to-be-add]
example :
 $ rar u compressed.rar new_doc.txt
8. Delete a file from Rar archive

With 'd' option we can delete a file from an Rar archive :
 $ rar d [compressed-file-name] [file-name-to-be-delete]
examaple :
 $ rar d compressed.rar new_doc.txt
9. Recover or fix a broken rar file archive :

To recover or fix a broken rar archive, we need to use 'r' option :
 $ rar r [compressed-file-name]
There's also another utility called unrar which are use to un-archive or decompress rar files. We can decompress an rar file with below command :
 $ unrar x [compressed-file-name]

Conclusion :

As we know that the rar files were mostly used in windows environment. So if we need to use rar files, then rar utility will be very useful tool for this. To help menu in rar just type the rar command in terminal.