Compress file or directory using RAR archive tool on Linux shell

Here is a quick config tip on how to compress and extract files using RAR archive utility. First let’s see how we can compress directory using RAR. In our example we have a directory called my_files containing five files:

$ mkdir my_files
$ touch my_files/file{1..5}
$ ls my_files/
file1  file2  file3  file4  file5

To compress entire directory using RAR archive tool we use rar’s a command. The below command will create a RAR archive called my_files.rar containing all five above files:

$ rar a my_files.rar my_files/

Creating archive my_files.rar

Adding    my_files/file5                                              OK 
Adding    my_files/file4                                              OK 
Adding    my_files/file3                                              OK 
Adding    my_files/file2                                              OK 
Adding    my_files/file1                                              OK 
Done
$ ls -l my_files.rar 
-rw-rw-r--. 1 lrendek lrendek 307 Nov  3 06:55 my_files.rar



The syntax is same even when we need to compress only single file. In the next example we will create a RAR archive containing only a single file, file1 called file.rar:

$ rar a file.rar my_files/file1 

Creating archive file.rar

Adding    my_files/file1                                              OK 
Done
$ ls -l file.rar 
-rw-rw-r--. 1 lrendek lrendek 83 Nov  3 06:58 file.rar

In to following example we again compress entire directory my_files but we instruct RAR to use maximum compression:



rar a my_files.rar -m5 my_files/

To list the contents of RAR archive use l command. For example to see what is inside of my_files.rar archive run:

$ rar l my_files.rar 

Archive my_files.rar

 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 file5               0        8   0% 03-11-14 06:52 -rw-rw-r-- 00000000 m5b 2.9
 file4               0        8   0% 03-11-14 06:52 -rw-rw-r-- 00000000 m5b 2.9
 file3               0        8   0% 03-11-14 06:52 -rw-rw-r-- 00000000 m5b 2.9
 file2               0        8   0% 03-11-14 06:52 -rw-rw-r-- 00000000 m5b 2.9
 file1               0        8   0% 03-11-14 06:52 -rw-rw-r-- 00000000 m5b 2.9
-------------------------------------------------------------------------------
    5                0       40   0%

Now that we know how to archive files we can see how to extract files. In the next example we extract all files from my_files.rar archive:

$ unrar x my_files.rar
OR
$ rar x my_files.rar 

Extracting from my_files.rar

Creating    my_files                                                  OK
Extracting  my_files/file5                                            OK 
Extracting  my_files/file4                                            OK 
Extracting  my_files/file3                                            OK 
Extracting  my_files/file2                                            OK 
Extracting  my_files/file1                                            OK 
All OK

To extract only a single file from RAR archive you can specify the file ant its path as an rar argument. Next, example will extract on single file, file3:

$ unrar x my_files.rar my_files/file3
OR
$ rar x my_files.rar my_files/file3

Extracting from my_files.rar

Creating    my_files                                                  OK
Extracting  my_files/file3                                            OK 
All OK


Comments and Discussions
Linux Forum