Tar目录内容没有父项,排除子目录/文件

简化的目录结构:

/root/test /root/test/dir1 /root/test/dir2 /root/test/dir3 /root/test/dir4 

我想tar /根/testing,以便当我提取档案,我得到dir1和dir2。 我想排除dir3和dir4以及可能的其他文件和子目录,具体取决于命令的具体应用。

我意识到networking上有很多tar例子和片段,但我似乎无法得到一个具体的例子,或者结合在线提供的这些例子和片段来完成上面的工作。 任何帮助将非常感激。

怎么样

 cd / tar cvf /tmp/test.tar --exclude="./root/test/dir[34]" ./root/test 

我想我已经设法做到这一点,把这里所说的一些实验放在一起。

 tar -cvf test.tar --exclude-from=exclude --directory="/root/test" . 

排除是一个文件与:

 dir3 dir4 

如果你有一个有限的数量的文件夹,你想要tar,你可以做

 tar -C /root/test -cf arch.tar dir1 dir2 root@pinkpony:~# ls /root/test/ dir1 dir2 dir3 root@pinkpony:~# pwd /root root@pinkpony:~# tar -C /root/test -cf arch.tar dir1 dir2 root@pinkpony:~# tar tf arch.tar dir1/ dir2/ 

编辑(使代码包含在注释中):

如果您有构build要tar焦点的目录列表的复杂规则,则可能需要启动脚本:

 root@pinkpony:~# (cd /root/test/ ; ls |grep -E '^dir(1|2)$' | xargs tar cf /tmp/blah.tar ) root@pinkpony:~# tar tf /tmp/blah.tar dir1/ dir2/ 

你可以使用任何你需要的而不是那个愚蠢的ls | grep thingie,只是一个例子; xargs是我想指出你的注意力的部分

FWIW您还可以指定一个文件列表排除一个较长的文件或目录列表。 这比调整长的命令行更容易。

查看mycrpt以获得快速,安全的encryption方法。 对不起,稍微OT。