Extract RAR Files with Full Paths Using Unrar
When extracting RAR archives using the command-line utility
unrar, directory hierarchies can either be reconstructed
exactly as packaged or flattened into a single folder. This guide
explains how unrar handles full directory paths during
extraction, the distinction between commands that preserve or discard
folders, and how the utility mitigates security risks associated with
internal paths.
The x
Command: Preserving Directory Trees
To extract files along with their full directory paths,
unrar uses the x (extract with full path)
command. The basic syntax is:
unrar x archive.rar /target/directory/When this command is executed, unrar parses the path
records embedded within each file header inside the archive. As it
uncompresses data, it recreates each intermediate folder inside the
destination folder before writing the corresponding files. If any folder
in the path does not exist, unrar automatically creates it
using the default system permissions.
Contrast with the e
Command
The standard e command extracts files without their
directory paths. Running unrar e archive.rar causes
unrar to strip all folder information, depositing every
single file directly into the target root directory. If multiple files
in different subdirectories share the same filename, this command will
trigger overwrite prompts or unintentionally replace files. Therefore,
using x is the standard method for restoring an archive's
exact file layout.
Path Sanitization and Security
Older archive formats and tools were occasionally vulnerable to
directory traversal attacks, where archives containing relative paths
like ../../ or absolute paths like /etc/ could
overwrite critical system files. Modern versions of unrar
handle this by default:
- Leading Slashes and Drive Letters: By default,
unrarstrips root path markers (such as leading slashes/on Unix-like systems or drive identifiers likeC:\on Windows). The paths are converted into relative paths anchored to the specified output directory. - Path Traversal Protection: Relative traversal
elements such as
../are stripped or normalized to prevent files from writing outside the targeted destination boundary. - Force Full Paths Flag (
-ep3): In specific backup scenarios where storing and restoring absolute paths is required,unrarprovides the-ep3switch. Without this explicit flag,unrarwill never write to absolute paths outside the target folder.
Additional Path Management Switches
Several command-line options allow fine-tuning of how
unrar handles paths during extraction:
-ad(Append Archive Name): Appends the name of the archive as a top-level directory inside the target destination, ensuring the extracted structure is contained within its own folder.-ep(Exclude Paths): Equivalent to using theecommand; strips all folder data during extraction.-ep1(Exclude Base Folder): Strips the top-level directory stored in the archive while keeping all nested subdirectories beneath it intact.