unzip -l
显示所有目录和子目录/文件。
我只想列出 zip 文件大小的第一个目录结构,类似于-maxdepth 1
命令find
。
有没有什么方法可以在没有完整脚本的情况下做到这一点?
答案1
至少UnZip 6.00
在基于 Debian 的发行版中,您可以使用-x
带有通配符的选项,如下所述man unzip
:
[-x xfile(s)] An optional list of archive members to be excluded from process‐ ing. Since wildcard characters normally match (`/') directory separators (for exceptions see the option -W), this option may be used to exclude any files that are in subdirectories. For example, ``unzip foo *.[ch] -x */*'' would extract all C source files in the main directory, but none in any subdirectories. Without the -x option, all C source files in all directories within the zipfile would be extracted.
例如,给定
$ unzip -l dir
Archive: dir.zip
Length Date Time Name
--------- ---------- ----- ----
0 2023-03-11 07:57 dir/
24 2023-03-11 12:50 dir/file
1464 2023-03-04 12:51 dir/input.csv
187 2023-03-06 09:03 dir/input
0 2023-02-28 10:25 dir/subdir3/
0 2023-02-28 10:25 dir/subdir3/Files.txt
55 2023-02-28 10:51 dir/file2
26 2023-03-10 20:00 dir/utf-16BE.txt
0 2023-02-28 10:25 dir/subdir2/
0 2023-02-28 10:25 dir/subdir2/File.txt
0 2023-02-28 10:25 dir/subdir1/
0 2023-02-28 10:25 dir/subdir1/file.txt
585 2023-03-01 06:54 dir/source.CSV
115 2023-02-28 10:50 dir/file1
211 2023-03-10 09:01 dir/01-netcfg.yaml
--------- -------
2667 15 files
然后
$ unzip -l dir -x '*/*/*'
Archive: dir.zip
Length Date Time Name
--------- ---------- ----- ----
0 2023-03-11 07:57 dir/
24 2023-03-11 12:50 dir/file
1464 2023-03-04 12:51 dir/input.csv
187 2023-03-06 09:03 dir/input
55 2023-02-28 10:51 dir/file2
26 2023-03-10 20:00 dir/utf-16BE.txt
585 2023-03-01 06:54 dir/source.CSV
115 2023-02-28 10:50 dir/file1
211 2023-03-10 09:01 dir/01-netcfg.yaml
--------- -------
2667 9 files
答案2
像这样,在一个MCVE方式:
curl -s https://gist.githubusercontent.com/sputnick-dev/1d2303e5fb75af8792f828ff33c127af/raw/e42cd4f56ce1b430752269cff2571342b19eb58a/gistfile1.txt | bash
cd /tmp
zip -r rand_dirs.zip rand_dirs
unzip -l rand_dirs.zip |
grep -oP '^\s+\d+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+\K[^/]+/[^/]+' |
sort -u
输出
rand_dirs/Yqol
rand_dirs/yT3oVke
rand_dirs/yt7p
rand_dirs/z
rand_dirs/z8Tx
rand_dirs/ZaQ7cE
rand_dirs/zeWgeCr8RbS
rand_dirs/zVf4
[...]