[comp.unix.questions] finding the missing links

rcm@flattop.UUCP (Ron McDowell) (05/30/89)

At the risk of starting another 'RTFM' war, I'd like the answer to a simple
question:  

$ echo "hello world" > $HOME/xxx ; ln xxx /tmp/yyy ; ln xxx /usr/tmp/zzz

I do a 'ls -l /tmp' and see that yyy has 3 links.  How can I find the other
two files?

Thanks in advance,
-- 
Ron McDowell @ Programming Consultants                  rcm@flattop.UUCP
4418 Monaco                  {uunet!dpmizar!petro | texbell}!flattop!rcm
San Antonio, Texas 78218-4339                            +1 512 655-3716

erikb@cs.vu.nl (Erik Baalbergen) (05/30/89)

In article <207@flattop.UUCP> rcm@flattop.UUCP (Ron McDowell) writes:
>At the risk of starting another 'RTFM' war, I'd like the answer to a simple
>question:  
>
>$ echo "hello world" > $HOME/xxx ; ln xxx /tmp/yyy ; ln xxx /usr/tmp/zzz
>
>I do a 'ls -l /tmp' and see that yyy has 3 links.  How can I find the other
>two files?

Find out /tmp/yyy's inode number, <inum>, by typing 'ls -i /tmp/yyy'.  
Then find all files with this particular <inum> as inode number:
	find / -inum <inum> -print
This command will print at least the three file names you mentioned. 
If multiple file systems are mounted, there is a chance that more file names
are printed since the inode number is unique only within a single file system.
You can apply further heuristics, like
	find / -inum <inum> -links 3 -user <your user name> -print
or even
	find / -inum <inum> -exec cmp '{}' /tmp/yyy ';' -print
See the 'find' manual page for details.

Unfortunately, it is not possible to use the device number to 'find' files.
The combination of device and inode number uniquely determines a file within
a (non-network-file-system) UNIX system.  Perhaps we should equip any future
'find' program with the "-dnum <dnum>" primary expression.
If you have a 'stat' command available, you can check the device numbers by 
hand.

Erik Baalbergen
-- 
Erik H. Baalbergen				    <erikb@cs.vu.nl>
Vrije Universiteit / Dept. of Maths. & Comp. Sc.
De Boelelaan 1081
1081 HV Amsterdam / The Netherlands		tel. +31 20 548 8080

hager@ksuvax1.cis.ksu.edu (Donald E. Hager) (05/31/89)

In article <207@flattop.UUCP> rcm@flattop.UUCP (Ron McDowell) writes:
>At the risk of starting another 'RTFM' war, I'd like the answer to a simple
>question:  
>
>$ echo "hello world" > $HOME/xxx ; ln xxx /tmp/yyy ; ln xxx /usr/tmp/zzz
>
>I do a 'ls -l /tmp' and see that yyy has 3 links.  How can I find the other
>two files?

The other two files are xxx and zzz.  Whenever you do a 'ln', it doesn't
create another copy of the file, but instead it "links" (or "points")
the file to the same inode.  If you do an "ls -i $HOME/xxx /tmp/yyy
/usr/tmp/zzz" you will see that all the files have the same inode
number.  I hope this helps.
--
Donald Hager  (hager@ksuvax1.cis.ksu.edu)	|    // //  =====   //   //
KSU Dept. of Computing & Information Sciences	|   // //  //___   //   //
BITNET: hager@KSUVAX1       			|  //=<<      //  //   //
UUCP: {rutgers,atanasoff,texbell}!ksuvax1!hager	| //  //  =====   ======

dce@Solbourne.COM (David Elliott) (05/31/89)

In article <2654@erikb.cs.vu.nl> erikb@cs.vu.nl (Erik Baalbergen) writes:
>>I do a 'ls -l /tmp' and see that yyy has 3 links.  How can I find the other
>>two files?

>	find / -inum <inum> -links 3 -user <your user name> -print

>Unfortunately, it is not possible to use the device number to 'find' files.
>The combination of device and inode number uniquely determines a file within
>a (non-network-file-system) UNIX system.  Perhaps we should equip any future
>'find' program with the "-dnum <dnum>" primary expression.

While it's a nice idea, it may not be necessary.

If you have 4.3BSD or a recent SunOS, your find command has the option
-xdev, which prevents it from looking at files in other filesystems
(hard links can't cross filesystems), so you can say

	find <root of filesystem> -xdev -inum <inum> -print

To find the root of the filesystem, just use df on the file and
pipe the output through an awk or sed script to extract the name
(watch out for df printing long names, especially NFS filesystems,
since it will produce extra lines).

-- 
David Elliott		dce@Solbourne.COM
			...!{boulder,nbires,sun}!stan!dce

guy@auspex.auspex.com (Guy Harris) (06/01/89)

>Unfortunately, it is not possible to use the device number to 'find' files.
>The combination of device and inode number uniquely determines a file within
>a (non-network-file-system) UNIX system.  Perhaps we should equip any future
>'find' program with the "-dnum <dnum>" primary expression.

Or perhaps we should equip any future "find" program with an option to
tell it not to cross file system boundaries; that's such a good idea
that Berkeley, in either 4.2BSD or 4.3BSD ("-xdev"), and AT&T, in S5R3
("-mount"), have already done it.  The advantage of such an option is
that it keeps "find" from wasting its time by marching off onto file
systems other than the one you're interested in....

mikej@lilink.UUCP (Michael R. Johnston) (06/01/89)

In article <207@flattop.UUCP> rcm@flattop.UUCP (Ron McDowell) writes:
>
>$ echo "hello world" > $HOME/xxx ; ln xxx /tmp/yyy ; ln xxx /usr/tmp/zzz
>
>I do a 'ls -l /tmp' and see that yyy has 3 links.  How can I find the other
>two files?

The simplest way I know of is to say:
       ls -i xxx        [command returns inode #]
       ncheck -i inode_number

This will return the paths to all the files that share the same inode #.
--
Michael R. Johnston
System Administrator                           rutgers!lilink!mikej
LILINK Public Access Xenix  (516) 872-2137/2138/2349 1200/2400 Login: new

guy@auspex.auspex.com (Guy Harris) (06/03/89)

 >The simplest way I know of is to say:
 >       ls -i xxx        [command returns inode #]
 >       ncheck -i inode_number

...

 >Michael R. Johnston
 >System Administrator                           rutgers!lilink!mikej

The "System Administrator" part is probably significant here.  "ncheck"
has to actually *read* the raw devices upon which the file systems
you're checking reside; generally they are *not* made publicly readable,
so J. Random User probably won't be able to use "ncheck" for this.