【问题标题】:Pipe command Find + Grep + Xargs + Tar (extract)管道命令 Find + Grep + Xargs + Tar(提取)
【发布时间】:2018-12-11 20:27:13
【问题描述】:

我不明白为什么这个 bash 代码不起作用。

find -type f | grep file_ | xargs -0 -I file tar -zxf file --directory /tmp

我有这个错误:

tar (child): ./file_ok.tar.gz\n : open 
impossible: No file or folder of this type
tar (child): Error is not recoverable: 
exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now

【问题讨论】:

  • 我添加了我的错误
  • find 有一个内置的搜索功能,这就是为什么它被称为“查找”,所以这里的 grep 是多余的。 find 也可以将它发现的内容本地发送到另一个程序,因此到 xargs 的管道也是多余的。像find -type f -name "*file_*" -exec tar -zxf {} --directory /tmp \; 这样的东西应该可以解决问题。这并不能解释为什么你会看到一个换行符,但我打赌它会解决它。
  • 完美,我学到了一些东西。感谢您的帮助!
  • 我认为问题在于您使用 xargs -0(期望 NUL 字节分隔的参数),但是您在任何地方都不会产生 NUL 字节(必须是 find -print0grep -z)。 xargs 有效地获得了一个包含换行符的长字符串,因此 \n tar 抱怨。

标签: bash ubuntu debian


【解决方案1】:

感谢 JNevill,该命令有效,我们可以在 find 命令中执行所有这些操作

find -type f -name "*file_*" -exec tar -zxf {} --directory /tmp \;

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多