Score:0

Unzip a file as soon as it is downloaded

tr flag

Do we have any utility (or) can we write any script such that if any zip file is downloaded, it is immediately unzipped and the zip file is deleted.

Rajesh Keladimath avatar
dz flag
You can check this link once https://www.techpond.in/automatically-unzip-file-using-inoticoming-ubuntu
Score:1
vn flag

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.

DynoMonster avatar
tr flag
I see that the command `/path/to/script/dlmon &` should be executed everytime my system is up. Do we have any alternatives such that I do not need to run it everytime my system is up?
Artur Meinild avatar
vn flag
You can start it with `cron` or a similar mechanism. Please research this, search the site, and ask a new question if something is not already covered.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.