【问题标题】:Shell TAR files from list or using pipe来自列表或使用管道的外壳 TAR 文件
【发布时间】:2020-03-09 23:49:00
【问题描述】:

在列出一堆文件后,我试图通过管道传输 TAR。 首先,我尝试根据日期列出我的文件。像这样:

ll /home/dan/test/dir*/file* | grep 2014
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir1/file1_2014_1
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir1/file1_2014_10
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir1/file1_2014_100
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir1/file1_2014_11
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir1/file1_2014_12
...
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir4/file4_2014_97
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir4/file4_2014_98
-rw-r--r-- 1 root root 0 Jan  2  2014 /home/dan/test/dir4/file4_2014_99

由于结果有点污染,我尝试对其进行过滤。

ll /home/dan/test/dir*/file* | grep 2014 | cut -f 2- -d "/" | sed 's/home/\/home/'
/home/dan/test/dir1/file1_2014_1
/home/dan/test/dir1/file1_2014_10
/home/dan/test/dir1/file1_2014_100
/home/dan/test/dir1/file1_2014_11
/home/dan/test/dir1/file1_2014_12
...
/home/dan/test/dir4/file4_2014_97
/home/dan/test/dir4/file4_2014_98
/home/dan/test/dir4/file4_2014_99

它只给出了文件路径和名称。 然后我尝试使用管道对这个列表进行 TAR,但没有成功。

所以我将列表导出到文件

ll /home/dan/test/dir*/file* | grep 2014 | cut -f 2- -d "/" | sed 's/home/\/home/' > list_files_2014.txt

现在,我需要对这个列表进行 TAR。

但是当我尝试时它给了我一个错误:

tar -cvjf test.tar.bz2 -T list_files_2014.txt
tar: Removing leading `/' from member names
/home/dan/test/dir1/file1_2014_1
/home/dan/test/dir1/file1_2014_10
/home/dan/test/dir1/file1_2014_100
/bin/sh: 1: /home/dan/test/dir1/file1_2014_11
bzip2: not found/home/dan/test/dir1/file1_2014_12

/home/dan/test/dir1/file1_2014_13
/home/dan/test/dir1/file1_2014_14
/home/dan/test/dir1/file1_2014_15
/home/dan/test/dir1/file1_2014_16
/home/dan/test/dir1/file1_2014_17
/home/dan/test/dir1/file1_2014_18
/home/dan/test/dir1/file1_2014_19
/home/dan/test/dir1/file1_2014_2
/home/dan/test/dir1/file1_2014_20
/home/dan/test/dir1/file1_2014_21
/home/dan/test/dir1/file1_2014_22
/home/dan/test/dir1/file1_2014_23
/home/dan/test/dir1/file1_2014_24
/home/dan/test/dir1/file1_2014_25
/home/dan/test/dir1/file1_2014_26
tar: test.tar.bz2: Cannot write: Broken pipe
tar: Child returned status 127
tar: Error is not recoverable: exiting now

谁能帮帮我? 谢谢

【问题讨论】:

  • 什么意思?
  • bzip2 未找到。要么安装它,要么不使用-j 选项到tar
  • which bzip2的输出是什么
  • 如果没有安装 bzip2 sudo apt-get install bzip2

标签: shell tar broken-pipe


【解决方案1】:

您不应该解析ls,因为ls 的输出不稳定,只能供人类阅读。更新操作系统后,此输出(其格式)可能会发生变化,从而使您的脚本出现错误。 @Cyrus 提供的链接导致了更详尽的解释,我不想在这里重复。无论如何,使用其他 Unix 工具来实现您的目标会更好,例如find.

但这不是你的问题。

您的问题仅仅是您的tar 尝试为您选择的压缩生成第二个进程。它试图执行一个名为bzip2 的程序。这似乎没有安装在您的计算机上,所以它失败了。消息有点延迟,因为首先创建了第二个进程,然后这个并行运行的其他进程尝试运行程序bzip2。与此同时,tar 进程已经开始打包文件(并制作了一些日志消息)。由于这两个进程是通过管道连接的,因此第一个进程在尝试写入该管道(现在缺少读取器进程)时最终会收到一个 SIGPIPE。

因此,您要么必须安装bzip2,要么只需选择另一种已安装的压缩算法。例如,您可以通过将选项 -j 替换为选项 -z 来选择 gziptar 的手册页列出了许多其他受支持的压缩算法。

【讨论】:

    猜你喜欢
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多