How do you find the symbolic links to files.

Barry Shein bzs at world.std.com
Wed Nov 28 04:07:56 AEST 1990


From: mchinni at pica.army.mil (Michael J. Chinni, SMCAR-CCS-E)
>If so, how would you find all symbolic links to the file ?

In general, it's very difficult. Consider that valid symlinks can
point across NFS mounts. 

I have just created the following:

	% ln -s /pica.army.mil/root/etc/termcap ./termcap

That's a perfectly valid symlink from my machine to your termcap file.
I can't resolve it right now because I can't NFS mount your root
directory, but it's still a symlink to your termcap file, no? How
would you find it?

In fact, to be more realistic, NFS mounts are not even commutative
(some workstation at your site can mount and point thru a file system
on your system, but you can't necessarily see that valid symlink.)

But, if we narrow the question to "how can I find all the symlinks I
can find?" the find command can locate every symlink (-type l) and
they can be resolved and their i-node numbers tested for equality with
a target with an only slightly convoluted shell command (or trivially
by writing a 20 line C-program which can be -exec'd by find with the
path in question and the inode number desired.)

	% find /mount-point -type l -a -exec testinode '{}' #inum ';'

where testinode is just something like:

	#include <stdio.h>
	#include <sys/types.h>
	#include <sys/stat.h>

	main(argc,argv) int argc; char **argv;
	{
		struct stat st;

		if(!stat(argv[1],&st) && (st.st_ino == atoi(argv[2])))
			printf("%s\n",argv[1]);
		exit(0);
	}

(you could add argv checks)
-- 
        -Barry Shein

Software Tool & Die    | {xylogics,uunet}!world!bzs | bzs at world.std.com
Purveyors to the Trade | Voice: 617-739-0202        | Login: 617-739-WRLD



More information about the Comp.unix.internals mailing list