How to Create and Extract RAR Files on Linux

Create Extract And List Rar Files Linux Feature Image

RAR files are a popular format on Windows, allowing you to efficiently manage large files and share them across platforms. On Linux, though the TAR format is more commonly used, handling RAR files is just as simple. But what if you need to extract a RAR file or share it with a Windows user directly from the Linux terminal?

In this article, we’ll cover the steps for creating the RAR files and how to extract an archive using the unrar command on Linux.

Installing Rar and Unrar Commands on Linux

To handle RAR files on Linux, you need two key utilities: rar and unrar. Installing them is simple and depends on your specific Linux distribution.

For Debian-based distros like Ubuntu or its derivatives, you can install both by running:

sudo apt install rar unrar
Installation Of rar and unrar command tool in Ubuntu terminal.

For Arch Linux, run:

sudo pacman -S rar unrar

If you’re using Red Hat-based distribution (RHEL, CentOS, Fedora), first set up EPEL repository, then use:

sudo yum install epel-release
sudo yum install rar unrar

Alternatively, if you prefer building RAR from the source code, download its TAR files from the official RAR website. After the installation is finished, you’ll be all set to begin creating and working with RAR files on your Linux system.

Create a RAR File on Linux

To create a RAR file, first, you require some files or directories that can be compressed and archived. For the demonstration, let’s take some sample text files.

First, create the text files using the touch command:

touch file1.txt file2.txt file3.txt

Next, you can add these files to a RAR archive by following rar command:

rar a test.rar file1.txt file2.txt file3.txt
Adding files to RAR archive using the rar command in terminal.

One thing to note here is the use of the a flag. It will add all the files or directories to the archive using the rar command. Next, you can specify the RAR file name followed by the list of all the files you want to archive in a RAR file.

Afterward, you can use the ls command to display the contents of the current directory, which will help verify whether the RAR file was successfully created:

ls
Displaying the contents of the current directory using the ls command.

Extract RAR Files on Linux

To extract a RAR file on Linux, you can use the unrar command followed by the e flag. This flag tells the unrar command to extract the archive file content in the current directory.

Now use the unrar command to extract the RAR file we created earlier:

unrar e test.rar
Extracting and listing rar files using the unrar and ls command.

Extracting RAR File to a Specific Location

If you’d like to extract files from a RAR archive into a specific directory instead of the current one, you can modify the unrar command like this:

unrar e test.rar /home/linux/Extract
Extracting files from a rar archive into specified directory.

In this revised command, the -d describes the destination directory. Additionally, you can change the file location of the output extracted file.

Note: Before you extract the RAR file, first make sure the directory you specified already exists, else the command will not work.

When extracting files using the unrar command, if your archive contains multiple directories, the default e option will only display a flat list of files without showing the directory structure in the terminal. However, if you want to see the directories and their files listed separately, reflecting the proper directory structure, you can use the x option instead.

For example, to extract and display the directory structure in the terminal, use:

unrar x test.rar
Extracting and displaying directory using the unrar command.

This will list the files along with their corresponding directories in the terminal, making it easier to understand the archive’s structure.

List RAR Files on Linux

Sometimes, you may get RAR files from untrusted sources and want to ensure they are safe before extracting them. In such cases, you can use the listing feature of the unrar command to inspect the contents without extracting the archive.

By using the l flag, you can view the file structure and metadata of the RAR archive. For instance, to examine the contents of a file test.rar, run the following command:

unrar l test.rar
Examine the contents using the unrar command with l flag.

This will display detailed information such as file sizes, permissions, creation date and time, and the total number of files within the archive.

Password Protect Your RAR Files

While sharing a RAR file, it’s important to consider the security of your sensitive information to restrict it from unauthorized access. To protect your files, you can either set a password or lock the file to prevent any modifications.

RAR files typically use AES-256 encryption when protected by a password, which is a robust encryption standard. Also, AES-256 is highly secure, which makes it difficult to crack without the correct password.

To set a password, run:

rar a -p test.rar
Setting a password using rar with a and p flag.

Now, if anyone wants to access your archive, they need a password.

In addition to password protection, you can stop others from altering the RAR file by locking it. Use the k option for this:

rar k test.rar
Locking the rar file using the rar command with k option.

Locking a RAR file prevents modifications but does not block access or extraction. To restrict access entirely, you must use password protection. A locked file simply ensures that no one can alter the contents of the archive.

Wrapping up

Whether you’re extracting files or creating secure, password-protected archives, the rar and unrar commands provide full control over your data. There are also various online tools available that can be used to extract RAR files.

Image credit: Unsplash. All alterations and screenshots by Haroon Javed.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Haroon Javed Avatar