Home Terminal Tuts How to extract RAR files in Linux using command line tools

How to extract RAR files in Linux using command line tools

Opening RAR files in Linux via the command line is a straightforward process. This guide shows you how to install necessary tools like 'unrar' or 'rar' and use simple commands to extract or manage RAR files, enhancing your file handling capabilities in the Linux command line environment.

by Divya Kiran Kumar
open rar files linux

Handling various file formats efficiently is a skill that enhances our productivity, especially when using Linux. This guide focuses on one important task – opening RAR files in Linux. RAR, a popular compression format, often presents a challenge due to its lack of native support in Linux. However, with the right tools and knowledge, this task becomes a breeze.

We delve into the essentials of installing and using Unrar, a versatile tool for managing RAR files. From basic extraction to advanced options like selective file extraction, viewing contents without extraction, and ensuring file integrity, this guide covers a range of techniques suited for both beginners and seasoned Linux users.

Understanding RAR files

Before diving into the “how,” let’s understand the “what.” RAR (Roshal Archive) is a file format for data compression and archiving, created by Eugene Roshal. It’s widely used for its efficient compression. However, unlike the ubiquitous ZIP format, RAR is not natively supported in many operating systems, including Linux. This brings us to our task at hand.

Prerequisites: Installing Unrar

Most Linux distros don’t come with RAR support out of the box, so we need to install a utility called “Unrar.” Unrar is a freeware command-line tool for extracting RAR files. Here’s how to install it in various distros.

Debian/Ubuntu

  1. Open the terminal (Ctrl+Alt+T).
  2. Update your package list:
    sudo apt update
    
  3. Install Unrar:
    sudo apt install unrar
    

Fedora

  1. Open the terminal.
  2. Install Unrar using DNF:
    sudo dnf install unrar
    

Arch Linux

  1. Open the terminal.
  2. Use the Pacman package manager:
    sudo pacman -S unrar
    

Extracting RAR files

Once Unrar is installed, extracting RAR files is a breeze.

  1. Open the terminal.
  2. Navigate to the directory containing your RAR file using the cd command.
  3. Use the following command to extract:
    unrar x yourfile.rar
    

Replace yourfile.rar with the name of your RAR file. The x parameter tells Unrar to preserve directory structures and file permissions.

Example output in Ubuntu

fosslinux@fl-ubuntu:~$ unrar x example.rar

Unrar 6.0.3 - myfreeware
Copyright (c) Eugene Roshal

Extracting from example.rar
Extracting  file1.txt                     OK 
Extracting  file2.jpg                     OK 
All OK

This output shows each file being extracted, followed by “OK” to indicate a successful extraction.

Handling password-protected RAR files

Encountering a password-protected RAR file? No worries. Just use:

unrar x -p yourfile.rar

You’ll be prompted to enter the password. Remember, the password will not be displayed while you type it (for security reasons).

Using selective file extraction

Sometimes, you don’t need to extract every file from a RAR archive. Unrar allows you to extract specific files.

Extracting a single file

  1. Open your terminal.
  2. Navigate to the directory containing your RAR file.
  3. Use the following command:
    unrar e yourfile.rar filename.ext
    

Replace filename.ext with the name of the file you want to extract.

Example output in Fedora

[fosslinux@fl-fedora]$ unrar e example.rar document.txt

Unrar 6.0.3 - myfreeware
Extracting document.txt... OK
All OK

This output indicates that only document.txt has been extracted from example.rar.

Viewing contents without extracting

Curious about what’s inside a RAR file without extracting it? You can easily view the contents.

Listing files in an archive

  1. Open your terminal.
  2. Use the command:
    unrar l yourfile.rar
    

Example output in Arch Linux

[fosslinux@fl-arch]$ unrar l example.rar

Unrar 6.0.3 - myfreeware
Archive example.rar
Details: RAR 4, encrypted headers
List of files
Name               Size      Packed
-------------------------------------
document.txt       1024      512
image.jpg          2048      1024
-------------------------------------
2 files

Extracting files without original directory paths

Want to extract files but don’t want to retain the original directory structure? Here’s how:

Extracting files flatly

  1. Open your terminal.
  2. Use the command:
    unrar e yourfile.rar
    

The e parameter extracts files to the current directory, ignoring their paths in the archive.

Example output in Ubuntu

fosslinux@fl-ubuntu:~$ unrar e example.rar

Unrar 6.0.3 - myfreeware
Extracting  document.txt                  OK 
Extracting  photos/image.jpg              OK 
All OK

In this example, all files are extracted to the current directory, without preserving their original folder structure.

Testing archive integrity

To check if a RAR file is damaged or corrupted, use the test function.

Verifying the integrity of RAR files

  1. Open your terminal.
  2. Use the command:
    unrar t yourfile.rar
    

Example output in Debian

fosslinux@fl-debian:~$ unrar t example.rar

Unrar 6.0.3 - myfreeware
Testing archive example.rar
Testing     document.txt                    OK 
Testing     photos/image.jpg                OK 
All OK

This output indicates that all files in example.rar are intact and not corrupted.

Quick reference table showing unrar commands

These commands are quite powerful and can be combined for more specific tasks as needed. Keep this table handy as a quick reference while working with RAR files in Linux!

Command Option Description
unrar x yourfile.rar Extracts the files with full path.
unrar e yourfile.rar Extracts the files to the current directory, ignoring paths.
unrar e yourfile.rar filename.ext Extracts a specific file.
unrar l yourfile.rar Lists the contents of the archive without extracting them.
unrar t yourfile.rar Tests the integrity of the files in the archive.
unrar x -p yourfile.rar Extracts files from a password-protected archive.
unrar x yourfile.rar -kb Keeps broken extracted files (useful in case of partial file corruption).
unrar x -o+ yourfile.rar Overwrites existing files without prompting.
unrar x -o- yourfile.rar Avoids overwriting existing files (prompts before overwrite).
unrar v yourfile.rar Displays the verbose list of files including file size, packed size, and more.

Conclusion

Extracting RAR files in Linux is straightforward once you have the right tools. Whether you’re using Ubuntu, Fedora, or Arch, the process is quite similar. As a personal preference, I find the simplicity of the command line more efficient than navigating through a GUI. However, if you’re more comfortable with graphical interfaces, there are plenty of options like File Roller in GNOME or Ark in KDE.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.