Using RAR in Linux: Guide and Samples

By | October 15, 2009

Nowadays almost everyone who had ever used a computer knows what a file archiver is. It represents a program that combines a number of files together into an archive file for easier storage. Although many users consider that file archivers are used to compress the data in order to reduce the size of the archive, their functions are not limited to that. For instance, the most basic archivers just take files and create an archive without compressing them.

There are multiple compression algorithms available to compress data. You should remember that some kinds of data can be highly compressed and some kinds of data do not benefit from compression. At the moment archive formats are also used to package software files for distribution, installation, and execution.

Let’s see what the most popular archive formats are and what they are used for. All of them are divided into 5 different types:

1. Archiving only formats (only combine files):

ar, cpio, tar and etc.

2. Compression only formats (only compress files):

gzip, bzip2, LZMA, compress and etc.

3. Multi-function formats can combine, compress, encrypt, create error detection and recovery information, and repackage the archive into self-extracting/self-expanding files:

7z, ACE, ARC, cpt, dmg, GCA, kgb, LZX, RAR, qda, ZIP and etc.

4. Software Packaging formats are used to create software packages that may be self-installing files:

deb, pkg, RPM, MSI, JAR and etc.

5. Disk Image formats are used to create disk images or optical disk images of mass storage volumes:

OEB Package Format, OEBPS Container Format, Open Packaging Conventions and etc.

If you would like to learn more about archive formats, please visit this page.

Most likely you’ve already understood that in the given post we are not going to review all the archive formats, but the most popular one – RAR.
As it was mentioned before RAR is a multi-function archive file format that supports data compression, error recovery, file spanning and etc. Though at the moment it’s licensed by Win.rar GmbH, it was originally developed by Russian software engineer – Eugene Roshal (RAR comes from Roshal ARchive).

You should know that RAR files can be created only using commercial software and the one that has permission from the author. In fact the only freeware that can be used to create RAR files is RAR for Pocket PC.

The most popular software for creating RAR files is WinRAR, which you probably are familiar with. Of course, it’s available only for Windows, so we won’t discuss it in detail. However there is WinRAR’s command line counterpart – RAR. It’s also commercial software but is available for WIndows, Linux, Mac OS X, MS-DOS, OS/2 and FreeBSD. In addition to that there is a multi-platform unrar tool from the same distributor that is used just to extract RAR files but not to write them.

Let’s try to figure out the advantages of RAR in comparison to other file archivers. Here are the most important ones:

  • RAR uses 128-bit AES (Advanced Encryption Standard) algorithm that is significantly better than the cryptographic algorithm used in Zip 2.0. This means that RAR encryption is much safer.
  • RAR has a better rate of compression than early compression algorithms like ZIP and gzip (still it generally compresses the data a little bit slower)
  • RAR has a recovery record function: archive formats contain redundant data embedded in the files in order to detect data storage or transmission errors, and the software used to read the archive files contain logic to detect and correct errors. Thus it allows you to restore a damaged file.
  • Multi-volume archive support: ability to break a RAR-archive into many smaller files
  • RAR allows you to work with any files as far as their size is concerned (till 8 EB = 8,000,000,000,000,000,000 B = 8 x 1018 bytes or 8 billion gigabytes), while popular ZIP format is limited to 2 GB.
  • RAR offers an optional compression algorithm highly optimized for multimedia data.

Now we are going to discuss how you can use RAR in Linux. First of all let’s see what should you do to open RAR-archives.

Due to the fact that there is no RAR support in Linux by default, we will need to install unrar command:

a) If you use Debian Linux, you need to type apt-get as follows to install unrar program:

# apt-get install unrar

b) In case of Fedora use yum command:

# yum install unrar

c) If you none of the above mentioned works for you, please do the following:

– download binary package from official rarlab site:

$ cd /tmp
$ wget http://www.rarlab.com/rar/rarlinux-3.6.0.tar.gz

– untar file:

$ tar -zxvf rarlinux-3.6.0.tar.gz

– go to rar directory (both unrar and rar commands are located in rar sub-directory):

$ cd rar
$ ./unrar

– copy rar and unrar to /bin directory:

