Ultimate Guide to Extracting RAR Archives in Linux
Discover the power of the unrar command in Linux with this comprehensive guide. Learn how to list all folders/files in RAR archives, extract files matching specific patterns, uninstall UnRAR, extract multiple RAR files at once, test archive files, retrieve comprehensive information about RAR files, and much more.
List all folders/files in rar archives that match "Passwords.txt" and print to stdout
find /path -name "*.rar" -print0 | xargs -0 -P100 -I{} unrar p -inul "{}" "*sswords*"
Extract files matching the pattern looo from the RAR archive archive.rar to the specified path
unrar e -or archive.rar *looo* passwords/
Delete all files except RAR files inside the current directory
find . -type f ! -name "*.rar" -exec rm {} +
Uninstall UnRAR
sudo apt-get remove unrar
Extract RAR archive
unrar x foo.rar
Extract 25
RAR archive files at once
find . -maxdepth 1 -name '*.rar' -print0 | xargs -0 -I {} -P 25 unrar x {}
List all JPG files inside archive.rar without extracting
- List by extension
unrar lb archive.rar *.jpg
Extract a specific file from archive.rar
unrar e archive.rar */foo.txt
unrar e archive.rar *foo.txt
unrar e archive.rar foo.txt*
Find folders starting with a character
unrar -lb archive.rar | grep ^[A-Z]
Read files on the fly
unrar p archive.rar */foo.txt*
Extract the archive inside the current directory
unrar e -y archive.rar
Extract the archive and its folder layout
unrar x archive.rar
Extract all files and convert files and folders to lowercase
unrar e -cl archive.rar
Extract all files and convert files and folders to uppercase
unrar e -cu archive.rar
Specify the character set as UTF-8
unrar x -scUTF-8 archive.rar
Add files or paths to an existing RAR file
unrar -u archive.rar file1.txt directory/
Test archive files
unrar t archive.rar
Extract to a destination path
unrar x archive.rar destination_path
Freshen (update) any files in the destination directory that are older or missing
unrar f archive.rar
Read additional filter masks from stdin and list all Passwords.txt from archive.rar
unrar lb archive.rar -r n@ Passwords.txt
Read additional filter masks from wordlist.txt and list them
unrar lb archive.rar -r n@wordlist.txt
Read additional filter masks from stdin and extract all Passwords.txt from archive.rar
unrar x archive.rar -r n@ Passwords.txt
Read additional filter masks from stdin and extract all files matching the filter masks
unrar x archive.rar -r n@wordlist.txt
Read output on the fly
unrar p archive.rar | less
Retrieve comprehensive information about the files within a RAR archive
unrar l -slt archive.rar
Diff two RAR files
diff -y <(unrar lb archive.rar) <(unrar lb archive2.rar) | head -n50
Diff two archives and dump matched files only
diff -y <(unrar lb archive.rar) <(unrar lb archive2.rar) | grep "|"
List directories up to a maximum depth of 2 using awk
unrar l archive.rar | awk 'BEGIN {FS="/" } !NF || NF<=3'
Verbosely list archive contents
unrar e -va archive.rar
unrar e -vb archive.rar
unrar e -vt archive.rar
Rename all duplicate files to avoid overwriting
#!/bin/bash
# Author: wuseman
#
# Description:
#
# This script helps ensure that duplicate files extracted from the RAR archive are not overwritten.
# It uses a loop to extract the log.txt file, perform modifications, and rename the file with a
# counter to create unique filenames for each extraction. The loop continues until there are no
# more files matching the criteria. This prevents any extracted files from being overwritten and
# allows you to retain all versions of the extracted files.
#
counter=1
while unrar e -or archive.rar '*/log.txt' $dir/ |
grep -i log.txt |
sed '
s/OK/[OK]/;
s:.*/::;
s/log.txt/logs.txt/g'
do
mv logs.txt "logs($counter).txt"
((counter++))
done
Parallel extraction of RAR files
In this script, we define a function extract_rar
that takes two arguments: the RAR file to extract and the destination directory. The unrar
command is used to extract the RAR file in the background by appending &
to the command.
We then call the extract_rar
function for each RAR file you want to extract, providing the appropriate file and destination arguments. Replace "file1.rar"
, "/path/to/destination1"
, "file2.rar"
, and "/path/to/destination2"
with your actual RAR file names and destination directories.
The wait
command is used to ensure that the script waits for all the background processes to finish before exiting.
This script allows the two RAR files to be extracted in parallel, maximizing efficiency by utilizing multiple processes.
#!/bin/bash
# Author: wuseman
#
# Description: This script demonstrates how to extract two RAR files in parallel.
# Function to extract RAR file in the background
extract_rar() {
unrar x "$1" "$2" & # Extract the RAR file in the background
}
# Call the function for each RAR file
extract_rar "file1.rar" "/path/to/destination1" # Replace "file1.rar" and "/path/to/destination1" with your desired values
extract_rar "file2.rar" "/path/to/destination2" # Replace "file2.rar" and "/path/to/destination2" with your desired values
# Wait for all background processes to finish
wait # Wait for all the extraction processes to complete