RAR, an acronym for Roshal Archive is a popular program used for compressing files for easier storage and transportation over the network. RAR is a proprietary compression format and while Linux distributions don’t come with built-in support for RAR, you can easily install utilities like rar
to extract or create RAR archives. In this blog, we’ll guide you through installing the necessary tools and demonstrate a couple of common use cases in Linux.
Installing RAR
Ubuntu/Debian
sudo apt update
sudo apt install rar
Fedora
sudo dnf install rar
CentOS/RHEL
For CentOS and RHEL, you may need to enable the EPEL (Extra Packages for Enterprise Linux) before you can install rar
sudo dnf install epel-release
sudo dnf install rar
Command syntaxes
Rar uses the following syntaxis
rar [flag] [archive_name] [files_to_archive]
Here are some of the most common flags
a : Creates archive; Add files to archive.
d : Delete files from archive.
e : Extract files to current directory. Does not create any
subdirectories.
x : Extract files with full path.
l : List content of archive
Create an archive
Here is an example of creating an archive with the name "archive.rar" which includes the files "file1", "file2" and "file3"
rar a archive.rar file1 file2 file3
Output:
root@hostname:~# rar a archive.rar file1 file2 file3
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Evaluation copy. Please register.
Adding file1 OK
Adding file2 OK
Adding file3 OK
Done
We can also use the a flag to add a new file to an already existing archive. In this example, we want to add "file4" to our "archive.rar" file
rar a archive.rar file4
Output:
root@hostname:~# rar a archive.rar file4
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Evaluation copy. Please register.
Updating archive archive.rar
Adding file4 OK
Done
Extract files from an archive
To extract files from a .rar archive use the e flag
rar e archive.rar
Output:
root@hostname:~# rar e archive.rar
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Extracting from archive.rar
Extracting file1 OK
Extracting file2 OK
Extracting file3 OK
All OK
If you want to keep the full path of the files with directories you will need to use the x flag otherwise, you will extract all the files in your current directory.
Let's say that along with the other 3 files in your archive, you have a directory called "dir1" with a file inside called "file4". You want to keep this path for file4 so you use the following command:
rar x archive.rar
Output:
root@hostname:~# rar x archive.rar
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Extracting from archive.rar
Extracting file1 OK
Extracting file2 OK
Extracting file3 OK
Creating dir1 OK
Extracting dir1/file4 OK
All OK
List contents of an archive
In order to list the contents of an archive you can use the l flag
tar l archive.rar
Output:
root@hostname:~# rar l archive.rar
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Archive: archive.rar
Details: RAR 5
Attributes Size Date Time Name
----------- --------- ---------- ----- ----
-rw-r--r-- 0 2024-10-11 14:30 file1
-rw-r--r-- 0 2024-10-11 14:30 file2
-rw-r--r-- 0 2024-10-11 14:30 file3
----------- --------- ---------- ----- ----
0 3
Delete a file from an archive
You can use the previously mentioned l flag to check the contents of an archive. Let's say we want to delete "file1" from the archive. Use the d flag in order to delete it.
tar d archive file1
Output:
root@hostname:~# rar d archive.rar file1
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Deleting from archive.rar
Deleting file1
Done
Archive a folder
Let's say that you want to archive your log folder with all the files inside it to have as a backup. You can do so with the following command:
rar a log_archive.rar /var/log
Output:
RAR 5.50 Copyright (c) 1993-2017 Alexander Roshal 11 Aug 2017
Trial version Type 'rar -?' for help
Evaluation copy. Please register.
Creating archive log_archive.rar
Adding /var/log/dpkg.log OK
Adding /var/log/lastlog OK
Adding /var/log/fontconfig.log OK
Adding /var/log/upgrade-policy-changed.log OK
Adding /var/log/btmp OK
Adding /var/log/wtmp OK
Adding /var/log/bootstrap.log OK
Adding /var/log/ubuntu-advantage.log OK
Adding /var/log/apt/history.log OK
Adding /var/log/apt/eipp.log.xz OK
Adding /var/log/apt/term.log OK
Adding /var/log/alternatives.log OK
Adding /var/log/faillog OK
Adding /var/log/dist-upgrade OK
Adding /var/log/journal OK
Adding /var/log/private OK
Adding /var/log/apt OK
Adding /var/log/unattended-upgrades OK
Adding /var/log OK
Done