How to unzip a file (ex: foo.zip
) to a folder with the same name (foo/
)?
Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That's how Mac's unzip utility works and I want to do the same in CLI.
Best Answer
I use
unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:You can force the creation of a directory in all cases with the
-d
option:Alternatively, a function can do this with
unzip
:The
line removes the
.zip
extension, with no regard for anything else (sofoo.zip
becomesfoo
, and~/foo.zip
becomes~/foo
). Theparameter expansion removes anything up to the last
/
, so~/foo
becomesfoo
. This means that the function extracts any.zip
file to a directory named after it, in the current directory. Useunzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.