finding filenames from inodes

Guy Harris guy at auspex.auspex.com
Fri May 12 18:37:28 AEST 1989


 >>Is there anyway to find the filename of a file given the inode that it is on
 >>other than to search through the filenames for it?  I am running Sun OS 4.0
 >>Thanx.
 >
 >I guess a should have mentioned that I want to do this from inside a program
 >and must be relatively fast.  ie find / -inode foo -print is NOT what I am
 >am looking for.

(Well, "find / -inode foo -print" "searches through the filenames", so
presumably nobody'd suggest that if they'd read your original article
carefully.)  Unfortunately, there really isn't a way to find all the
names (yes, plural, potentially) for a file given the file system on
which it resides and its i-number other than "searching through the
filenames".

If you know what directory it resides in, or at least what directories
it could reside in (and that set is small), it's a bit easier; you can
scan through those directories and "stat" each file in that directory
and compare "st_dev" and "st_ino".

If you know you're running on a system where the directory-reading
routines will give you the i-number of a file as part of its entry (the
UNIX systems with which I'm familiar do this; however, it's not
guaranteed by POSIX, so there could conceivably be systems that don't),
you might be able to skip the "stat" part (although you'd have to check
for stuff mounted on directories within the directories you're
searching).  For example, "ttyname" uses this trick, since it knows that
your tty is supposed to be in "/dev"; if it's not, you lose.  (If it's
not a tty, you lose, too, but "ttyname" isn't supposed to find names for
things that aren't ttys.)



More information about the Comp.unix.questions mailing list