How to Use Unrar -p for Password Protected RARs
The unrar command-line utility allows users to extract
RAR archives across Unix-like operating systems, and its -p
switch specifically handles archives secured with passwords. This guide
explains how to apply the switch to supply passwords directly in the
terminal, prompt for hidden input to protect credentials from command
history, and automatically reject password prompts when necessary.
Basic Syntax of the
-p Switch
The -p switch modifies the extraction commands
x (extract with full path structure) and e
(extract all files to the current directory).
The general syntax is:
unrar x -p[password] archive.rar [destination_directory/]Important: Do not place a space between the
-pswitch and the password string. Placing a space will causeunrarto treat the password as a missing argument or target file name.
Method 1: Provide the Password Directly
If you want to extract an archive non-interactively (such as in a
shell script), attach the password immediately after the -p
switch:
unrar x -pMySecretPassword archive.rarTo extract the files into a specific directory, append the target path to the end:
unrar x -pMySecretPassword archive.rar /path/to/destination/Note: Entering cleartext passwords in the terminal makes them
visible in process lists (ps) and shell history files
(~/.bash_history).
Method 2: Mask the Password via Interactive Prompt (Recommended)
To protect your credentials from appearing in your command history,
supply the -p switch without a password. The utility will
prompt you for the password and mask your input while typing:
unrar x -p archive.rarTerminal Output:
Enter password (will not be echoed) for archive.rar:
Type your password and press Enter to begin extraction.
Method 3: Disable Password Prompts
If you want to process archives automatically and skip any files that
require a password, use the -p- switch. This explicitly
specifies a blank or null password and skips interactive prompting:
unrar x -p- archive.rarIf the archive requires a password, the operation will fail immediately rather than waiting for user input, which is useful for background batch jobs.