tar 命令 - 跳过符号链接

16 tar symlink wildcards files

我使用 tar 命令,

tar -cvf protTests.tar protTests/*
Run Code Online (Sandbox Code Playgroud)

tar该文件夹内的所有文件,protTests。但这包括文件夹内的符号链接,这不是我们想要的。

是否有命令行选项,可以忽略所有符号链接?

Cel*_*ada 12

您可以这样做,以提供除符号链接之外tar的所有文件的列表protTests

find protTests -maxdepth 1 -mindepth 1 -not -type l -print0 |
  tar --null --files-from - -cvf protTests.tar
Run Code Online (Sandbox Code Playgroud)

顺便说一下,您现有的命令:

tar -cvf protTests.tar protTests/*
Run Code Online (Sandbox Code Playgroud)

不会存档 中的所有文件protTests,只会存档名称不以 开头的文件.(未隐藏的文件)。该*水珠运营商跳过的文件,其名称开头.的设计。该命令还存在一个问题,即如果protTests有大量文件(超过数千个),则protTests/*可以扩展到太多参数以适应命令行。

像这样的更简单的命令不会有这些问题:

tar -cvf protTests.tar protTests
Run Code Online (Sandbox Code Playgroud)

  • @schily,GNU `tar` 和 `bsdtar` 都有 `--files-from` 和 `--null`,它们消除了有趣字符的问题(如果与 `find` 的 `-print0` 或 `-exec printf '%s\0' {} +`)。但是在这里,您可能想要添加`--no-recursion` 选项。一些 `pax` 实现也有一个 `-0` 选项。 (2认同)
  • @schily 我觉得这个评论线程不是倡导不同版本的 `tar` 的地方。请把它带到[聊天](http://chat.stackexchange.com/rooms/26/unix-and-linux)。它已经太长了,读起来就像一场宗教战争。除非 OP 在这一点上插话,否则我认为使用什么版本的 `tar` 并不重要。我们甚至不知道他们使用的是哪种 Unix。 (2认同)