How to get a list of directories in a zip? How to get a list of directories in a zip? linux linux

How to get a list of directories in a zip?


To list only directories:

unzip -l foo.zip "*/"

Output (e.g.):

Archive:  foo.zip  Length      Date    Time    Name---------  ---------- -----   ----        0  2015-09-10 20:10   work/        0  2015-08-31 10:45   work/test1/        0  2015-08-31 10:50   work/test1/cc/        0  2015-08-31 10:45   work/test1/dd/        0  2015-08-31 10:45   work/test1/aa/        0  2015-08-31 10:45   work/test1/bb/        0  2015-09-09 21:17   work/tmp/        0  2015-08-23 18:49   work/tmp/work/        0  2015-09-08 19:33   work/tmp/work/loop/        0  2015-08-15 16:00   work/tmp/work/1/        0  2015-08-15 16:00   work/1/        0  2015-08-24 18:40   work/dir/        0  2015-09-05 18:07   work/rename/---------                     -------        0                     13 files

or use

zipinfo -1 foo.zip "*/"

Output (e.g.):

work/work/test1/work/test1/cc/work/test1/dd/work/test1/aa/work/test1/bb/work/tmp/work/tmp/work/work/tmp/work/loop/work/tmp/work/1/work/1/work/dir/work/rename/


You can try piping unzip -l to awk like this:

unzip -l foo.zip | awk '/\/$/ { print $NF }'

All the directories in the unzip -l output end with a slash and $NF prints just the directory path.


On Cygwin

I coudn't get either of these to work on my Cygwin. For my immediate use, where I only needed to get one directory deep:

zipinfo -1 foo.zip | grep -o "^[^/]\+[/]" | sort -u

For other directories, you could use xargs in combination with some other parsing thing, something like:

## PSEUDOCODE$ zipinfo -1 foo.zip | \# from the pipe, run xargs \# do something like the following while loop, going one file at a timewhile there is still a '/' in the line; do  parse off "[^/]+([/]|)$" from the line  print out the new linedone | \sort -u

System Info

$ uname -aCYGWIN_NT-10.0 C-D-ENG-E-INT3 2.11.2(0.329/5/3) 2018-11-08 14:34 x86_64 Cygwin$ bash --versionGNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)Copyright (C) 2016 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software; you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.$ systeminfo | sed -n 's/^OS\ *//p'Name:                   Microsoft Windows 10 EnterpriseVersion:                10.0.17134 N/A Build 17134Manufacturer:           Microsoft CorporationConfiguration:          Member WorkstationBuild Type:             Multiprocessor Free$