1. Overview

RAR files are a popular format for compressing and archiving files. The acronym RAR stands for Roshal Archive. Despite being a proprietary format, there are several reasons for opting for the default RAR utility file type over others:

  • uses lossless data compression technique
  • supports splitting big archives into smaller volumes simplifying storage and sharing
  • facilitates archive comments and customizable compression settings

While RAR exhibits slower performance compared to ZIP, it has a higher compression rate and superior data redundancy. Thus, RAR files save more storage space on average.

In this tutorial, we’ll see how to create and extract RAR files on Linux.

2. Installing rar and unrar

Before we move on to creating and extracting RAR files, let’s install the rar and unrar command-line utilities.

Staying with the official rar and unrar applications ensures regular upgrades and compatibility for proprietary RAR archives. Still, we can opt to use other software for extraction but not creation.

They are both available in most Linux distributions’ repositories. For example, to install them on a Ubuntu 22.04 system, we use the apt command:

$ sudo apt install rar unrar

The sudo command ensures we have enough privileges for the installation task.

Once UnRAR is installed, we can use the unrar command for extracting files.

Since rar is a proprietary program, we’ll only get a trial version. The trial version requires registration after 40 days. However, with trial software like RAR, we get several advantages:

  • the software can be tested thoroughly during this period
  • for temporary usage, it’s more affordable than a full license software
  • user can make informed decisions when buying the paid version

Once the installation is done, we can see how to create and extract RAR archives.

3. Creating RAR Files

We can fairly easily produce RAR files on Linux by compressing one or more files into a single RAR file.

3.1. Basic Syntax

The rar command follows a general syntax for compressing files:

$ rar <option> <archive_name> <file1 file2...fileN>

Let’s understand the fields used above:

  • <option>: specifies the commands and switches for different file operations
  • <archive_name>: name of the output file
  • <file1 file2…fileN>: list of the files to compress

There are many options available with the rar command. All of them are also available in the output of rar without any options:

$ rar
           Type 'rar -?' for help
Usage:     rar  - -  
               <@listfiles...> <path_to_extract\>
  a             Add files to archive
  c             Add archive comment
  ch            Change archive parameters
  cw            Write archive comment to file
  d             Delete files from archive
  e             Extract files without archived paths

Let’s see some usage of the rar command.

3.2. Simple RAR File

Let’s now create a RAR file, myfile.rar, that contains the files file1.txt, file2.txt, file3.txt, and file4.txt:

$ rar a myfile.rar file1.txt file2.txt file3.txt file4.txt
Creating archive myfile.rar
Adding    file1.txt                                                  OK 
Adding    file2.txt                                                  OK 
Adding    file3.txt                                                  OK 
Adding    file4.txt                                                  OK 

The -a option adds files to the RAR file. Further, we can include directories with or without files using the -r recursive option:

$ rar a -r myfile.rar file1.txt ~/Desktop
Updating archive myfile.rar
Adding  ~/Desktop/ttt.txt                                             OK 
Adding  ~/Desktop/ooo.txt                                             OK 
Adding  ~/Desktop/mmm                                                 OK 
Adding  ~/Desktop/kkk                                                 OK 
Adding    file1.txt                                                   OK 

As a result, all the objects below the directory are also compressed.

3.3. Splitting Archives

When they have a bigger size, we can split archives into multiple parts or volumes using the -v<size> option.

For example, let’s create multiple RAR files of 50MB size:

$ rar a -v50M myfile.rar Book.pdf file.txt* Ubuntu.iso
Creating archive myfile.rar
Adding    Book.pdf     
Creating archive myfile.part2.rar
...       Book.pdf  OK 
Adding    file.txt                                                  OK 
Adding    Ubuntu.iso                                         
Creating archive myfile.part3.rar
...       Ubuntu.iso                                         
Creating archive myfile.part4.rar
...       Ubuntu.iso                                      OK 

