How to Prevent Unrar Overwriting Files

Extracting RAR archives using the command-line utility unrar can inadvertently overwrite files with identical names in your destination directory. This guide explains how to protect your data by preventing unrar from overwriting existing files during extraction, utilizing specific command switches such as -o- to skip existing files and -or to automatically rename conflicting files.

The Skip Overwrite Switch: -o-

The most direct way to prevent unrar from replacing any existing file is to use the -o- switch. When this flag is passed, the utility automatically skips any file in the archive that already exists in the destination folder without asking for confirmation.

To extract with full directory paths while skipping existing files:

unrar x -o- archive.rar

To extract all files directly into the current directory without subdirectories:

unrar e -o- archive.rar

Note: The minus sign immediately following the letter o specifies the "do not overwrite" behavior. Be careful not to use -o+, which forces unrar to overwrite existing files without prompting.

The Auto-Rename Switch: -or

If you want to keep your existing files but still need to extract the incoming conflicting files, use the -or (overwrite rename) switch. This option automatically renames the extracted file by appending a unique number to the filename if a file with the same name already exists.

unrar x -or archive.rar

For example, if document.txt already exists in your destination folder, the extracted file will be saved as document(1).txt.

Interactive Mode (Default Behavior)

If you run unrar without specifying any overwrite switches, it runs in interactive mode when a name collision occurs:

unrar x archive.rar

When a duplicate file is detected, unrar halts and prompts you with the following options:

  • [N]o: Skip the current conflicting file.
  • n[E]ver: Skip all remaining conflicting files for the rest of the extraction session.
  • [R]ename: Manually enter a new name for the file being extracted.
  • [Q]uit: Stop the extraction process entirely.

Best Practice for Automation and Scripts

When using unrar in shell scripts or automated workflows, relying on interactive prompts will cause the script to hang indefinitely. Always supply the -o- flag to ensure the process completes unattended while guaranteeing your existing files are never replaced.