如何获取zip目录列表?

问题描述 投票:9回答:3

我正在寻找一种在Linux下bash的zip文件中列出目录的方法。我发现有一个名为zipinfo的应用程序,它可以在zip中列出路径(无需解析(zipinfo -1 foo.zip)(而不是unzip -l foo.zip)有任何多余的噪音)。但是,这还不够好,我想知道是否有更好的方法。

linux bash zip unzip
3个回答
12
投票

仅列出目录:

unzip -l foo.zip "*/"

输出(例如):

存档:foo.zip长度日期时间名称--------- ---------- ----- ----0 2015-09-10 20:10工作/0 2015-08-31 10:45工作/测试1 /0 2015-08-31 10:50工作/测试1 / 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工作/ tmp /工作/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工作/ 1 /0 2015-08-24 18:40工作/目录/0 2015-09-05 18:07工作/重命名/--------- -------0 13文件

或使用

zipinfo -1 foo.zip "*/"

输出(例如):

工作/工作/测试1 /工作/测试1 / cc /工作/测试1 / DD /工作/测试1 / AA /工作/测试1 / BB /工作/ tmp /工作/ tmp /工作/工作/ tmp /工作/循环/工作/ tmp /工作/ 1 /工作/ 1 /工作/目录/工作/重命名/

1
投票

您可以尝试将unzip -l管道输送到awk

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

unzip -l输出中的所有目录均以斜杠结尾,$NF仅打印目录路径。


0
投票

关于Cygwin

我无法在我的Cygwin上使用任何一个。为了立即使用,我只需要深入一个目录即可:

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

对于其他目录,可以将xargs与其他解析内容结合使用,例如:

## 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

系统信息

$ 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

$
© www.soinside.com 2019 - 2024. All rights reserved.