How do I find circular symbolic links?

17,479

GNU find's manpage says that all POSIX finds are supposed to detect filesystem loops and emit error messages in these cases, and I have tested

find . -follow -printf ""

on GNU find, which was able to find loops of the form ./a -> ./b and ./b -> ./a printing the error

find: `./a': Too many levels of symbolic links
find: `./b': Too many levels of symbolic links

(this also worked on a->b->c->a)

Likewise, loops of the form ./foo/x -> .. and ./foo/a -> ./bar + ./bar/b -> ./foo printed the errors

find: File system loop detected; `./foo/a/b' is part of the same file system loop as `./foo'.
find: File system loop detected; `./bar/b/a' is part of the same file system loop as `./bar'.
find: File system loop detected; `./foo/x' is part of the same file system loop as `.'.

If you wanted to do something with the output other than read it, you would need to redirect it from stderr to stdout and pipe it to some script that can parse out the error messages.

Share:
17,479

Related videos on Youtube

Vladimir
Author by

Vladimir

Updated on September 18, 2022

Comments

  • Vladimir
    Vladimir over 1 year

    I'm working on a HP-UX system and I want to find if there are any circular symbolic links.

    So far I'm using the command:

    ls -lrt  `find ./ -follow -type l`
    

    But it's only doing ls -lrt on current directory as result.

    What command should I use to find all circular symbolic links in a system?

    • DerfK
      DerfK about 13 years
      1) You are getting the current directory because the find command is either just printing . or not printing anything (so you are just running ls -lrt or ls -lrt .) Don't know enough about HP-UX find to tell you how to fix this (maybe it requires an explicit -print?). 2) What do you mean "circular"? ./a -> ./b and ./b -> ./a? What about /home/foo/a -> /home? Or /home/foo/a -> /home/bar and /home/bar/b -> /home/foo?
    • Vladimir
      Vladimir about 13 years
      By circular I meant any kind of link which can create a loop so all of the above. I'm trying with -print right now.
    • Vladimir
      Vladimir about 13 years
      Also, why does not including -follow actually give me some real links are results?
    • DerfK
      DerfK about 13 years
      Without -follow, find examines the link itself, not the file it points to. So find . -type l prints things that are links (because they are -type l) without even looking to see what they point at (which would be files or directories or other links that pointed to files or directories).
  • Vladimir
    Vladimir about 13 years
    Does this mean that if there were any loops an error message would appear?
  • Vladimir
    Vladimir about 13 years
    Ok, but how does that explain getting just an ls instead of getting actual links when not using -follow?
  • DerfK
    DerfK about 13 years
    That's how the shell works. You asked it to run the ls command using the output of your find command. Your find command didn't print anything, so your shell executed ls with nothing, which lists the current directory.
  • Don Gateley
    Don Gateley almost 11 years
    DerfK's solution also finds cycles in Windows 7 by invoking it from a cygwin shell.
  • Andy Braham
    Andy Braham about 7 years
    Excellent Tip! However, when I ran that sequence it did find a loop but only displayed it as the relative path. Is there a way to force it to give absolute paths?
  • Alexander Mills
    Alexander Mills over 5 years
    Ok, how do we exit with code > 0 on the first circular detection?