Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: locate path to a file from inode number

by Daruma (Curate)
on Nov 08, 2002 at 17:00 UTC ( #211473=note: print w/replies, xml ) Need Help??

in reply to locate path to a file from inode number


I have run accross this issue in the past and solved it by writing Bourne, Korn and BASH shell scripts to find all paths to a single inode.

In Unix/Linux operating systems, a hard link is basically another path pointing to the same inode. Unfortunately, there's no quick and easy solution for finding all the places (paths) that point to that same inode. Here's the basics of how I've done this in the past.

  1. Use ls -i filename to find the inode of the file in question.
  2. Use find / -inum XXXXXX -print to find the full path for each file pointing to inode XXXXXX.

Note that this is searching from the root, and can take quite a while. Not necessarily the most desirable of solutions, but it's worked in the past.

One of our applications makes very heavy use of hard links. Often our clients do not understand hard linking and might move things around or inadvertently break links. I've taken this very shell script idea and used it to help me reconcile the links and report on those that are misplaced, broken or completely missing.

Are you looking for a perl solution to your problem? If so, I've not yet developed something to trace down inodes. I would certainly like to give it a try, however. If you are looking for a basic shell utility, it's not hard to write one up.

I note that you mention the utility you saw previously did not use ls -i filename. However, many utilities on Unix systems are collections of the basic shell functions of the system, like ls and find. You might find that down in the workings of this utility, it is making use of good old Unix standard utilites.

Alternatively, it could be that this utility, if written in something like C, made use of the underlying C functions behind ls and find.

With a perl solution, it might be hard to get away from the heavy IO you are experiencing in File::Find. I do think it is worth a shot.


Replies are listed 'Best First'.
Re: Re: locate path to a file from inode number
by gnu@perl (Pilgrim) on Nov 08, 2002 at 18:27 UTC
    Yeah, i kind of wanted to keep it to perl if I could. Shortly after posting here I did find the utility, as was pointed out in this thread (ncheck).

    I was really trying to stay away from C if possible, I haven't done C in about 3 years and I'm a little rusty. Also, C would need to be changed from one *nix to another. I would like something more portable. If you can think anything else I am open.

    Also, if anyone does know of a C method of doing something like this, please don't hold back. Like I said, I haven't done C in a little while, but I wouldn't be opposed to it.

Re: Re: locate path to a file from inode number
by petral (Curate) on Nov 08, 2002 at 22:35 UTC
    " find / -inum XXXXXX -print " Use    find [....] -xdev  to stay on the file system the inode number came from.    Not only is this more efficient, but if you find a file with the same inode on a different file system, it will be a different file!

Re^2: locate path to a file from inode number
by Anonymous Monk on Aug 20, 2009 at 22:46 UTC
    You could install the samesame package and rebuild the links automatically, by typing: find /path/ | samefile -i | samelink

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://211473]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2022-08-11 09:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found