[Linux] Extract and Manage RAR Files with the unrar Command

目次

Overview

The unrar command is used to extract files from “RAR format” archives, which offer high compression ratios. It is commonly used to handle files created in Windows or to restore large data distributed as split volumes (e.g., .part1.rar, .part2.rar).

Note: Since this is often not included in standard Linux distributions, you may need to install it from a “non-free” repository.

Specifications (Arguments and Options)

Syntax

unrar [command] [options] [archive_filename] [path/file…]

Note: unrar uses “commands” (without a hyphen) and “options” (with a hyphen).

Main Commands (Action Modes)

CommandDescription
xExtracts files while maintaining the directory structure (Recommended).
eExtracts all files into the current directory, ignoring the folder structure.
lLists the files inside the archive.
vLists files with detailed information (compression ratio, version, etc.).
tTests the integrity of the archive (does not extract).
pDisplays the file content on standard output (for use with pipes).

Main Options (Behavior Modification)

OptionDescription
-yAutomatically answers “Yes” to all prompts (e.g., overwrite confirmation).
-o+Forces the command to overwrite existing files.
-o-Prevents overwriting existing files.
-x[file]Excludes a specific file from being processed.
-inulDisables all output except for error messages (silent mode).
-ierrSends all messages to standard error (stderr).
-p[pass]Specifies a password (e.g., -pSecret123).

Basic Usage

It is best to check the contents of the archive before extracting them.

# List files within the archive
unrar l design_assets.rar

Example Result

Archive: design_assets.rar
Details: RAR 5

 Attributes      Size     Date    Time   Name
----------- ---------  ---------- -----  ----
 -rw-r--r--   1048576  2025-10-15 10:00  schema.sql
 -rw-r--r--    524288  2025-10-15 10:01  images/logo.psd
----------- ---------  ---------- -----  ----
              1572864                    2

Practical Commands

Extract with Directory Structure

This is the standard way to extract files. Use the x command.

# Extract files to the current directory while keeping the structure
unrar x design_assets.rar

Extract Only a Specific File

You can restore just one file instead of the whole archive. Specify the path as an argument.

# Extract only logo.psd from the images folder
unrar x design_assets.rar images/logo.psd

Exclude Specific Files During Extraction

Use the -x option if you want to extract everything except certain files.

# Extract everything except heavy .psd files
unrar x design_assets.rar -x*.psd

Customization Points

  • Skip Confirmation in Batch Processing (-y): This is useful in scripts to prevent the process from stopping for overwrite prompts.Bashunrar x -y backup.rar /var/www/html/
  • Extracting Password-Protected RARs (-p): While you will be prompted for a password in interactive mode, you can also specify it in the command.Bashunrar x -pMyPassword123 secure_data.rar

Important Notes

  • Difference Between e and x: The e command extracts all files into one location, which can cause naming conflicts. It is generally safer to use x to preserve the full path.
  • Licensing: unrar is freeware but not open-source. Some repositories may offer a completely free alternative called unar.
  • Handling Split Archives: For files named data.part01.rar, data.part02.rar, etc., simply run the command on the first file. The system will automatically find and combine the remaining parts.

Application

Archive Integrity Check

Confirm if data is corrupted before extracting. This is helpful after a large download.

# Test all files without extracting
unrar t large_backup.rar

Example Result

Testing archive large_backup.rar

Testing     images/background.png                                     OK
Testing     db_dump.sql                                               OK
All OK

Summary

The unrar command is essential for handling large files from Windows environments or multi-part archives. Master the three basic actions—l to list, x to extract properly, and t to test—and combine them with -y for automation.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次