itsource

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

mycopycode 2023. 5. 12. 22:16
반응형

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

웹 사이트의 디렉터리 트리에서 모든 심볼 링크를 찾고 있습니다.나는 내가 사용할 수 있다는 것을 알고 있습니다.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옵션; 사실은.findman page는 말합니다.

   -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 {} \;

더 많은 예제 찾기

find예제는 여기에서 찾을 수 있습니다: https://hamwaves.com/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 

어떤 파일을 대상으로 하는지도 확인하려면 추가만 하면 됩니다.

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...

Linux 기반 OS에서 모든 손상된 심볼릭 링크를 재귀적으로 찾으려면 아래 하나의 라이너 bash 스크립트 명령을 찾으십시오.

a=$(find / -type l); for i in $(echo $a); do file $i ; done |grep -i broken 2> /dev/null

symlinks 패키지를 설치하고 유틸리티를 사용할 수 있습니다.

symlinks -rv "/path"

    -c == change absolute/messy links to relative
    -d == delete dangling links
    -o == warn about links across file systems
    -r == recurse into subdirs
    -s == shorten lengthy links (displayed in output only when -c not specified)
    -t == show what would be done by -c
    -v == verbose (show all symlinks)

매달린 링크는 끊어진 링크입니다.

제가 하는 일은 제 bin 디렉토리에 별칭과 같은 스크립트를 만드는 것입니다.예를 들어 lsdls -l | grep ^d라는 스크립트가 있습니다.

당신은 lsls -lR | grep ^l을 만들 수 있습니다.

그들 +x를 chmod하면 바로 갈 수 있습니다.

언급URL : https://stackoverflow.com/questions/8513133/how-do-i-find-all-of-the-symlinks-in-a-directory-tree

반응형