OSETC TECH

How to Extract RAR File in Ubuntu/CentOS/RHEL Linux

This post will guide you how to extract a RAR archive file under CentOS/RHEL/Ubuntu Linux. How do I open a rar file in Linux. How to create a rar file using RAR tool under Linux operating system.

What is RAR?


RAR is a proprietary archive file format that supports data compression, error recovery and file spanning.It was developed by a Russian software engineer, Eugene Roshal and the RAR software is licensed by win.rar.

Install UNRAR Tool


If you have a rar archive files in your CentOS/Ubuntu Linux system, and you want to extract all files from this rar archive file, you need to use unrar tool to achieve the result. and the unrar tool is not installed by default. so you need to install it via yum or apt-get command firstly.

For Ubuntu/Debian Linux:

If you are using Ubuntu or Debian Linux, you can use apt or apt-get command to install unrar package, type:

$ sudo apt-get install unrar

Outputs:

devops@devops-osetc:~$ sudo apt-get install unrar
sudo: unable to resolve host devops-osetc
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
unrar
0 upgraded, 1 newly installed, 0 to remove and 598 not upgraded.
Need to get 123 kB of archives.
After this operation, 310 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/multiverse amd64 unrar amd64 1:5.3.2-1 [123 kB]
Fetched 123 kB in 1s (90.6 kB/s)
Selecting previously unselected package unrar.
(Reading database ... 179223 files and directories currently installed.)
Preparing to unpack .../unrar_1%3a5.3.2-1_amd64.deb ...
Unpacking unrar (1:5.3.2-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up unrar (1:5.3.2-1) ...
update-alternatives: using /usr/bin/unrar-nonfree to provide /usr/bin/unrar (unrar) in auto mode

For CentOS/RHEL Linux

If you are using CentOS/RHEL Linux, you need to use yum command to install unrar package, type:

# yum install unrar -y

Or you can download the latest unrar source file, just do the following steps:

#1 download the latest unrar source package, type:

# wget https://www.rarlab.com/rar/rarlinux-x64-5.6.1.tar.gz

#2 unzip rarlinux package, type:

# tar -zxvf rarlinux-x64-5.6.1.tar.gz

Outputs:

[root@osetc_test ~]# tar -zxvf rarlinux-x64-5.6.1.tar.gz
rar/
rar/order.htm
rar/acknow.txt
rar/readme.txt
rar/default.sfx
rar/license.txt
rar/rarfiles.lst
rar/whatsnew.txt
rar/makefile
rar/rar
rar/unrar
rar/rar.txt

#3 change the current directory to rar, type:

# cd rar

#4 copy rar and unrar script to /usr/local/bin directory

# cp -v rar unrar /usr/local/bin

Outputs:

root@osetc_test rar]# cp -v rar unrar /usr/local/bin/
`rar' -> `/usr/local/bin/rar'
`unrar' -> `/usr/local/bin/unrar'

For Fedora Linux

If you are using Fedora Linux, you need to use dnf command to install unrar pcakge, type:

$ sudo dnf install unrar

For FreeBSD Unix

If you are using FreeBSD Unix system, you need to use the pkg command to install unrar package, type:

# pkg install unrar

For MacOS system

If you are using macOS system, and you need to use brew command to install unrar package, type:

$ brew install unrar

Extract RAR File Using UNRAR Tool


After installed unarar tool, you can use unrar command to extract all files from a rar archive file. If you want to extract a RAR file in the current working directory, you just need to use the unrar command with e option, type:

$ unrar e test.rar

Outputs:

devops@devops-osetc:~/working$ unrar e test.rar
UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal
Extracting from test.rar
Extracting fio OK
All OK

If you want to extract a RAR archive file into a specific destination directory, such as: /tmp, just type:

$ unrar e test.ra /tmp

Outputs:

devops@devops-osetc:~/working$ unrar e test.rar /tmp
UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal
Extracting from test.rar
Extracting /tmp/fio OK
All OK

List All Files inside RAR File


If you want to list or check Files in a RAR archive file, you can use unrar command with l option, type:

$ unrar l test.rar

Outputs:

devops@devops-osetc:~/working$ unrar l test.rar
UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal
Archive: test.rar
Details: RAR 4
Attributes Size Date Time Name
----------- --------- ---------- ----- ----
-rwxrwxr-x 8600 2017-07-31 14:21 fio
----------- --------- ---------- ----- ----
8600 1

It will display the list of files with their attributes, size, date,etc.

Test RAR File


If you want to check the integrity of a RAR archive file, you can use the unrar commadn with t option, type:

$ unrar t test.rar

outputs:

devops@devops-osetc:~/working$ unrar t test.rar
UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal
Testing archive test.rar
Testing fio OK
Testing fio.c OK
All OK

Create RAR File


If you want to create a rar archive file under your Linux system, you can use the rar command to achieve it. And the rar command is not installed by default, so you need to installed it firstly via yum or apt-get command.

For CentOS/RHEL, you need to download the latest rar package from rarlab site, as I memtioned above.

For Ubuntu/Debian Linux, you can use the apt-get command to install it, type:

$ sudo apt-get install rar

Outputs:

