和-slt

和-slt

我想使用 7z 打印存档内文件的名称。

的输出7z l myArchive.7z

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.utf8,Utf16=on,HugeFiles=on,64 bits,4 CPUs Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz (206A7),ASM,AES-NI)

Scanning the drive for archives:
1 file, 171329 bytes (168 KiB)

Listing archive: myArchive.7z

--
Path = myArchive.7z
Type = 7z
Physical Size = 171329
Headers Size = 237
Method = LZMA2:18
Solid = +
Blocks = 1

  Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2020-06-05 16:03:29 ....A            0            0  file with spaces
2020-06-05 11:53:13 ....A        96616       171092  screen_2020-06-05_11-53-13.png
2020-06-05 11:53:43 ....A       106932               screen_2020-06-05_11-53-43.png
------------------- ----- ------------ ------------  ------------------------
2020-06-05 16:03:29             203548       171092  3 files

我想让 7z 仅打印文件名:

file with spaces
screen_2020-06-05_11-53-13.png
screen_2020-06-05_11-53-43.png

有没有办法做到这一点?

答案1

只需使用 libarchive 的bsdtar

bsdtar tf file.7z

另请注意,7z l如果存档已加密,则会提示您输入密码,而bsdtar只会返回错误,这在脚本中更可取。

答案2

-slt

这个命令

7z -slt l myArchive.7z | grep -oP "(?<=Path = ).+" | tail -n +2

印刷

file with spaces
screen_2020-06-05_11-53-13.png
screen_2020-06-05_11-53-43.png

选项-slt“[s]ets Technical mode for l (list) command”,根据手动的

此选项使 7z 以可解析的方式打印有关存档文件的信息。

这是输出7z -slt l myArchive.7z

Listing archive: file with spaces.7z

--
Path = file with spaces.7z
Type = 7z
Physical Size = 171329
Headers Size = 237
Method = LZMA2:18
Solid = +
Blocks = 1

----------
Path = file with spaces
Size = 0
Packed Size = 0
Modified = 2020-06-05 16:03:29
Attributes = A_ -rw-r--r--
CRC = 
Encrypted = -
Method = 
Block = 

Path = screen_2020-06-05_11-53-13.png
Size = 96616
Packed Size = 171092
Modified = 2020-06-05 11:53:13
Attributes = A_ -rw-r--r--
CRC = 41911DBA
Encrypted = -
Method = LZMA2:18
Block = 0

Path = screen_2020-06-05_11-53-43.png
Size = 106932
Packed Size = 
Modified = 2020-06-05 11:53:43
Attributes = A_ -rw-r--r--
CRC = B0ECEA85
Encrypted = -
Method = LZMA2:18
Block = 0

命令的 grep 部分| grep -oP "(?<=^Path = ).+"需要解释:

  • -o: grep 只打印匹配的字符串,而不打印整行。
  • P: 使能够Perl 兼容的正则表达式在 grep 中。我们需要这个来进行正则表达式中的lookbehind。
  • (?<=^Path = ).+": grep 的正则表达式。获取以“Path =”开头的行之后的所有字符。该(?<=部分是一个积极的后视这意味着该行必须以“Path =”开头,但该字符串不是匹配的一部分。后面的字符是匹配的字符串。这些字符是文件名。

之后,第一行是存档名称,下面是所有文件名。我们删除第一行| tail -n +2

相关内容