How do you find the symbolic links to files.

Gene Spafford spaf at cs.purdue.EDU
Tue Nov 27 17:09:20 AEST 1990


Okay, the question is, how do you find all links to a file?

Hard links are easy.  Barry & I have shown two ways -- using find and
ncheck -- to do it.  Hard links, by their nature, must always reside
in the same disk partition as the file system entry they describe
(doesn't have to be a file -- links can be to directories, FIFOs,
devices, etc...anything describeable by an i-node).

Anyhow, the question was then posed, as posed before, how to find all
the symbolic links.  The simple answer given earlier is "You can't."
I'll give the same answer, but also try to explain it.

A symbolic link contains a pathname to the object it describes.
Therefore, the symbolic link can exist on another disk partition and
still be valid.  That means that there could be a link to your file
but it is not "active" right now.  For instance, if the partition on
which the link exists is not currently mounted, the link isn't there,
but as soon as the partition is mounted it is a link to your file.

Or consider this.  If I mount a partition on /mnt, and create a
symbolic link to the ../etc/passwd file, that works until I unmount
and remount the file system on /usr/tmp in which case the link no
longer works.

Other examples come to mind, but the same thing happens.  There could
be lots of links to your file, but they currently aren't available.

Now let's refine the problem some and ask how to find all currently
available symbolic links (mounted in proper places, etc) that point to
a specific file system entry.  That's not simple, but it is possible--
however, I don't know of a way to do this with standard commands.  You
need to compare major dev, minor dev, i-node triples to make an exact
determination, and I can't think of a standard utility that reports
the device numbers.  You can't just compare contents, because it is
possible that a copy of a file could be made on another parition and
end up with the same i-node number.  Unlikely, but possible.

The way to do it is to write a program that stats your object, and
gets the device numbers and i-node number.  Then scan the file system
(using find, for instance) looking for symbolic links.  For each one
found, fetch the device numbers and compare against your saved trio.
Voila.  Not simple, but it will work.
-- 
Gene Spafford
NSF/Purdue/U of Florida  Software Engineering Research Center,
Dept. of Computer Sciences, Purdue University, W. Lafayette IN 47907-2004
Internet:  spaf at cs.purdue.edu	uucp:	...!{decwrl,gatech,ucbvax}!purdue!spaf



More information about the Comp.unix.internals mailing list