How to Extract and Create RAR Files in Linux
Got an RAR file? Learn to extract the rar file in the Linux command line. Also learn to create a RAR file in this quick tutorial.
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 x file.rar
Similarly, you can create RAR file archive from a given files and folders using the
rar a output_file.rar files_or_folders_to_archive
Install RAR archive support in Linux
There are two components involved here. You need:
unrarcommand: To extract a RAR file
rarcommand: To create a RAR file
You can install either or both, depending on your needs.
Check which Linux distribution you are using and then use its package manager to install the required packages.
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
As you can see in the screenshot below, my
tag.rar file has four images and a folder called vr with an image inside it.
Let's see about extracting the RAR file.
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
Let's see it with an example. Here, I extract a file named tag.rar and it extracts all the contents of the rar file in the same directory:
You can instruct the unrar command to extract the file to another directory location like this:
unrar e file.rar extract_to_another_dir
vrsubdirectory is not presented in the extracted files but its content is extracted.
Let's deal with it in the next section.
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
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:
abhishek@lhb:~/code$ 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.
RAR, Zip or other archive formats
The zip compression is more popular than RAR and you are more likely to encounter it than RAR.
Gzip is excellent for compressing log files and reduce its size. Learn to use the gzip command.
I hope you find this little tutorial helpful in extracting and creating RAR files in Linux. Let me know if you spot any issues or face any problems.