Option 1: Download and unzip script
You can create a pretty simple script to do this. You could create the script dlext
(short for "Download-Extract") with this content:
#!/bin/bash
wget "$1"
filename="${1##*/}"
if [[ -f "./$filename" ]]
then
case "$filename" in
*.zip)
echo "Zip file detected in ./$filename"
unzip "./$filename"
rm "./$filename"
;;
*)
echo "Unknown file format in ./$filename"
;;
esac
else
echo "File not written to disk: ./$filename"
fi
Make the script executable, and put it in your path. In addition, unzipping files requires the package unzip
to be installed (install with sudo apt install unzip
).
This script is run with
dlext https://path.to/zipfile.zip
It will then fetch the file (with wget
), check if the file was written to disk (the if
-statement), and if true run an extraction command depending on the file extension (the case
-statement).
I've deliberately made a case
-statement for the file format, so you easily can add other file formats beside zip as well.
Option 2: Monitor downloads folder script
If you instead want to monitor a specific folder for zip files (or other files) and run a command when a new file is added to the folder, create the dlmon
script instead:
#!/bin/bash
dlfolder="/path/to/downloads"
while inotifywait -e close_write "$dlfolder"
do
# Create a for-loop for each file type you want to process
for f in "$dlfolder"*.zip
do
[[ -f "$f" ]] && unzip "$f" -d "$dlfolder" && rm "$f"
done
done
This script can be run in the background, and possibly on startup using:
/path/to/script/dlmon &
Now files of the designated types that are downloaded in the downloads folder will be processed accordingly.