[bash] 디렉토리 트리에서 모든 심볼릭 링크를 찾으려면 어떻게합니까?

내 웹 사이트의 디렉토리 트리에서 모든 심볼릭 링크를 찾으려고합니다. 나는 find이것을 하기 위해 사용할 수 있다는 것을 알고 있지만 디렉토리를 재귀 적으로 확인하는 방법을 알 수는 없다.

이 명령을 시도했습니다.

find /var/www/ -type l

… 그리고 나중에 내용 /var/www이 심볼릭 링크라는 것을 알았으므로 명령을 다음과 같이 변경했습니다.

find -L /var/www/ -type l

실행하는 데 시간이 걸리지 만 일치하는 항목이 없습니다.

하위 디렉토리를 확인하려면 어떻게해야합니까?



답변

이것은 재귀 적으로 /path/to/folder디렉토리를 순회 하고 심볼릭 링크 만 나열합니다.

ls -lR /path/to/folder | grep ^l

심볼릭 링크를 따르려는 의도라면 find명령을 사용해야하지만 -L옵션을 포함해야합니다 . 실제로 find매뉴얼 페이지는 다음과 같이 말합니다.

   -L     Follow symbolic links.  When find examines or prints information
          about files, the information used shall be taken from the  prop‐
          erties  of  the file to which the link points, not from the link
          itself (unless it is a broken symbolic link or find is unable to
          examine  the file to which the link points).  Use of this option
          implies -noleaf.  If you later use the -P option,  -noleaf  will
          still  be  in  effect.   If -L is in effect and find discovers a
          symbolic link to a subdirectory during its search, the subdirec‐
          tory pointed to by the symbolic link will be searched.

          When the -L option is in effect, the -type predicate will always
          match against the type of the file that a symbolic  link  points
          to rather than the link itself (unless the symbolic link is bro‐
          ken).  Using -L causes the -lname and -ilname predicates  always
          to return false.

그런 다음 시도하십시오.

find -L /var/www/ -type l

이것은 아마도 효과가있을 것입니다 : find맨 다이아몬드 페이지 에서이 다이아몬드를 찾았습니다 . -type옵션을 사용하는 경우 옵션으로 변경해야합니다 -xtype.

          l      symbolic link; this is never true if the -L option or the
                 -follow option is in effect, unless the symbolic link  is
                 broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

그때:

find -L /var/www/ -xtype l


답변

하나의 명령, 파이프 없음

find . -type l -ls

설명 : find 현재 디렉토리 .부터 모든 -type l잉크 참조를 참조하십시오 -ls. 평범하고 간단한 …

이 답변을 확장하면 다음과 같은 심볼릭 링크 관련 find명령 이 몇 가지 더 있습니다 .

특정 대상에 대한 심볼릭 링크 찾기

find . -lname link_target

참고 link_target와일드 카드 문자를 포함 할 수있는 패턴이다.

깨진 심볼릭 링크 찾기

find -L . -type l -ls

-L옵션은 find깨진 경우가 아니면 심볼릭 링크를 따르도록 지시 합니다.

깨진 심볼릭 링크 찾기 및 바꾸기

find -L . -type l -delete -exec ln -s new_target {} \;

더 많은 예제 찾기

https://hamwaves.com/find/find 에서 더 많은 예제를 찾을 수 있습니다.


답변

find 기본적으로 이미 재귀 적으로 보입니다.

[15:21:53 ~]$ mkdir foo
[15:22:28 ~]$ cd foo
[15:22:31 ~/foo]$ mkdir bar
[15:22:35 ~/foo]$ cd bar
[15:22:36 ~/foo/bar]$ ln -s ../foo abc
[15:22:40 ~/foo/bar]$ cd ..
[15:22:47 ~/foo]$ ln -s foo abc
[15:22:52 ~/foo]$ find ./ -type l
.//abc
.//bar/abc
[15:22:57 ~/foo]$ 


답변

심볼릭 링크 자체 만 보려면

find -L /path/to/dir/ -xtype l 

대상 파일을 확인하려면 ls를 추가하십시오.

find -L /path/to/dir/ -xtype l -exec ls -al {} \;


답변

이것은 내가 지금까지 찾은 가장 좋은 것입니다-현재 디렉토리의 심볼릭 링크를 재귀 적으로 표시하지만 따르지 않고 전체 경로 및 기타 정보와 함께 표시합니다.

find ./ -type l -print0 | xargs -0 ls -plah

출력은 다음과 같습니다.

lrwxrwxrwx 1 apache develop 99 Dec  5 12:49 ./dir/dir2/symlink1 -> /dir3/symlinkTarget
lrwxrwxrwx 1 apache develop 81 Jan 10 14:02 ./dir1/dir2/dir4/symlink2 -> /dir5/whatever/symlink2Target
etc...


답변

내가하는 일은 내 별칭 디렉토리와 같은 스크립트를 bin 디렉토리에 작성하는 것입니다. 예를 들어, lsd ls -l | grep ^ d

하나의 lsl ls -lR | grep ^ l

그것들을 + x로 chmod하면 잘 갈 수 있습니다.


답변