How to Extract and Create RAR Files.

How To Extract And Create RAR Files.

Usually, you’ll come across the .zip or .gz compressed files. However, every now and then, you’ll also encounter RAR files in Linux.

Dealing with RAR files in the Linux command line is not that complicated.

You extract RAR files with the unrar command:

unrar x file.rar

Similarly, you can create RAR file archive from a given files and folders using the rar command:

rar a output_file.rar files_or_folders_to_archive

 

Install RAR archive support in Linux

There are two components involved here. You need:

  • unrar command: To extract a RAR file

  • rar command: To create a RAR file

You can install either or both, depending on your needs.

In Ubuntu and Debian-based distros, use:

sudo apt install rar unrar

In Arch and Manjaro, use the pacman command:

sudo pacman -S rar unrar

In Fedora-based Linux distros, use:

sudo dnf install rar unrar

In Red Hat, use the yum commands:

sudo yum install rar unrar

In SUSE, use the zipper command:

sudo zypper install rar unrar

 

List the contents of the RAR file (without extracting it)

If you just want to peek into the contents of the RAR file, use the listing option l like this:

unrar -l file.rar

 

Extracting RAR Files in Linux

You can easily extract the contents of the RAR file in the present working directory from where you are running the command.

unrar e file.rar

You can instruct the unrar command to extract the file to another directory location like this:

unrar e file.rar extract_to_another_dir

 

Extract the RAR file and retain the directory structure

I believe the extracted file should retain the directory structure inside the RAR file.

Thankfully, the unrar command does allow to do that with the use of option -x:

unrar x file.rar

Let’s take the same sample file tag.rar and extract it with option -x so that it keeps the same directory structure.

So, when I extracted tag.rar file this time, it is extracted to a directory called tags-images and contains the following:

root@liviu$ tree tags-images/
tags-images/
├── courses.png
├── guides.png
├── privacy.png
├── trivia.png
└── vr
    ├── boy-with-vr-glasses-play-with-virtual-videogame.jpg
    └── images.jpeg

2 directories, 6 files

Now, where did this tags-images come from? Actually, that’s the original folder name from which the tag.rar file was created. You can see it when you list the contents of the RAR file.

Similar to the previous option, you can also extract the file to another directory location:

unrar x file.rar extract_to_another_dir

 

Creating RAR archive file in Linux

Creating a RAR archive file in Linux is rather simple. You use the rar command for this purpose in the following way:

rar a archive.rar files_and_folders_to_archive

The output file name is mandatory but you may skip the .rar extension as it is automatically added if not specified.