Using Rar files in Ubuntu
How To

Use Rar Files in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

While installing packages from the source, you may encounter files in the RAR format.

So the question is how do you unrar a file in Ubuntu?

Well, in this tutorial, I will walk you through the following:

  • How to unrar files in Ubuntu
  • How to create a RAR file in Ubuntu

How to unrar files in Ubuntu

To unrar files in Ubuntu, you'd have to install a CLI tool called unrar using the following command:

sudo apt install unrar

Once done, you can use the unrar command with the x option and it will extract files in the original directory structure:

unrar x Filename.rar

For example, here, I extracted files from the Scripts.rar file:

unrar x Scripts.rar
Unrar files in ubuntu

But you can do a lot more with the unrar command.

1. Unrar files to a different directory

By default, the unrar command will extract files in the current working directory but that's not always user want.

To unrar files to a different directory, all you have to do is append the path where the files need to be extracted as shown:

unrar x Filename.rar /path/to/extract

For example, here, I extracted to the subdirectory named New:

unrar x Scripts.rar New/
unrar x Scripts.rar New/

2. List file contents of a RAR file

If you want to list the contents of a RAR file to verify file contents, it can be achieved using the l flag:

unrar l Filename.rar
list the file contents of a RAR fie

How to create RAR files in Ubuntu

Similar to unrar, you need a different CLI utility that will allow you to create and modify RAR files.

And for that purpose, you'd have to install rar using the following:

sudo apt install rar

Once done, you can create a RAR file by using the rar command in the following manner:

rar Filename.rar File1 File2 File3 File4 FileN

For example, here, I created a RAR file named Scripts.sh containing multiple scripts:

rar a Scripts cat.sh empty-string.sh length.sh lines.sh

And unlike unrar command, the rar command allows you to modify existing RAR files in various ways.

1. Delete a file from the RAR archive

There are times when you may want to remove some unnecessary files from a RAR file and in that case, you can use the d option:

rar d Filename.rar File_to_remomve

For example, here, I removed cat.sh from the Scripts.rar file:

rar d Scripts.rar cat.sh
remove a file from RAR archive in Ubuntu

2. Add files to the existing RAR archive

Want to update the existing RAR file by adding some files to it? It can easily be done using the u option as shown:

rar u Filename.rar File1 File2

For example, here, I added non-empty.sh file to the Scripts.rar:

rar u Scripts.rar non-empty.sh
add files to existing RAR file in Ubuntu

To verify the process, you can list its contents:

verify whether the file was removed from RAR or not by listing its file contents

3. Create password-protected  RAR files

If you want to create a RAR file enabled with password protection, you'd have to use the -p flag with the a as shown:

rar a -p Filename.rar File1 File2 File3 

Once you execute the rar command in the above manner, it will prompt you to enter the password (twice),

Let's say I want to create a password-protected RAR file named Hello.rar with few files then, I will be using the following:

rar a -p Hello.rar read.sh length.sh
create password protected RAR file

And now, if I try to unrar protected file, it will ask for the password:

extract password-protected RAR file in Ubuntu

Want to unzip gz files? Here you go

If you have no idea how to extract files from the gz files, here's a detailed guide to help you:

Unzip gz Files in Ubuntu Command Line
Learn to extract .gz files in the Ubuntu command line with gzip and unzip commands.

I hope you will find this guide helpful. Have any questions? Leave a comment.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.