How do I find circular symbolic links?
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.
Related videos on Youtube
Vladimir
Updated on September 18, 2022Comments
-
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 about 13 years1) You are getting the current directory because the
find
command is either just printing.
or not printing anything (so you are just runningls -lrt
orls -lrt .
) Don't know enough about HP-UXfind
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 about 13 yearsBy 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 about 13 yearsAlso, why does not including
-follow
actually give me some real links are results? -
DerfK about 13 yearsWithout
-follow
,find
examines the link itself, not the file it points to. Sofind . -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 about 13 yearsDoes this mean that if there were any loops an error message would appear?
-
Vladimir about 13 yearsOk, but how does that explain getting just an
ls
instead of getting actual links when not using-follow
? -
DerfK about 13 yearsThat's how the shell works. You asked it to run the
ls
command using the output of yourfind
command. Yourfind
command didn't print anything, so your shell executedls
with nothing, which lists the current directory. -
Don Gateley almost 11 yearsDerfK's solution also finds cycles in Windows 7 by invoking it from a cygwin shell.
-
Andy Braham about 7 yearsExcellent 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 over 5 yearsOk, how do we exit with code > 0 on the first circular detection?