# cp rar unrar /bin

In case you get glibc error, you need to use rar_static (you can find it in the same installation package) instead of rar and copy it to /bin under rar name.

d) You can also install it using Synaptic: System – Administration – Synapric Package Manager – find both rar and unrar – install them

For now everything should work fine. Here are some of the examples how you can use unrar command:

1) Suppose we want to open music.rar in the current directory:

$ unrar e music.rar

2) If you want to extract files with the full path, type the following command:

$ unrar x music.rar

3) In case you need to check the integrity of the file, use the command:

$ unrar t music.rar

4) To list the file inside RAR archive, use command:

$ unrar l music.rar

If you are searching for a GUI utility to extract and handle rar files with, there is PeaZip for Linux which may be very useful.

Let’s check how we can create archives using rar command. The list of rar options for Linux is pretty impressive, so we will mention just the most important ones:

–  Add files to archive (add all *.mp3 files from the current directory to the archive.rar):

rar a archive *.mp3

In this case the .rar extension will automatically append to the archive (the full name will be             archive_file.rar). If archive.rar already exists in the present working directory, then the         file you are trying to add to archive.rar will be simply added to the existing archive.           Just in case archive.rar already contains a file with the same name the old file will be replaced with the new one. Other files stored inside the archive will be unaffected. Please remember that this action is blind, meaning that RAR does not check the time stamps of files.

Add files and check time stamps (denoted by «-u») using the command:

rar a -u archive *.mp3

Archiving all files and directories in current directory:

rar a archive

Archiving the content of a particular directory:

rar a archive  /music/korn

Archiving groups of files (according to the type):

rar a archive '*.jpg'

Deleting files from archive (all .png files in from the current directory):

rar d archive '*.png'

Locking archive so that any command which intends to change the archive will be ignored:

rar k final.rar

Renaming archived files. Here is the command syntax:

rar rn <arcname> <srcname1> <destname1> … <srcnameN> <destnameN>

rar rn data.rar readme.txt readme.bak info.txt info.bak

Thus it will rename readme.txt to readme.bak and info.txt to info.bak in the archive data.rar

–  Creating recovery volumes – .rev files.

They can be later used to reconstruct missing and damaged files in a volume set. This command makes sense only for multivolume archives and you need to specify the name of the first volume in the set as the archive name. For example:

rar rv3 data.part01.rar

This feature may be useful for backups or, for example, when you posted a multivolume archive to a newsgroup and a part of subscribers did not receive some of the files. Reposting recovery volumes instead of usual volumes may reduce the total number of files to repost.

Each recovery volume is able to reconstruct one missing or damaged RAR volume. For example, if you have 30 volumes and 3 recovery volumes, you are able to reconstruct any 3 missing volumes. If the number of .rev files is less than the number of missing      volumes, reconstructing is impossible. The total number of usual and recovery volumes must not exceed 255.

«N» parameter specifies a number of recovery volumes to create and must be less than the total number of RAR volumes in the set.

– Encrypting files with the password (using «-p») while archiving. The password is case-sensitive. If you omit the password on the command line, you will be prompted with message “Enter password”.

rar a -pfcfcfc archive '*.txt'

This command adds files *.txt and encrypt them with password «fcfcfc»

– Creating SFX – Self-Extracting archives:

If you use this switch when creating a new archive, a Self-Extracting archive (using a module in filedefault.sfx or specified in the switch) would be created. In the Windows version default.sfx should be placed in the same directory as the rar.exe, in Unix – in the user’s home directory, in /usr/lib or /usr/local/lib.

rar a -sfxwincon.sfx myinst

Create SelF-eXtracting (SFX) archive using wincon.sfx SFX-module.

Of course all the above mentioned options represent just some of the available ones. If you would like to learn more, please check «rar.txt» file from the installation package (User’s manual).

Conclusion

As you can see RAR for Linux is an extremely powerful tool that represents very decent replacement for WinRAR. As far as the functionality is concerned you get absolutely the same software. The only thing that may be a little bit difficult in the very beginning is the necessity to use command line. However it’s not as hard to use, so in some time you will find yourself using it without any problem.