devops@devops-osetc:~/working$ sudo apt-get install rar
sudo: unable to resolve host devops-osetc
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
rar
0 upgraded, 1 newly installed, 0 to remove and 598 not upgraded.
Need to get 587 kB of archives.
After this operation, 1,657 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/multiverse amd64 rar amd64 2:5.3.b2-1 [587 kB]
Fetched 587 kB in 2s (262 kB/s)
Selecting previously unselected package rar.
(Reading database ... 179228 files and directories currently installed.)
Preparing to unpack .../rar_2%3a5.3.b2-1_amd64.deb ...
Unpacking rar (2:5.3.b2-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up rar (2:5.3.b2-1) ...

Then you can use rar command with a option to create a new rar archive file. type:

$ rar a test.rar fio fio.c

Outputs:

devops@devops-osetc:~/working$ rar a test.rar fio fio.c
RAR 5.30 beta 2 Copyright (c) 1993-2015 Alexander Roshal 4 Aug 2015
Trial version Type RAR -? for help
Evaluation copy. Please register.
Creating archive test.rar
Adding fio OK
Adding fio.c OK
Done

devops@devops-osetc:~/working$ ls *.rar
test.rar

So test.rar file has been created and two files fio and fio.c were added into test.rar.

Adding New files into Existing RAR File


If you have a rar archive file call test.rar, and you want to add another file into it, you can use the rar command with u option, type:

$ rar u test.rar fio3

Outputs:

devops@devops-osetc:~/working$ rar u test.rar fio3
RAR 5.30 beta 2 Copyright (c) 1993-2015 Alexander Roshal 4 Aug 2015
Trial version Type RAR -? for help
Evaluation copy. Please register.
Updating archive test.rar
Adding fio3 OK
Done

Let’s verify if fio3 is added into test.rar archive file, type:

$ rar l test.rar

Outputs:

devops@devops-osetc:~/working$ rar l test.rar

RAR 5.30 beta 2 Copyright (c) 1993-2015 Alexander Roshal 4 Aug 2015
Trial version Type RAR -? for help

Archive: test.rar
Details: RAR 4

Attributes Size Date Time Name
----------- --------- ---------- ----- ----
-rwxrwxr-x 8600 2017-07-31 14:21 fio
-rw-rw-r-- 80 2017-07-31 14:20 fio.c
-rw-rw-r-- 0 2017-08-01 15:16 fio3
----------- --------- ---------- ----- ----
8680 3

From the above outputs, we can see that fio3 has been added into test.rar file.

Set Password to Protect RAR File


You can set a password to protect a rar archive file, just use the rar command with a -p option, type:

$ rar a -p test.rar

Outputs:

devops@devops-osetc:~/working$ rar a -p test.rar
Enter password (will not be echoed):
Reenter password:
RAR 5.30 beta 2 Copyright (c) 1993-2015 Alexander Roshal 4 Aug 2015
Trial version Type RAR -? for help
Evaluation copy. Please register.
Updating archive test.rar
Updating fio OK
Updating fio.c OK
Updating fio3 OK
Done

Then let’s try to extract this rar archive check if it prompt you to type a password to continue, type:

$ unrar e test.rar

Outputs:

devops@devops-osetc:~/working$ unrar e test.rar
UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal
Extracting from test.rar
Enter password (will not be echoed) for fio:
Extracting fio OK
fio.c - use current password ? [Y]es, [N]o, [A]ll A
Extracting fio.c OK
Extracting fio3 OK
All OK

Get More Information about UNRAR Command


If you want to get more help about unrar or rar command, you can type the following command:

$ man rar

Or

$ rar | less

Or

$ man unrar

Or

$ unrar

Outputs:

devops@devops-osetc:~/working$ unrar

UNRAR 5.30 beta 2 freeware Copyright (c) 1993-2015 Alexander Roshal

Usage: unrar <command> -<switch 1> -<switch N> <archive> <files...>
<@listfiles...> <path_to_extract\>

<Commands>
e Extract files without archived paths
l[t[a],b] List archive contents [technical[all], bare]
p Print file to stdout
t Test archive files
v[t[a],b] Verbosely list archive contents [technical[all],bare]
x Extract files with full path

<Switches>
- Stop switches scanning
@[+] Disable [enable] file lists
ad Append archive name to destination path
ag[format] Generate archive name using the current date
ai Ignore file attributes
ap<path> Set path inside archive
c- Disable comments show
cfg- Disable read configuration
cl Convert names to lower case
cu Convert names to upper case
dh Open shared files
ep Exclude paths from names
ep3 Expand paths to full including the drive letter
f Freshen files
id[c,d,p,q] Disable messages
ierr Send all messages to stderr
inul Disable all messages
kb Keep broken extracted files
n<file> Additionally filter included files
n@ Read additional filter masks from stdin
n@<list> Read additional filter masks from list file
o[+|-] Set the overwrite mode
ol[a] Process symbolic links as the link [absolute paths]
or Rename files automatically
ow Save or restore file owner and group
p[password] Set password
p- Do not query password
r Recurse subdirectories
sc<chr>[obj] Specify the character set
sl<size> Process files with size less than specified
sm<size> Process files with size more than specified
ta<date> Process files modified after <date> in YYYYMMDDHHMMSS format
tb<date> Process files modified before <date> in YYYYMMDDHHMMSS format
tn<time> Process files newer than <time>
to<time> Process files older than <time>
ts<m,c,a>[N] Save or restore file time (modification, creation, access)
u Update files
v List all volumes
ver[n] File version control
vp Pause before each volume
x<file> Exclude specified file
x@ Read file names to exclude from stdin
x@<list> Exclude files listed in specified list file
y Assume Yes on all queries