遞歸創建符號鏈接

遞歸創建符號鏈接

我本質上想運行這個命令......

ln -s /opt/gitlab/embedded/service/gitlab-shell/hooks/ /var/opt/gitlab/git-data/repositories/web/*/hooks

這將在 Web 資料夾下的所有資料夾中建立一個名為 hooks 的符號鏈接,但它不會返回任何錯誤,但實際上不會添加符號鏈接。

答案1

您可能希望find透過該maxdepth選項來使用該命令。我建立了這個範例目錄結構:

/tmp/parent
/tmp/parent/subdir2
/tmp/parent/subdir1
/tmp/parent/subdir4
/tmp/parent/subdir4/notme
/tmp/parent/subdir3

假設我想/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

答案2

find可用於在特定路徑下的每個目錄的上下文中執行命令。

以下命令查找/var/opt/gitlab/git-data/repositories/web/目錄 ( -type d) 下的所有文件,並建立相對於它正在檢查的當前目錄的符號連結(由{}in表示-exec

因此以下find命令將滿足您的要求:

find /var/opt/gitlab/git-data/repositories/web/ -type d -exec ln /opt/gitlab/embedded/service/gitlab-shell/hooks/ {}/hooks \;

答案3

ln工作原理有點像cp.如果有兩個以上的參數,則最後一個被視為目錄。

曼林:

ln [option]... target... directory

您需要使用 for 迴圈。

答案4

在環顧四周並使用 find 命令後,我發現使用./*/.感謝您的幫忙!我在我的 github 帳戶上製作了一個更精細的腳本。雖然它是 gitlab 特定的,但只需幾分鐘即可根據您的需求修改它https://github.com/michaeljs1990/bash-scripts/blob/master/gitlab-hooks-migration.sh

#!/bin/bash

find . -name "hooks" -type l -delete

hooks="hooks"
for i in ./*/; do
    ln -s  /opt/gitlab/embedded/service/gitlab-shell/hooks/ $i$hooks
done

相關內容