我有一个包含文件名列表的 txt 文件。在 bash 中,如何仅从 zip 文件中解压缩列表中指定的那些文件?
问问题
1267 次
2 回答
4
这应该有效:
unzip -q /path/to/zipfile $(cat thetxtfile)
当然,该命令需要在一个目录中发出,该目录最好一开始就为空。
请注意,如果您的文件名中有空格,这将不起作用,您需要这样做:
while read thefile; do unzip -q /path/to/zipfile "$thefile"; done <thetxtfile
于 2013-01-04T11:57:14.163 回答
2
如果您在命令行处理文件列表,xargs
这几乎总是最好的答案——它干净地处理带有空格的文件名,并且它绕过了参数数量的限制。我建议这样做:
tr '\n' '\0' <filelist.txt | xargs -0 unzip -q /path/to/zipfile
使用tr '\n' '\0' <filelist.txt
获取您的文件列表并将 nul 字符替换为新行。xargs -0
读取以 nul 分隔的文件列表并将其添加到以下命令的 agument 列表中。
如果您的文件名列表使用 '\r\n' 或 '\r' 样式的行尾,这将中断。
于 2013-01-04T12:28:19.770 回答