如何告诉 tar 仅排除根目录之外的目录,而不排除树中更深层次的同名目录?

jco*_*lum 6 tar directory-structure

文件夹结构:

\n\n
etc (dir)\ndeploy (dir)\nsrc (dir)\nconfig (dir)\ndist\n \xe2\x94\x9c- config (dir) \n \xe2\x94\x9c- index.js\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想要做的是将除根配置文件夹之外的所有内容打包并将其放在 .tar 文件中deploydist/config该文件夹出现在输出中非常重要。开始于:

\n\n
tar -czf deploy/deploy.gz   --exclude=deploy  --exclude="./config/"  ./*\n\n$ l ./deploy/deploy0/dist \nindex.js\nrouter.js\ntest\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这会导致缺失dist/config。我能过来的唯一方法dist/config是如果我不尝试排除任何名为 config 的内容:

\n\n
tar -czf deploy/deploy.gz   --exclude=deploy   ./*\n\n$ l ./deploy/deploy1/dist \nconfig\nindex.js\nrouter.js\ntest\n
Run Code Online (Sandbox Code Playgroud)\n\n

OSX 和 CentOS(开发和构建)。tar的 man让我认为这是不可能的。

\n\n

我通过删除不需要的文件夹来解决这个问题,因为这是在构建环境中并且文件是一次性的。

\n

Fel*_*xJN 2

你可以用来find创建你的论点 - 虽然有点长:

 find -mindepth 1 ! -wholename './config' ! -wholename './config/*'
Run Code Online (Sandbox Code Playgroud)

mindepth 1排除.,以及wholename目录本身及其内容的两个排除。

tar -xzf deploy/deploy.gz --exclude="deploy" \
$( find -mindepth 1 ! -wholename './config' ! -wholename './config/*' )
Run Code Online (Sandbox Code Playgroud)