Find all absolute links in a directory tree

To find absolute links, you can use find's -lname option if your find supports that (it's available at least in GNU find, on FreeBSD and macOS):

find . -type l -lname '/*'

This asks find to print the names of files which are symbolic links and whose content (target) matches /* using shell globbing.

Strictly speaking, POSIX specifies that absolute pathnames start with one / or three or more /; to match that, you can use

find . -lname '/*' ! -lname '//*' -o -lname '///*'

On what systems is //foo/bar different from /foo/bar? has more details on that.

(Thanks to Sato Katsura for pointing out that -lname is GNU-specific, to fd0 for mentioning that it's actually also available on at least FreeBSD and macOS, and to Stéphane Chazelas for bringing up the POSIX absolute pathname definition.)


You may find the symlinks utility useful:

$ symlinks -r .
other_fs: /home/chazelas/test/bin -> /bin
dangling: /home/chazelas/test/DIR/foo -> foo
dangling: /home/chazelas/test/blah -> not-here
absolute: /home/chazelas/test/chazelas -> /home/chazelas

And can fix the links for you. Here with -t to tell what it would do:

$ symlinks -rct .
other_fs: /home/chazelas/test/bin -> /bin
dangling: /home/chazelas/test/DIR/foo -> foo
dangling: /home/chazelas/test/blah -> not-here
absolute: /home/chazelas/test/chazelas -> /home/chazelas
changed:  /home/chazelas/test/chazelas -> ../../chazelas

Tags:

Symlink