rar - Linux


Overview

The rar command is an archive manager used in Linux for creating, managing, and controlling archive files (rar archives). The primary use of rar involves data compression, archiving, and extraction of files. The command is widely used because it offers a high compression ratio and strong encryption capabilities, making it suitable for data backup and secure file sharing.

Syntax

The general syntax of the rar command is as follows:

rar <command> [options] <rar_file> [files...] [@listfiles...]
  • <command>: One of several commands such as a (add files to archive), x (extract files), etc.
  • [options]: Command-line options to modify behavior.
  • <rar_file>: The archive name.
  • [files...]: Files to be added or extracted.
  • [@listfiles...]: A file list to be processed.

Options/Flags

Below are some common options used with rar:

  • -p[password]: Adds a password to the archive. If just -p is used without a password, the program prompts for it.
  • -r: Recursively process subdirectories.
  • -m<level>: Set the compression level (0-store only to 5-maximal).
  • -f: Freshen existing files, i.e., only replace files that are older than those in the archive.
  • -u: Update files, i.e., only include files that are newer than those in the archive.
  • -v<size>[type]: Create a multi-volume archive, with each volume having the specified size; type can be k, m, g (kilobytes, megabytes, gigabytes).
  • -x<file>: Exclude the specified file.
  • -t: Test archive files.
  • -s: Create solid archive.

Examples

1. Creating an archive

rar a archive.rar file1.txt file2.txt

2. Creating a password-protected and encrypted archive

rar a -pYourPassword archive.rar file1.txt file2.txt

3. Extracting files from an archive

rar x archive.rar

4. Creating a multi-volume archive

rar a -v50m archive.rar largefile.iso

5. Updating an archive with fresh files only

rar u archive.rar new_files/

Common Issues

  • Forgotten Passwords: If a password is forgotten, the archive cannot be opened. Always keep backups of your passwords in a secure location.
  • Corrupt Files: Corrupt archive errors can occur due to incomplete downloads or media errors. You can try repairing the archive using rar r archive.rar.

Integration

rar can be integrated into shell scripts or combined with other commands using pipes and redirections. For example, to find specific files and archive them:

find /path/to/files -name "*.txt" -print0 | xargs -0 rar a text_files.rar
  • unrar: Used to extract rar files.
  • zip: Another compression tool, commonly used for creating .zip files.
  • tar: Widely used for bundling multiple files into a single file in Linux.

For more detailed information, you can visit the official RARLab documentation or use man rar on your Linux terminal if the man pages are installed.