RAR files, a common compressed file format, are widely used to store and share large amounts of data efficiently. While Linux natively supports various compression formats like ZIP and TAR, RAR remains one of the most popular tools for creating and extracting compressed archive (.rar) files.

When you download an archive file from the web, you’ll need a RAR tool to extract it. RAR is available freely under Windows operating systems to handle compressed files, but unfortunately, the RAR tool isn’t pre-installed on Linux systems.

In this article, we’ll guide you through the process of installing unrar and rar command-line tools to open, extract, uncompress, or unrar and create archive files on a Linux system.

Install rar and unrar on Linux

To work with RAR files on Linux, you’ll need the rar and unrar command-line utilities, which allow you to create and extract content from RAR archives.

To install rar and unrar, open a terminal and use the default package manager specific to your Linux distribution.

Install on Debian and Ubuntu

For example, on Debian and Ubuntu-based distributions, you can easily install the rar and unrar packages using the apt-get or apt program as shown.

sudo apt install rar unrar

Install on RHEL-based Distributions

If you are using RHEL-based distributions, you can use the dnf command or yum command to install it.

------------ On Fedora Linux ------------ 
sudo dnf install rar unrar        

------------ On RHEL-based Linux ------------
sudo yum install epel-release
sudo yum install rar unrar

Install on Other Linux Distributions

On other popular Linux distributions, you can install it using your default package manager as shown.

sudo emerge -a rar unrar          [On Gentoo Linux]
sudo apk add rar unrar            [On Alpine Linux]
sudo pacman -S rar unrar          [On Arch Linux]
sudo zypper install rar unrar     [On OpenSUSE]    

Manual Installation from Source

If your distribution does not offer rar and unrar packages, you need to download the latest unrar/rar file and install it using the following commands.

cd /tmp
wget https://www.rarlab.com/rar/rarlinux-x64-720.tar.gz
tar -zxvf rarlinux-x64-720.tar.gz
cd rar
sudo cp -v rar unrar /usr/local/bin/

Understanding rar vs unrar

The rar and unrar commands serve different purposes:

  • rar – Creates, modifies, and manages RAR archives (create, add, delete, lock, repair).
  • unrar – Extracts, lists, and tests RAR archives (extract, list, test integrity).

Think of rar as the full-featured tool for archive management, while unrar is specifically designed for extraction and viewing.

How to Create RAR File in Linux

To create an RAR archive file in Linux, run the following command with a option, which will create an archive file for a tecmint directory.

rar a tecmint.rar tecmint
Creating RAR File in Linux
Creating RAR File in Linux

Create RAR with Maximum Compression

To create a RAR archive with maximum compression, use the -m5 option (compression levels range from -m0 to –m5):

rar a -m5 tecmint.rar tecmint

This will take longer but produce a smaller archive file.

Create RAR While Excluding Files

To create a RAR archive while excluding specific files or patterns, use the -x option:

rar a -x*.log -x*.tmp tecmint.rar tecmint

This command will exclude all .log and .tmp files from the archive.

How to Extract RAR Files in Linux

Once you have unrar installed, you can easily open or extract the contents of a RAR file in the current working directory by using the following command with the e option.

unrar e tecmint.rar
Extracting RAR Files
Extracting RAR Files

How to Extract RAR File to a Specific Directory

To open/extract a RAR file in a specific path or destination directory, use the e option, it will extract all the files in the specified destination directory.

unrar e tecmint.rar /home/tecmint/rarfiles
Extracting RAR Files to Directory
Extracting RAR Files to the Directory

How to Extract RAR File with a Directory Structure

To open/extract an RAR file with its original directory structure, just issue the command below with the x option, which will extract according to its folder structure. See the output of the command below.

unrar x tecmint.rar
Extracting RAR Files with Directory Structure
Extracting RAR Files with Directory Structure

How to Extract Multi-Part RAR Archives

If you have a multi-part RAR archive (e.g., archive.part1.rar, archive.part2.rar, archive.part3.rar), simply extract the first part and unrar will automatically process all remaining parts:

unrar x archive.part1.rar

Make sure all parts are in the same directory before extracting.

How to List RAR Files in Linux

To list the contents of an RAR file in Linux, you can use the unrar l command, which will display the list of files with their sizes, dates, times, and permissions.

unrar l tecmint.rar
Listing Content of RAR Files
Listing Content of RAR Files

How to Check the Integrity of RAR File in Linux

To check the integrity of an RAR archive file, you can use the unrar t command, which will perform a complete integrity check for each file for errors and display the status of the file.

unrar t tecmint.rar
Testing RAR Files
Testing RAR Files

The unrar command only extracts, lists, or tests archive files. It has no option for creating RAR files under Linux. So, here we need to install the RAR command-line utility to create archive files.

How to Delete Files in RAR Archive

The rar d command is used to delete files from an existing RAR archive in Linux. The d option directly modifies the existing RAR archive by removing the specified files.

rar d tecmint.rar randfile001 randfile002 

In the above command, the randfile001 and randfile002 files will be deleted from the tecmint.rar RAR archive.

Delete Files in RAR Archive
Delete Files in the RAR Archive

How to Repair RAR Files in Linux

The rar r command is used to repair and recover data from damaged or corrupted RAR archives in Linux.

rar r tecmint.rar
Repair RAR Archive
Repair RAR Archive

This command will create recovery records and attempt to reconstruct damaged portions of the archive.

How to Add Files to RAR Archive

To update or add files to the existing archive file, use the rar u command, which allows you to add files to an existing RAR archive or update files within the archive.

rar u tecmint.rar hello.py

Now, verify that the file hello.py is added to the archive file:

rar l tecmint.rar
Add Files to RAR Archive
Add Files to the RAR Archive

How to Set a Password for a RAR File

This is a very interesting feature of the rar tool, which allows us to set a password to the RAR archive file using the following command.

rar a -p tecmint.rar
Set Password to RAR File
Set Password to RAR File

Now verify it by extracting the archive file and see whether it will prompt us to enter the password that we have set above.

unrar x tecmint.rar
Extract Password Protected RAR File
Extract Password-Protected RAR File

How to Lock RAR File

The rar k command is used to lock an existing RAR archive file, which is useful if you want to prevent further modifications to the archive.

rar k tecmint.rar
Lock RAR File
Lock RAR File

How to Split a RAR Archive

To split a RAR archive into 50MB parts or segments, use the following command with the -v50M option, which will split the RAR file into four parts.

rar a -v50M archive_name.part.rar file1 file2 directory

Make sure to replace “archive_name.part.rar” with your desired archive name and size with the desired size (e.g., 50M or 100M). Include the files or directories you want to compress.

RAR Command Quick Reference

Here’s a quick reference table for the most commonly used RAR and unrar commands:

Command Option Description
rar a Create a new RAR archive
rar a -m5 Create archive with maximum compression
rar a -p Create password-protected archive
rar a -v50M Create split archive (50MB parts)
rar u Update or add files to existing archive
rar d Delete files from archive
rar r Repair damaged archive
rar k Lock archive (prevent modifications)
rar l List contents of archive
unrar e Extract files to current directory
unrar x Extract with full directory structure
unrar l List archive contents
unrar t Test archive integrity
Conclusion

For more RAR and Unrar options and usage, run the following command, which will display a list of options with their description.

man unrar
man rar

We have presented almost all the options above for the rar and unrar commands with their examples. If you believe we’ve missed anything on this list and would like us to add it, please use the comment form below.

Similar Posts