How to Extract Multiple Zip Files and Show Progress in Linux
Streamlining the Extraction Process and Enhancing User Experience with Linux Commands
Extracting multiple zip files can be a time-consuming and tedious task, especially if you have a large number of files to extract. Fortunately, Linux provides several tools that can make this task much easier. In this tutorial, we will show you how to extract multiple zip files and show progress using the pv
command.
Prerequisites
Before we get started, you will need to make sure that your system has the unzip
and pv
commands installed. If you're using a Debian-based system like Ubuntu, you can install these commands using the following command:
sudo apt-get install unzip pv
Extracting Multiple Zip Files
To extract multiple zip files, we can use a shell script that loops through all the zip files in a directory and extracts them to a directory with the same name without the .zip
extension. Here's an example script:
#!/bin/bash
# loop through all zip files in the current directory
for file in *.zip
do
# extract the zip file to a directory with the same name without .zip extension
unzip -q "$file" -d "${file%.*}"
# remove the original zip file to save space
rm "$file"
done
This script uses the for
loop to iterate over all zip files in the current directory. The ${file%.*}
syntax expands to the value of $file
with the last period (.
) and everything after it removed, effectively removing the .zip
extension. The unzip
command extracts the contents of the zip file to a directory with the same name as the zip file without the .zip
extension. Finally, the rm
command removes the original zip file to save disk space.
Showing Progress
To show progress while extracting zip files, we can use the pv
command in combination with unzip
. The pv
command takes input on stdin (in this case, the output of unzip
) and shows progress to the user. Here's an updated version of the previous script that shows progress:
#!/bin/bash
# loop through all zip files in the current directory
for file in *.zip
do
# extract the zip file to a directory with the same name without .zip extension,
# and show progress using pv
unzip -q "$file" -d "${file%.*}" | pv -bep -s $(unzip -l "$file" | awk 'END {print $1}' | tr -d ',') > /dev/null
# remove the original zip file to save space
rm "$file"
done
This script uses the pv
command to show progress while extracting the zip files. The -bep
flags tell pv
to show the ETA (estimated time of arrival), the progress bar, and the transfer rate. The -s
flag specifies the total size of the input in bytes, which is obtained by running unzip -l "$file" | awk 'END {print $1}' | tr -d ','
. This command extracts the file list from the zip file using unzip -l
, selects the first field of the last line using awk
, and removes any commas using tr
.
Conclusion
In conclusion, extracting multiple zip files can be a daunting task, especially if you have a large number of files to extract. However, with the help of the unzip
and pv
commands in Linux, the process can be made much easier and even show progress. By following the steps outlined in this tutorial, you can save time and disk space while extracting large numbers of files. If you have any questions or feedback, feel free to leave a comment.