Shell TAR文件来自列表或使用管道

Shell TAR文件来自列表或使用管道,shell,tar,broken-pipe,Shell,Tar,Broken Pipe,在列出了一堆文件之后,我试图通过管道传输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 r

在列出了一堆文件之后,我试图通过管道传输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
只给出了文件路径和名称。 然后我试着用烟斗给这个列表涂焦油,但没有成功

所以我将列表导出到一个文件中

ll /home/dan/test/dir*/file* | grep 2014 | cut -f 2- -d "/" | sed 's/home/\/home/' > list_files_2014.txt
现在,我需要给这个列表加焦油

但当我尝试时,它会给我一个错误:

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
有人能帮我吗?
谢谢

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

但这不是你的问题

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


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

什么意思?
bzip2
未找到。安装它或者不使用
-j
选项
tar
如果没有安装bzip2,哪个bzip2的输出是什么