Zip – How to Tell if Unzip Will Create a Single Folder

zip

A common scenario is having a zip file in a directory with other work:

me@work ~/my_working_folder $ ls
downloaded.zip  workfile1  workfile2  workfile3

I want to unzip downloaded.zip, but I don't know if it will make a mess or if it nicely creates its own directory. My ongoing workaround is to create a temporary folder and unzip it there:

me@work ~/my_working_folder $ mkdir temp && cp downloaded.zip temp && cd temp
me@work ~/my_working_folder/temp $ ls
downloaded.zip
me@work ~/my_working_folder/temp $ unzip downloaded.zip 
Archive:  downloaded.zip
   creating: nice_folder/

This prevents my_working_folder from being populated with lots of zip file contents.

My question is: Is there a better way to determine if a zip file contains only one folder before unzipping?

Best Answer

From the manual...

[-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 normal 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.

So...

unzip -d new_dir zipfile.zip

This creates a directory, new_dir, and extracts the archive within it, which avoids the potential mess every time even without looking first. It is also very useful to look at man unzip. More help for manual pages.

Related Question