Linux `unzip -l` 时提取 zip 存档中的文件名列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7148604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:50:20  来源:igfitidea点击:

Extract list of file names in a zip archive when `unzip -l`

linuxunixksh

提问by Thang Pham

When I do unzip -l zipfilename, I see

当我这样做时unzip -l zipfilename,我看到

1295627  08-22-11 07:10   A.pdf
473980  08-22-11 07:10   B.pdf
...

I only want to see the filenames. I try this

我只想看到文件名。我试试这个

unzip -l zipFilename | cut -f4 -d" "

but I don't think the delimiter is just " ".

但我不认为分隔符只是" ".

采纳答案by glenn Hymanman

Assuming none of the files have spaces in names:

假设所有文件的名称中都没有空格:

unzip -l filename.zip | awk '{print $NF}'

My unzip output has both a header and footer, so the awk script becomes:

我的解压输出有页眉和页脚,所以 awk 脚本变成:

unzip -l filename.zip | awk '/-----/ {p = ++p % 2; next} p {print $NF}'


A version that handles filenames with spaces:

处理带空格的文件名的版本:

unzip -l filename.zip | awk '
    /----/ {p = ++p % 2; next}
    $NF == "Name" {pos = index(
unzip -l zipfilename | awk '{print }'
,"Name")} p {print substr(
unzip -l zipfilename.zip | awk -v f=4  ' /-----/ {p = ++p % 2; next} p { for (i=f; i<=NF;i++) printf("%s%s", $i,(i==NF) ? "\n" : OFS) }'
,pos)} '

回答by Manny D

Use awk:

使用 awk:

unzip -Z -1 archive.zip

回答by peteretep

If you need to cater for filenames withspaces, try:

如果您需要处理带有空格的文件名,请尝试:

zipinfo -1 archive.zip

回答by amicitas

The easiest way to do this is to use the following command:

最简单的方法是使用以下命令:

##代码##

or

或者

##代码##

This will list only the file names, one on each line.

这将只列出文件名,每行一个。

The two commands are exactly equivalent. The -Zoption tells unzip to treat the rest of the options as zipinfo options. See the man pages for unzipand zipinfo.

这两个命令完全等效。该-Z选项告诉 unzip 将其余选项视为 zipinfo 选项。请参阅unzipzipinfo的手册页。