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
Extract files matching the pattern looo from the RAR archive archive.rar to the specified path
Delete all files except RAR files inside the current directory
Extract 25 RAR archive files at once
List all JPG files inside archive.rar without extracting
- List by extension
Extract a specific file from archive.rar
Freshen (update) any files in the destination directory that are older or missing
Read additional filter masks from stdin and list all Passwords.txt from archive.rar
Read additional filter masks from stdin and extract all Passwords.txt from archive.rar
Read additional filter masks from stdin and extract all files matching the filter masks
Diff two archives and dump matched files only
List directories up to a maximum depth of 2 using awk
Verbosely list archive contents
Rename all duplicate files to avoid overwriting
#!/bin/bash
#
# 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
# 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