Extract File from RAR by Index Using Unrar
The standard unrar command-line utility does not have a
native flag to extract files directly by their numerical archive index.
However, you can achieve this by pairing unrar's
bare-listing mode (vb) with shell filtering tools to select
the file at a specific line number and pass its name back to
unrar for extraction. This guide demonstrates how to
accomplish this in both Linux/Unix and Windows environments.
Step 1: Identify the File Index
Before extracting, list the archive contents in bare format. The
vb command displays only the file paths, one per line,
omitting headers, footers, and metadata:
unrar vb archive.rarYou can append line numbers to identify the exact numerical index of the target file:
unrar vb archive.rar | nlStep 2: Extract by Index in Linux or macOS (Bash / Zsh)
Once you know the index number (for example, file index
3), use sed to isolate that line and pass the
filename back to unrar.
Extract to Current Directory (Flat Extraction)
unrar e archive.rar "$(unrar vb archive.rar | sed -n '3p')"Extract with Full Directory Paths Preserved
unrar x archive.rar "$(unrar vb archive.rar | sed -n '3p')"Extract to a Specific Folder
unrar x archive.rar "$(unrar vb archive.rar | sed -n '3p')" /path/to/destination/How it works:
unrar vb archive.rar: Outputs the archive's file list as raw filenames.sed -n '3p': Prints only the 3rd line of the output.$(...): Passes the resulting filename as an argument to the outerunrarextraction command.
Step 3: Extract by Index in Windows (PowerShell)
In Windows PowerShell, you can treat the bare listing as an array and
index it directly. Keep in mind that PowerShell arrays use zero-based
indexing, so file index 3 corresponds to index
[2]:
Extract to Current Directory
$index = 2
unrar e archive.rar ((unrar vb archive.rar)[$index])Extract with Full Directory Paths
$index = 2
unrar x archive.rar ((unrar vb archive.rar)[$index])Handling Files with Spaces or Special Characters
Because the filename is captured dynamically, filenames containing spaces or symbols can cause argument errors if unquoted. Always ensure the subshell expansion is wrapped in double quotes:
unrar e archive.rar "$(unrar vb archive.rar | sed -n '5p')"This ensures the filename is read as a single argument regardless of whitespace or special characters inside the RAR archive.