Quoted from man unzip
of version UnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler.
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory
(always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the nor‐
mal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note
that this may cause normal shell behavior to be suppressed. In particular, ``-d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but ``-d~'' is treated as a
literal subdirectory ``~'' of the current directory.
Never used unzip
, but it seems like it is possible to run the following:
for f in dir/*.zip; do
# Creating a name for the new directory.
new_dir="${f##*/}"
new_dir="${new_dir%.*}"
# Creating the directory if it doesn't already exist.
mkdir -p "$new_dir"
# Unzip contents of "$f" to "$new_dir"
unzip -d "$new_dir" -- "$f"
done
Which will iterate over all zip files inside a directory called dir, create a new directory inside the present-working-directory (or PWD) named after the base-name of the zip file - if doesn't already exist, and will unzip
the contents of the zip file to the created or already exists directory.
Let's test it:
Inside a directory called test I created two archives: a.zip and b.zip.
a.zip contains the files 1.txt, 2.txt and 3.txt.
b.zip contains the files 4.txt, 5.txt and 6.txt.
for f in test/*.zip; do new_dir="${f##*/}"; new_dir="${new_dir%.*}"; mkdir -p "$new_dir"; unzip -d "$new_dir" -- "$f";done
Archive: test/a.zip
extracting: a/1.txt
extracting: a/2.txt
extracting: a/3.txt
Archive: test/b.zip
extracting: b/4.txt
extracting: b/5.txt
extracting: b/6.txt