在要归档的目录的子目录中创建tar文件

时间:2011-06-28 21:09:04

标签: unix tar non-recursive

我想创建目录中所有文件的tar文件,减去该目录中的子目录,并将该tar文件放在其中一个子目录中。例如,我在/ test中有几个.txt文件,在/ test中有另一个名为ArchivedFiles的目录。我想告诉tar命令存档所有.txt文件并将其放在/ test / ArchivedFiles中。
我该怎么做呢?

2 个答案:

答案 0 :(得分:1)

tar cf test/foo/test.tar -- `find test  -maxdepth 1 -name '*.txt' -type f`

我认为应该做你想做的事。

由于tar命令的使用年限而无效的选项是:

find test -maxdepth 1 -type f -name '*.txt' -print0 | tar -cf test/foo/test.tar --null --files-from -

您遇到问题,因此可以尝试以下命令:

tar cf test/foo/test.tar `find test  -maxdepth 1 -name '*.txt' -type f`
echo tar cf test/foo/test.tar `find test  -maxdepth 1 -name '*.txt' -type f`
tar c f test/foo/test.tar `find test  -maxdepth 1 -name '*.txt' -type f`
find test  -maxdepth 1 -name '*.txt' -type f

然后粘贴输出,以便我们可以看到正在发生的事情。

鉴于find也非常遗留,让我们尝试以下方法:

tar cf test/foo/test.tar test/*.txt

答案 1 :(得分:0)

以下命令将起作用。它会将所有子目录放入tar中,但不会将这些子目录的内容放入tar中。这意味着如果你将tar放入一个子目录中,你就不必担心它会把它自己置于其自身内部......

例如,如果要将当前目录中的所有文件放入名为mytar.tar的tar文件中,该文件位于名为d1的子目录中:

tar --no-recursion -cvf d1/mytar.tar *