7

I have been able to find a command to get a list of the broken links only, but not the "working" symbolic links

find -H mydir -type l

How can I do the opposite?

If I have these files in mydir:

foo
bar
bob
carol

If they are all symbolic links, but bob points to a file that doesn't exist, I want to see

foo
bar
carol
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
brendangibson
  • 173
  • 1
  • 4
  • 9

3 Answers3

8

With find(1) use

$ find . -type l -not -xtype l

This command finds links that stop being links after following - that is, unbroken links. (Note that they may point to ordinary or special files, directories or other. Use -xtype f instead of -not -xtype l to find only links pointing to ordinary files.)

$ find . -type l -not -xtype l -ls

reports where they point to.

If you frequently encounter similar questions in your interactive shell usage, zsh is your friend:

% echo **/*(@^-@)

which follows the same idea: Glob qualifier @ restricts to links, and ^-@ means "not (^) a link (@) when following is enabled (-).

(See also this post about finding broken links in python, actually this demonstrates what you can do in all languages under Linux. See this blog post for a complete answer to the related question of "how to find broken links".)

Community
  • 1
  • 1
tiwo
  • 3,238
  • 1
  • 20
  • 33
2

This will show (LINUX) only symbolic links that are links to regular files that actually exist:

find DIR1 -type l -xtype f -ls

From the "find" (LINUX) documentation on the xtype option: "for symbolic links, -xtype checks the type of the file that -type does not check"

This option will show symbolic links to directories that actually exist:

find DIR1  -type l -xtype d -ls
A B
  • 4,068
  • 1
  • 20
  • 23
1

one bash solution:

find . -type l -print | while read src
do
    [[ -e "$src" ]] && echo $src
done

so, find all symlinks and check for the existence, if exists -> echo it

clt60
  • 62,119
  • 17
  • 107
  • 194