Creating and Extracting RAR Files on Linux: A Step-by-Step Guide

Creating and Extracting RAR Files on Linux: A Step-by-Step Guide
Create Extract And List Rar Files Linux Feature Image

RAR files are a widely used format on Windows, facilitating the effective management and sharing of large files across different platforms. While Linux typically favors the TAR format, handling RAR files is straightforward. So, how can you extract a RAR file or share it with a Windows user directly from the Linux terminal?

This article outlines the steps for creating RAR files and extracting archives using the unrar command on Linux.

How to Install Rar and Unrar on Linux

To manage RAR files on Linux, you need two essential tools: rar and unrar. Installation is straightforward and varies based on your Linux distribution.

For Debian-based distributions such as Ubuntu or its derivatives, you can install both commands by using:

Installation Of rar and unrar command tool in Ubuntu terminal.

For Arch Linux users, execute:

If you are on a Red Hat-based distribution (like RHEL, CentOS, Fedora), first enable the EPEL repository and then install the tools using:

Alternatively, for those who prefer building RAR from the source, you can download TAR files from the official RAR website. Once installation is complete, you will be ready to create and manage RAR files on your Linux system.

Creating a RAR File on Linux

To create a RAR file, you first need some files or directories to compress and archive. For demonstration, let’s create a few sample text files.

Begin by creating text files using the touch command:

Next, you can include these files in a RAR archive by using the rar command:

Adding files to RAR archive using the rar command in terminal.

Note the use of the a flag, which adds the specified files or directories to the archive. You can set the RAR file name followed by the files you want to archive.

After creating the archive, verify its existence by running the ls command to display the current directory’s contents:

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

Extracting RAR Files on Linux

To extract a RAR file on Linux, use the unrar command followed by the e flag, which instructs the unrar command to extract the archive’s content into the current directory.

Use the following command to extract the RAR file created earlier:

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

Extracting RAR Files to a Specific Directory

If you want to extract files from a RAR archive into a specific directory, adjust the unrar command as follows:

Extracting files from a rar archive into specified directory.

In this modified command, the -d option specifies the destination directory. Moreover, you can set the output location of the extracted files.

When using the unrar command, if the archive has multiple directories, the e option displays a flat list of files without showing the directory structure. To see the directories and their contents separately, use the x option instead.

For instance, to extract while displaying the directory structure, use:

Extracting and displaying directory using the unrar command.

This command will list the files within their respective directories in the terminal, providing a clearer overview of the archive’s structure.

Listing RAR Files on Linux

When you receive RAR files from untrusted sources, you may want to ensure their safety before extraction. In such circumstances, you can utilize the listing feature of the unrar command to inspect the contents without extracting the archive.

Using the l flag, you can view the file structure and metadata of the RAR archive. For example, to check the contents of test.rar, execute the following command:

Examine the contents using the unrar command with l flag.

This command displays detailed information such as file sizes, permissions, creation dates, and the total number of files within the archive.

Password Protecting Your RAR Files

When sharing a RAR file, it is crucial to secure sensitive information against unauthorized access. You can achieve this by setting a password or locking the file to prevent modifications.

RAR files typically employ AES-256 encryption for password protection, which is a highly secure encryption standard, making it difficult to crack without the correct password.

To set a password for your RAR file, execute:

Setting a password using rar with a and p flag.

This way, anyone attempting to access your archive will need the password.

Additionally, to prevent others from altering the RAR file, you can lock it using the k option:

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

Locking a RAR file prevents modifications but does not restrict access or extraction. To completely prevent access, you must use password protection. A locked file merely ensures that its contents cannot be altered.

Conclusion

Whether you are extracting files or creating secure, password-protected archives, the rar and unrar commands provide comprehensive control over your data. Various online tools are also available to assist in extracting RAR files.

Image credit: Unsplash. All alterations and screenshots courtesy of Haroon Javed.

Source

Leave a Reply

Your email address will not be published. Required fields are marked *