【问题标题】:Is it possible to simultaneously pipe the output of a tar command to a split command and md5sum check?是否可以同时将 tar 命令的输出通过管道传输到 split 命令和 md5sum 检查?
【发布时间】:2015-10-05 09:08:57
【问题描述】:

我有一个压缩命令,我将它传送到一个 md5sum 检查命令:

   tar zcvpf file.tar.gz file | xargs -I '{}' bash -c "test -f '{}' && md5sum '{}'" | tee file_mybackup.md5

我现在想将 tar.gz 文件拆分为 100MB 的块。我可以这样做:

   tar zcvpf - file | split -d -b 1M - file.tar.gz.

有没有一种方法可以将 tar 命令的输出通过管道传输到同时执行 split 命令和 md5sum 检查?我知道 split 不会输出到 STDOUT 所以我不能从 split 命令到 md5sum 命令。 我尝试使用命名管道:

    mkfifo newpipe

    tar zcvpf - file | tee newpipe | split -d -b 1M - file.tar.gz. &

    cat newpipe | xargs -I '{}' bash -c "test -f '{}' && md5sum '{}'" | tee file_mybackup.md5

但是,这无法输出 md5sum 输出。任何帮助将不胜感激

【问题讨论】:

  • command | tee >(command 2) | command 3
  • 你对xargs的使用被破坏了:你的代码被任意代码执行!
  • 好的,使用@999999999999999999999999999999 的方法:tar zcvpf - file | tee >(xargs -I '{}' bash -c "test -f '{}' && md5sum '{}'" > file.md5) | split -d -b 1M - file.tar.gz. 但是这会产生错误xargs: Warning: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option? 如何将 STDOUT 重定向到 xargs -I 命令?

标签: bash tar md5sum


【解决方案1】:

你的命令:

tar zcvpf file.tar.gz file | xargs -I '{}' bash -c "test -f '{}' && md5sum '{}'" | tee file_mybackup.md5

...没有比这个更简单的命令更有效(在最小化驱动器读取方面)(即不需要 xargs):

tar zcvpf file.tar.gz file && md5sum file | tee file_mybackup.md5

因为在这两种情况下,file 都被tar 读取一次,然后被md5sum 读取第二次

除非我在您尝试实现的目标中缺少某些东西,否则您需要最小化驱动器读取的命令是:

cat file | tee >(md5sum | sed 's/-/file/' > file_mybackup.md5) | gzip -c | split -d -b 1M - file.gz.

注意:由于您归档的是单个文件而不是目录,因此我只使用了gzip 而不是tar。是的,这不存储文件权限,并且不适用于目录! ……这是要求吗?

【讨论】:

    【解决方案2】:

    好的,我最终找到了解决方案

        tar zcvpf >(split -d -b 1M - file.) file | xargs -I '{}' bash -c "test -f '{}' && md5sum '{}'" | tee file.md5
    

    我将标准输入重定向到初始 tar 命令中的 split 命令,同时将此 tar 的输出通过管道传输到 xargs。希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2016-10-27
      • 2017-10-12
      • 2019-08-17
      • 2016-04-18
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      相关资源
      最近更新 更多