4

I am trying to unzip files supplied by Google Takeout and need a command to unzip all zip files in a directory to a directory of the same name as the zip file to avoid conflicts. The zip names cannot be pre-determined so therefore neither can the resulting unzipped directories.

Eg

-zipstore
----zipname1.zip
----zipname2.zip

should result in

-zipstore
----zipname1.zip
----zipname1
----zipname2.zip
----zipname2

Where zipname1 and zipname2 are the directories containing the unzipped files.

A further consideration is how do I detect if the zip has finished downloading before extracting - these are 10Gb files !!

muru
  • 197,895
  • 55
  • 485
  • 740

1 Answers1

7

AFAIK, unzip doesn't support that natively, but it supports specifying an extraction directory name with -d.

So, for extracting single archives, you can define your own function making use of the shell's parameter expansion to strip out the extension part i.e. .zip in the filename like this:

myunzip () 
{ 
    unzip "$1" -d "${1%.zip}"
}

... and then use it like so:

myunzip zipname1.zip

or use a shell loop for extracting all archive files in the current working directory at once like so:

for a in *.zip; do unzip "$a" -d "${a%.zip}"; done

However, to constantly monitor a certain download directory and automatically unzip completed zip archive files downloads, you'll need to set a watch on that directory using e.g. inotifywait with its close_write event (to wait until the archive file has completely downloaded and written to disk). Which you can implement in a script like:

#!/bin/bash

inotifywait -m -q -e close_write /path/to/dir/ | while read -r path action file; do if [[ "$file" =~ .*.zip$ ]]; then unzip "$path$file" -d "${path}${file%.zip}" fi done

Raffa
  • 32,237
  • testing now, thanks. A further consideration is how do I detect if the zip has finished downloading before extracting - these are 10Gb files !! – Datadimension Feb 08 '24 at 18:56
  • @Datadimension One way is the close_write event of inotifywait … See examples https://askubuntu.com/a/1443575 and https://askubuntu.com/a/1418903 – Raffa Feb 08 '24 at 19:07