Working with RAR Archives in Linux Command Prompt
The RAR format is famous for its high compression level, support for data recovery, and high tamper resistance, which made it quite popular in the Windows community. But yet, it’s less popular among Linux users and administrators than a number of other archives. However, you may need to be able to work with it on the server, so let’s take a look at the basic commands and options you’ll need to confidently work with RAR archives in a command prompt.
Before You Begin
Make sure that you have all the required packages installed.
For Debian and Ubuntu:
sudo apt update && sudo apt install rar unrar
For CentOS and Rocky Linux/AlmaLinux:
# Add an additional EPEL repository, since the system ones contain no rar
sudo yum install epel-release
sudo yum makecache && sudo yum install rar unrar
How to Create a RAR Archive
Use the following command to create an archive:
rar a archive.rar /directory/path/
For example:
rar a backup.rar /var/www/html/
How to extract a RAR Archive
To extract the archive in the current directory, use the following command:
unrar x archive.rar
To extract the archive to a directory other than the current one, you need to specify the path to it at the end. For example:
unrar x backup.rar /home/user/backup/
Additional Useful Options
- -l — show a list of files within the archive without extracting it:
unrar l archive.rar
- -u — update files within the archive:
rar u archive.rar /path/to/new_files
Consider the following details when updating a file within the archive:
- If the archive contains no new_file.txt, it will be added.
- If the archive already contains a file of the same name, but its contents on the drive have changed, then this file will be updated to the latest version.
You can also use this command to update multiple files at once, for example:
rar u archive.rar *.txt
This command will update all txt files in the archive and will add new ones if they haven’t been added before.
- -p — set a password for the archive:
rar a -p archive.rar /file/path