I am looking for a way to list the directories in a zip file in bash under Linux. I found out there is an application called zipinfo
, which can list the paths in the zip (without any extra noise to have to parse through (zipinfo -1 foo.zip
), as opposed to unzip -l foo.zip
). However, this isn't good enough and I am wondering if there is a better way.
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/
zipinfo -1 foo.zip "*/"
worked best, thank you! :) –
Tova I get
caution: filename not matched: */
although there are plenty of directories in my zip file. EDIT: Is a zip file expected to contain dedicated directory entries? My file only contains file entries... –
Maziemazlack @moi: I recommend to start a new question. –
Supinate
Neither
unzip -l foo.zip "*/"
nor zipinfo -1 foo.zip "*/"
are reliable solutions: the Zip file may have been created without dedicated directory entries. For instance the zip
tool has the option -D do not add directory entries
. –
Imamate 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 time
while there is still a '/' in the line; do
parse off "[^/]+([/]|)$" from the line
print out the new line
done | \
sort -u
System Info
$ uname -a
CYGWIN_NT-10.0 C-D-ENG-E-INT3 2.11.2(0.329/5/3) 2018-11-08 14:34 x86_64 Cygwin
$ bash --version
GNU 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 Enterprise
Version: 10.0.17134 N/A Build 17134
Manufacturer: Microsoft Corporation
Configuration: Member Workstation
Build Type: Multiprocessor Free
$
The open-source Zip_Dir_List tool will list all directories for you, even for Zip archives that don't have explicit directory entries. From the link, go up to the repository and follow the build instructions in readme.txt .
© 2022 - 2024 — McMap. All rights reserved.
zipinfo -1 foo.zip
is not good enough? – Hudnut