Unzip to directory of the same name

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.



should result in


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 !!

Asked By: Datadimension


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:


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}"
Answered By: Raffa
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.