Contents
Roshal Archive, abbreviated as RAR, is a popular program used to compress files for easier storage and transfer over a network. RAR is a proprietary compression format, and although Linux distributions do not come with built-in support for RAR, you can easily install utilities such as rar to extract or create RAR archives. In this blog post, we will guide you through installing the required tools and demonstrate a few common use cases on Linux.
Installing RAR
Ubuntu/Debian
sudo apt update
sudo apt install rar
Redhat
sudo dnf install rar
CentOS/RHEL
For CentOS and RHEL, you may need to enable EPEL (Extra Packages for Enterprise Linux) before installing rar
sudo dnf install epel-release
sudo dnf install rar
Command syntax
Rar uses the following syntax
rar [flag] [archive_name] [files_to_archive]
Here are some of the most common flags
a : Creates an archive; adds files to an archive.
d : Deletes files from an archive.
e : Extracts files to the current directory. Does not create any subdirectories
x : Extracts files with their full paths.
l : Shows the contents list of the archive
Create an archive
Here is an example of creating an archive named "archive.rar" containing the files "file1", "file2" and "file3"
rar a archive.rar file1 file2 file3
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
To add a new file to an existing archive, we can use a archive.rar . In this example, we want to add "file4" to our "archive.rar" file
rar a archive.rar file4
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
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 files in directories, you need to use the x flag; otherwise you will extract all files into your current directory.
Say you have a directory named "dir1" with a file named "file4" inside it, along with the other 3 files in your archive. You want to preserve this path for file4, so you use the following command:
rar x archive.rar
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 the contents of an archive
To list the contents of an archive, you can use l
tar l archive.rar
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. Say we want to delete "file1" from the archive. Use d to delete.
tar d archive file1
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
Say you want to archive and back up your log folder together with all the files in it. You can do this with the following command:
rar a log_archive.rar /var/log
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