如何执行 tar 压缩的试运行?

mor*_*l36 3 tar

我想执行 tar 压缩的试运行并将条目打印到 stdout 而不实际创建 tar。

到目前为止,我已经尝试过:

// just spins
$ tar t -O Downloads
Run Code Online (Sandbox Code Playgroud)
$ tar c -O Downloads
tar: Option -O is not permitted in mode -c
Run Code Online (Sandbox Code Playgroud)

tar --help 给出以下内容:

?> ~ tar --help                                                                                                                                                                                 08:06:22
tar(bsdtar): manipulate archive files
First option must be a mode specifier:
  -c Create  -r Add/Replace  -t List  -u Update  -x Extract
Common Options:
  -b #  Use # 512-byte records per I/O block
  -f <filename>  Location of archive
  -v    Verbose
  -w    Interactive
Create: tar -c [options] [<file> | <dir> | @<archive> | -C <dir> ]
  <file>, <dir>  add these items to archive
  -z, -j, -J, --lzma  Compress archive with gzip/bzip2/xz/lzma
  --format {ustar|pax|cpio|shar}  Select archive format
  --exclude <pattern>  Skip files that match pattern
  -C <dir>  Change to <dir> before processing remaining files
  @<archive>  Add entries from <archive> to output
List: tar -t [options] [<patterns>]
  <patterns>  If specified, list only entries that match
Extract: tar -x [options] [<patterns>]
  <patterns>  If specified, extract only entries that match
  -k    Keep (don't overwrite) existing files
  -m    Don't restore modification times
  -O    Write entries to stdout, don't restore to disk
  -p    Restore permissions (including ACLs, owner, file flags)
bsdtar 3.3.2 - libarchive 3.3.2 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.6
Run Code Online (Sandbox Code Playgroud)

gro*_*taj 5

tar t列出现有档案。您尚未提供任何参数 ( -f),因此它正在标准输入上等待它。人类不擅长在键盘上输入 tar 档案,因此它不是很有用。

tar c不接受-O选项。这是x帮助消息中所示的一个选项。

您可以将这两者结合起来以获得预期结果:

tar c Downloads | tar t
Run Code Online (Sandbox Code Playgroud)

没有-f参数tar c,因此它将压缩输出写入标准输出。然后,我们tar t使用之前“卡住”的模式将其通过管道传输到标准输入。


qwr*_*qwr 5

要进行仅列出文件的试运行,请使用-v详细模式列出文件并使用 /dev/null 作为输出以避免实际写入文件的时间和空间开销。-v如果您只想查看 tar 错误,请排除。

tar -cvf /dev/null mydir 
Run Code Online (Sandbox Code Playgroud)