Search code examples
shellunixhyperlinkpwd

How to find all symbolic link in PWD which refer to files outside of PWD? unix


i can find all link in dir by command:

find . -type l

But i need link which only refer to files outside of PWD. Can anybody say how to do it? Thanks.


Solution

  • I would do something like:

    find . -type l -exec readlink -f '{}' \; | grep -v "^`readlink -f ${PWD}`"
    

    readlink -f gives you the canonical path of a file, so the first command gives you the path of links and the grep command excludes files beginning with the current path.

    If you want to remember which links pointed to those paths, here's a way to do it:

    find . -type l -exec sh -c 'echo $(readlink -f "{}") "<-- {}"' \; \
      | grep -v "^$(readlink -f ${PWD})"
    

    the -exec switch is more complicated since you have to display both the linked path and the path of the symlink.