Unzip and Unrar Multiple Files in Linux - A Quick Guide

Automating File Extraction with Bash Loops
When dealing with a directory containing numerous compressed files – whether in .zip, .rar, or other formats – the bash shell provides a streamlined method for extracting them all with a single command.
This is achieved through the utilization of bash's for loop structure. The basic syntax involves substituting with a variable name and with a list of files or a command generating a list.
for in
do
command $;
done
Alternatively, the loop can be executed on a single line for conciseness.
for in ;do command $;done
Unrarring Multiple Files
To extract a series of .rar files, the following command can be employed. Using quotes around the filename is recommended, particularly when filenames contain spaces or special characters.
for f in *.rar;do unrar e "$f";done
Extracting with 7zip
If 7zip is your preferred extraction tool, the command structure remains similar, adapted for the .001 file extension often associated with 7zip archives.
for f in *.001;do 7z e "$f";done
Unzipping Multiple Archives
For extracting multiple .zip files, the command is straightforward.
for f in *.zip;do unzip "$f";done
Chaining Commands for Complex Tasks
The bash shell allows for the chaining of commands, enabling more complex operations. For example, you can unzip all .zip files containing .txt files and then move those extracted .txt files to a designated directory.
for f in *.zip;do unzip "$f";done; for f in *.txt;do mv "$f" /myfolder/;done
This demonstrates a fraction of the bash shell’s capabilities. It provides a powerful and efficient means of automating file extraction and manipulation.