我想将命令放入外壳脚本中,该脚本将创建指向目录的符号链接,但是此脚本可以反复运行,因此在后续调用中,该命令不应更改任何内容。
这是目录结构:
% tree /tmp/test_symlink
/tmp/test_symlink
├── foo
└── repo
└── resources
└── snippets
├── php.snippets
├── sh.snippets
├── snippets.snippets
├── sql.snippets
└── vim.snippets
我想在foo/
称为目录的片段中创建一个符号链接/tmp/test_symlink/repo/resources/snippets
。
所以我跑:
% ln -sfv /tmp/test_symlink/repo/resources/snippets /tmp/test_symlink/foo/snippets
'/tmp/test_symlink/foo/snippets' -> '/tmp/test_symlink/repo/resources/snippets'
这给出了预期的结果。
% tree /tmp/test_symlink
/tmp/test_symlink
├── foo
│ └── snippets -> /tmp/test_symlink/repo/resources/snippets
└── repo
└── resources
└── snippets
├── php.snippets
├── sh.snippets
├── snippets.snippets
├── sql.snippets
└── vim.snippets
5个目录,5个文件
但是,再次运行该命令时,
% ln -sfv /tmp/test_symlink/repo/resources/snippets /tmp/test_symlink/foo/snippets
'/tmp/test_symlink/foo/snippets/snippets' -> '/tmp/test_symlink/repo/resources/snippets'
它创建到目录的符号链接,该目录中存在符号链接aleady,将符号链接放入真实目录中
% tree /tmp/test_symlink
/tmp/test_symlink
├── foo
│ └── snippets -> /tmp/test_symlink/repo/resources/snippets
└── repo
└── resources
└── snippets
├── php.snippets
├── sh.snippets
├── snippets -> /tmp/test_symlink/repo/resources/snippets
├── snippets.snippets
├── sql.snippets
└── vim.snippets
为什么会发生这种情况?如何修改命令,以免后续调用不会产生这种奇怪的效果?
-n, --no-dereference treat LINK_NAME as a normal file if it is a symbolic link to a directory
。-T, --no-target-directory treat LINK_NAME as a normal file always
您认为始终将符号链接视为文件会更好吗?我以为最好限制使用这些“特殊”选项?