我有一个bzip2
版本的tar文件和一个带有文件列表的文本文件。我想从tar中提取文本文件中列出的文件,将它们添加到新的tar中,然后从第一个tar中删除它们。
例如,如果我有一个这样的tar文件:
$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
还有一个文本文件,其中包含以下文件列表:
$ cat files_to_extract
one/test/b
one/test/e/
one/test/c/b
one/test/c/d/a
完成后,原始tar文件应如下所示:
$ tar -tvf test.tar.bz2
drwxrwxrwx nacho/nacho 0 2018-11-16 23:30 one/test/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/a
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/c/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/a
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:25 one/test/c/d/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/b
新的tar文件应该是什么样子。
$ tar -tvf new.tar.bz2
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/b
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/b
drwxrwxrwx nacho/nacho 0 2018-11-16 23:34 one/test/e/
-rw-rw-rw- nacho/nacho 0 2018-11-16 23:25 one/test/c/d/a
请注意,文件的顺序无关紧要。
答案 0 :(得分:0)
使用GNU tar:
# unpack test.tar.bz2 to test.tar
bunzip2 test.tar.bz2
# create a working directory
mkdir tempdir
# extract desired files to tempdir
tar -C tempdir -xvf test.tar --files-from files_to_extract
# create new tar.bz2 with desired files in tempdir
tar -C tempdir -cvjf new.tar.bz2 one
# check content of new.tar.bz2
tar -tjf new.tar.bz2
最后一条命令的输出:
one/ one/test/ one/test/c/ one/test/c/b one/test/c/d/ one/test/c/d/a one/test/e/ one/test/b
# remove directory tempdir in current directory
rm -rf tempdir
# remove files in test.tar listed in files_to_extract
tar --delete -vf test.tar --files-from files_to_extract
# pack test.tar to test.tar.bz2
bzip2 test.tar
# check content of test.tar.bz2
tar -tjf test.tar.bz2
最后一条命令的输出:
one/ one/test/ one/test/c/ one/test/c/c/ one/test/c/c/a one/test/c/c/b one/test/c/a one/test/c/d/ one/test/c/d/b one/test/a
答案 1 :(得分:0)
这是我的做法:
将文件从“ files_to_extract”提取到stdout并通过管道传输到另一个tar:
tar -xOjf test.tar.bz2 -T files_to_extract | tar -cjf new.tar.bz2 -T -
解压缩,从tar中删除,然后再次bzip:
bunzip2 test.tar.bz2
tar -f test.tar --delete $(cat files_to_extract)
bzip2 test.tar
不常见的选项文档: