Question

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.

Was it helpful?

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.

OTHER TIPS

Ich would do something like:

for link in `find . -type l`; do X=$(dirname `readlink $link`); if [ $X != '.' ]; then echo $link; fi ; done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top