递归创建符号链接

eat*_*ght 7 shell bash symlink

我基本上想运行这个命令......

ln -s /opt/gitlab/embedded/service/gitlab-shell/hooks/ /var/opt/gitlab/git-data/repositories/web/*/hooks
Run Code Online (Sandbox Code Playgroud)

这将在名为 hooks 的 web 文件夹下的所有文件夹中创建一个符号链接,但它不会返回任何错误,但它实际上并未添加符号链接。

Bra*_*ley 6

您可能希望find通过该maxdepth选项使用该命令。我创建了这个示例目录结构:

/tmp/parent
/tmp/parent/subdir2
/tmp/parent/subdir1
/tmp/parent/subdir4
/tmp/parent/subdir4/notme
/tmp/parent/subdir3
Run Code Online (Sandbox Code Playgroud)

假设我想/tmp/hooks在每个子目录而不是notme子目录中创建一个符号链接:

root@xxxxxxvlp12 ~ $ find /tmp/parent -type d -maxdepth 1 -exec ln -s /tmp/hooks {} \;
root@xxxxxxvlp12 ~ $ find /tmp/parent -ls
2490378    4 drwxr-xr-x   6 root     root         4096 Oct  7 12:39 /tmp/parent
2490382    4 drwxr-xr-x   2 root     root         4096 Oct  7 12:39 /tmp/parent/subdir2
2490394    0 lrwxrwxrwx   1 root     root           10 Oct  7 12:39 /tmp/parent/subdir2/hooks -> /tmp/hooks
2490379    4 drwxr-xr-x   2 root     root         4096 Oct  7 12:39 /tmp/parent/subdir1
2490395    0 lrwxrwxrwx   1 root     root           10 Oct  7 12:39 /tmp/parent/subdir1/hooks -> /tmp/hooks
2490389    4 drwxr-xr-x   3 root     root         4096 Oct  7 12:39 /tmp/parent/subdir4
2490390    4 drwxr-xr-x   2 root     root         4096 Oct  7 12:38 /tmp/parent/subdir4/notme
2490396    0 lrwxrwxrwx   1 root     root           10 Oct  7 12:39 /tmp/parent/subdir4/hooks -> /tmp/hooks
2490387    4 drwxr-xr-x   2 root     root         4096 Oct  7 12:39 /tmp/parent/subdir3
2490397    0 lrwxrwxrwx   1 root     root           10 Oct  7 12:39 /tmp/parent/subdir3/hooks -> /tmp/hooks
2490391    0 lrwxrwxrwx   1 root     root           10 Oct  7 12:39 /tmp/parent/hooks -> /tmp/hooks
Run Code Online (Sandbox Code Playgroud)