How to Use Find and Unrar to Extract Specific Files

You can combine the Linux find utility with unrar to search an entire filesystem for RAR archives and extract only specific file types, such as documents, audio tracks, or images. By pairing the locating power of find with the pattern-matching extraction syntax of unrar, administrators and users can automate batch extractions without manually opening each archive or unpacking unwanted data.

The Basic Command Structure

The command uses find to locate .rar files and passes each matched archive to unrar via the -exec parameter:

find /path/to/search -type f -name "*.rar" -exec unrar e {} "*.pdf" /destination/dir/ \;

In this command:

  • find /path/to/search: Specifies where the search begins (use / to scan the whole system).
  • -type f: Restricts the search strictly to regular files.
  • -name "*.rar": Matches all files with the .rar extension.
  • -exec ... \;: Executes the specified command for every archive found.
  • {}: Acts as a placeholder replaced by the path of each found RAR file.

Understanding unrar Extraction Options

The unrar utility supports selective extraction natively by accepting target file masks at the end of the command:

  • e (Extract without paths): Unpacks all matching files into a single flat directory, ignoring internal folder structures:
    unrar e archive.rar "*.jpg" /target/folder/
  • x (Extract with full paths): Recreates the internal archive directory tree at the destination:
    unrar x archive.rar "*.jpg" /target/folder/

Always enclose file masks like "*.jpg" or "*.txt" in quotes. Quoting prevents the shell from expanding the wildcard before passing the pattern to unrar.

Practical Examples

1. Extract Specific File Types into a Central Directory

To search your entire home directory for RAR archives and extract only .docx files into a central folder named /home/user/Documents/Extracted:

find /home/user -type f -name "*.rar" -exec unrar e -o- {} "*.docx" /home/user/Documents/Extracted/ \;

The -o- switch ensures unrar skips files that already exist in the destination folder rather than prompting for overwrite confirmation, preventing the automated loop from stalling.

2. Extract Multiple Specific File Extensions

You can pass multiple patterns to unrar to extract multiple file formats in a single operation:

find /media/storage -type f -name "*.rar" -exec unrar e -y {} "*.png" "*.jpg" "*.jpeg" /media/storage/photos/ \;

The -y flag automatically answers "yes" to all internal queries.

3. Extract In-Place While Preserving Paths

To extract .csv files from archives found system-wide, unpacking them directly into the directory where each RAR file resides while preserving internal folder hierarchies:

find / -type f -name "*.rar" -execdir unrar x -o- {} "*.csv" \;

Using -execdir runs the unrar process from the directory containing the matched archive rather than the directory where you initiated the command.

Key Considerations

  • System-Wide Permissions: When searching the root filesystem (/), prepend sudo to avoid "Permission denied" errors on restricted directories.
  • Non-Interactive Execution: Always supply an overwrite switch like -o+ (overwrite all) or -o- (skip existing) so batch scripts do not pause waiting for terminal input.
  • Archive Integrity: Archives with passwords will prompt for input unless bypassed or provided using the -p<password> flag directly within the command string.