Symbolischen Link rekursiv erstellen

Symbolischen Link rekursiv erstellen

Ich möchte im Wesentlichen diesen Befehl ausführen ...

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

Dadurch wird in allen Ordnern unter dem Webordner mit dem Namen „Hooks“ ein symbolischer Link erstellt. Es werden jedoch keine Fehler zurückgegeben, und der symbolische Link wird auch nicht hinzugefügt.

Antwort1

Sie möchten den findBefehl wahrscheinlich mit der maxdepthOption verwenden. Ich habe diese Beispielverzeichnisstruktur erstellt:

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

/tmp/hooksNehmen wir an, ich möchte in jedem Unterverzeichnis einen symbolischen Link erstellen, aber nicht im notmeUnterverzeichnis:

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

Antwort2

findkann verwendet werden, um einen Befehl im Kontext jedes Verzeichnisses unter einem bestimmten Pfad auszuführen.

Der folgende Befehl sucht nach allen Dateien in /var/opt/gitlab/git-data/repositories/web/Verzeichnissen ( -type d) und erstellt einen symbolischen Link relativ zum aktuellen Verzeichnis, das er untersucht (dargestellt durch {}in -exec).

Der folgende findBefehl erledigt also das Gewünschte:

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

Antwort3

lnfunktioniert ähnlich wie cp. Wenn mehr als zwei Argumente vorhanden sind, wird das letzte als Verzeichnis behandelt.

AusMann ln:

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

Sie müssen stattdessen eine For-Schleife verwenden.

Antwort4

Nachdem ich mich umgesehen und mit dem Find-Befehl herumgespielt hatte, fand ich, dass es einfacher war, einfach eine Schleife mit zu machen ./*/. Danke für all die Hilfe! Ich habe auf meinem GitHub-Konto ein Skript erstellt, das viel feiner abgestimmt ist. Obwohl es gitlab-spezifisch ist, würde es nur ein paar Minuten dauern, es für alles zu modifizieren, was Sie brauchen.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

verwandte Informationen