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:
Similarly, you can create RAR file archive from a given files and folders using the rar command:
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:
In Arch and Manjaro, use the pacman command:
In Fedora-based Linux distros, use:
In Red Hat, use the yum commands:
In SUSE, use the zipper command:
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:
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.
You can instruct the unrar command to extract the file to another directory location like this:
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:
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:
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:
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:
The output file name is mandatory but you may skip the .rar extension as it is automatically added if not specified.