Linux rar Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the rar command on Ubuntu 22.04 to create and manage RAR archives. First, you will install the rar package, which provides the rar command. Then, you will explore creating and extracting RAR archives, as well as managing them with advanced options. The rar command is a powerful tool for compressing and archiving files, and this lab will give you practical examples to help you get started.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") subgraph Lab Skills linux/apt -.-> lab-422878{{"`Linux rar Command with Practical Examples`"}} linux/mkdir -.-> lab-422878{{"`Linux rar Command with Practical Examples`"}} linux/ls -.-> lab-422878{{"`Linux rar Command with Practical Examples`"}} linux/sudo -.-> lab-422878{{"`Linux rar Command with Practical Examples`"}} end

Install rar Package on Ubuntu 22.04

In this step, we will install the rar package on the Ubuntu 22.04 Docker container. The rar package provides the rar command, which is a powerful tool for creating and managing RAR archives.

First, let's update the package index:

sudo apt-get update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Next, install the rar package using the following command:

sudo apt-get install -y rar

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  librar5
Suggested packages:
  unrar
The following NEW packages will be installed:
  librar5 rar
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 725 kB of archives.
After this operation, 2,269 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 librar5 amd64 5.9.0-1 [407 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/universe amd64 rar amd64 1:6.1.0-1 [318 kB]
Fetched 725 kB in 1s (725 kB/s)
Selecting previously unselected package librar5.
(Reading database ... 28133 files and directories currently installed.)
Preparing to unpack .../librar5_5.9.0-1_amd64.deb ...
Unpacking librar5 (5.9.0-1) ...
Selecting previously unselected package rar.
Preparing to unpack .../rar_1%3a6.1.0-1_amd64.deb ...
Unpacking rar (1:6.1.0-1) ...
Setting up librar5 (5.9.0-1) ...
Setting up rar (1:6.1.0-1) ...
Processing triggers for man-db (2.10.2-1) ...

The rar package is now installed on your Ubuntu 22.04 Docker container.

Create and Extract rar Archives

In this step, we will learn how to create and extract RAR archives using the rar command.

First, let's create a sample directory and some files to work with:

cd ~/project
mkdir sample_files
cd sample_files
touch file1.txt file2.txt file3.txt

Now, let's create a RAR archive named sample.rar that contains the files in the sample_files directory:

rar a sample.rar *.txt

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Adding files to archive sample.rar

Creating archive sample.rar

Archiving  file1.txt
Archiving  file2.txt
Archiving  file3.txt

The archive has been updated successfully.

The rar a command creates a new RAR archive named sample.rar and adds all the .txt files in the current directory to it.

Next, let's extract the contents of the sample.rar archive to a new directory:

mkdir extracted_files
rar x sample.rar extracted_files

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Extracting  file1.txt
Extracting  file2.txt
Extracting  file3.txt

All files were extracted

The rar x command extracts the contents of the sample.rar archive to the extracted_files directory.

Now, let's verify that the files were extracted correctly:

ls -l extracted_files

Example output:

total 0
-rw-r--r-- 1 labex labex 0 May 22 12:34 file1.txt
-rw-r--r-- 1 labex labex 0 May 22 12:34 file2.txt
-rw-r--r-- 1 labex labex 0 May 22 12:34 file3.txt

The files have been extracted successfully.

Manage rar Archives with Advanced Options

In this step, we will explore some advanced options for managing RAR archives using the rar command.

First, let's create a new RAR archive with password protection:

rar a -p password sample_encrypted.rar sample_files/*.txt

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Enter password:
Verify password:

Adding files to archive sample_encrypted.rar

Creating archive sample_encrypted.rar

Archiving  file1.txt
Archiving  file2.txt
Archiving  file3.txt

The archive has been updated successfully.

The -p option allows you to set a password for the RAR archive. You will be prompted to enter and verify the password.

Next, let's extract the encrypted archive:

rar x sample_encrypted.rar extracted_encrypted

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Enter password:
Extracting  file1.txt
Extracting  file2.txt
Extracting  file3.txt

All files were extracted

The rar x command extracts the contents of the encrypted sample_encrypted.rar archive to the extracted_encrypted directory. You will be prompted to enter the password.

Now, let's list the contents of the RAR archive:

rar l sample_encrypted.rar

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Enter password:
Listing archive: sample_encrypted.rar

------------------------------------------------------------------
Name                 Packed  Size  Ratio  Date    Time   Attr
------------------------------------------------------------------
file1.txt            36      0     0.00%  05-22-23 12:34  ....
file2.txt            36      0     0.00%  05-22-23 12:34  ....
file3.txt            36      0     0.00%  05-22-23 12:34  ....
------------------------------------------------------------------
Total files: 3               108    0.00%

The rar l command lists the contents of the sample_encrypted.rar archive. You will be prompted to enter the password.

Finally, let's delete the sample_encrypted.rar archive:

rar d sample_encrypted.rar

Example output:

RAR 6.10   Copyright (c) 1993-2022 Alexander Roshal   25 Mar 2022
Evaluation copy. Registration is required for private or commercial use.

Enter password:
Deleting files from archive sample_encrypted.rar

The archive has been updated successfully.

The rar d command deletes the sample_encrypted.rar archive. You will be prompted to enter the password.

Summary

In this lab, we learned how to install the rar package on Ubuntu 22.04, create and extract rar archives, and manage rar archives with advanced options. We started by updating the package index and installing the rar package, which provides the rar command for working with RAR archives. Then, we learned how to create new rar archives, add files to existing archives, and extract the contents of rar archives. Finally, we explored advanced rar options, such as setting passwords, splitting archives, and testing the integrity of rar files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like