How to Extract RAR Files to a Specific Directory Using Command Line (with Practical Examples)


3 views

When working with RAR archives in terminal environments, a common challenge is extracting files to a target directory without first moving the archive itself. The basic extraction command:

rar e archive.rar

only extracts to the current working directory, which becomes problematic when dealing with large archives or when organization requirements dictate specific output locations.

The full RAR command syntax supports directory specification through the -o+ flag and target path parameter:

rar x -o+ archive.rar /target/path/

Key differences between extraction modes:

Command Behavior
rar e Extracts files to current dir (no paths)
rar x Preserves full directory structure

Extracting to an absolute path:

rar x -o+ backup.rar /mnt/external_drive/backups/2023/

Relative path example (from archive location):

rar x -o+ project_files.rar ../extracted_files/

For batch processing multiple archives:

for rarfile in *.rar; do
  rar x -o+ "$rarfile" "/extract_target/${rarfile%.*}/"
done

When dealing with password-protected archives:

rar x -pMyPassword -o+ secure.rar /decrypted_files/

Extracting specific file types only:

rar x -o+ -n*.txt docs.rar /text_files/
  • Ensure target directory exists before extraction
  • Use absolute paths when running from cron jobs
  • Add -y flag for automated "yes to all" responses
  • Check free space with df -h before large extractions

For systems without rar installed, use:

sudo apt-get install rar    # Debian/Ubuntu
sudo yum install rar        # RHEL/CentOS
brew install rar            # macOS

When working with RAR archives in a Unix-like environment, the simplest extraction command is:

rar e archive.rar

This extracts all files to the current working directory. However, in many development scenarios, we need more control over the output location.

The RAR command provides a straightforward way to specify the output directory:

rar e archive.rar /path/to/target/folder/

This will extract all files directly to the specified directory without needing to move the archive first.

For those who prefer using the 'x' command instead of 'e', which preserves the directory structure:

rar x archive.rar /path/to/target/folder/

Here are some real-world use cases:

# Extract to a temporary directory
rar e project_backup.rar /tmp/project_files/

# Preserve structure while extracting to a specific location
rar x data_archive.rar ~/projects/current/data/

When dealing with multiple RAR files, you can combine with find:

find . -name "*.rar" -exec rar x {} /target/directory/ \;

Remember that:

  • The target directory must exist before extraction
  • You need write permissions for the target location
  • Paths can be relative or absolute

On Windows (using WinRAR), the equivalent would be:

winrar x archive.rar "C:\target\folder\"