The -v50M option split our RAR file into four parts: myfile.part1.rar, myfile.part2.rar, myfile.part3.rar, myfile.part4.rar, each being 50MB.

3.4. RAR File With Password

Moreover, we can set a password for our RAR file with the -p option:

$ rar a -p myfile.rar file1.txt file2.txt
Enter password (will not be echoed): 
Reenter password: 
Creating archive myfile.rar
Adding    file1.txt                                                  OK 
Adding    file2.txt                                                  OK 

This command prompts us for a password. As a result, the files are compressed and protected with the entered password.

3.5. RAR File With Encrypted Files

Furthermore, we can also add encryption to our archived files. To do this, we use the -hp option:

$ rar a -hp myfile.rar file1.txt file2.txt
Enter password (will not be echoed): 
Reenter password:
Adding    test 1.txt                                                  OK 
Adding    test 2.txt                                                  OK 

In this case, both the file data and the headers are encrypted.

4. Extracting RAR Files

Extracting a RAR file is very similar to creating it. Unlike the limited software that produces them, there are many programs available to extract RAR files. For example, we can use WinZip, 7-Zip, WinRAR from RARLab, and others.

In this case, we’ll use the classic unrar.

4.1. Extracting RAR File to Current Directory

When we want all the files in the archive to be extracted to the same directory where the archive is placed, we use the e subcommand. This won’t preserve the original directory layout but instead will dump all objects in a flat structure.

Let’s take the example of the -e option:

$ unrar e myfile.rar
Extracting  file1.txt                                                OK 
Extracting  file2.txt                                                OK 
Extracting  file3.txt                                                OK 
All OK

This command moves all files from the RAR file to the current location without recreating the subdirectories but still extracting their contents.

4.2. Extracting RAR File With Full Path

On the other hand, we can use the x subcommand to preserve the full path of the files inside the RAR file:

$ unrar x myfile.rar
Extracting  test/test4.txt                                           OK 
Creating    test/AAA                                                  OK
Creating    test/AAA/mmm                                              OK
Extracting  test/AAA/mmm/ttt.txt 
All OK

Consequently, the files are extracted to the current directory within their original tree structure.

4.3. Extracting RAR File to a Specific Directory

Let’s see how to extract a RAR file to a specific directory with the -o option:

$ unrar e <filename.rar> -o <path_to_directory>

For example, we’ll extract the contents of the RAR file myfiles.rar to the directory ~/Downloads:

$ unrar e myfile.rar -o ~/Downloads
Extracting  ~/Downloads/sample.txt                                         OK 
Extracting  ~/Downloads/test 2.txt                                         OK 
Extracting  ~/Downloads/test 1.txt                                         OK 
All OK 

This places the extracted files in the directory we specified.

4.4. Extracting a Password-Protected RAR File

If the RAR file is password-protected, we need to specify the password when extracting it. To do this, we use the -p option:

$ unrar e myfiles.rar -p<password>

We can either put the password for our file in the placeholder <password> or enter it on the terminal:

$ unrar e myfile.rar -p123
Extracting from myfile.rar
Extracting  file1.txt                                          OK 
Extracting  file2.txt                                          OK 
All OK

Consequently, we successfully extract our file.

5. Testing the Integrity of a RAR File

Importantly, we can use the unrar t command to test the integrity of a RAR file. This command checks the file for errors.

For example, let’s test the integrity of our file myfiles.rar:

$ unrar t myfiles.rar
Testing archive myfile.rar
Testing     file1.txt                                                 OK 
Testing     file2.txt                                                 OK 
All OK

This way, we check whether everything is as expected with the format of a given file or if there is corruption.

6. Conclusion

In this article, we looked at how to create and extract RAR files in Linux using the rar and unrar utilities.

First, we started off by installing both tools. Then, we looked at the use of rar to create a plain RAR archive, a password-protected RAR archive, and a RAR archive with encrypted files. Next, we studied RAR archive extraction with unrar. Finally, we saw a method to test the integrity of a RAR.

Comments are closed on this article!