How to find hard links

Ask a Question related to Linux / Unix Administration, Design and Development.

  1. #1

    Default How to find hard links

    For symbolic links, we can issue "ls -l" command to see the file the
    link points to. Is there any command to see the file the hard link
    point to?

    I know we can issue "ls -i" command to find the inode number of the
    file with the hard links and compare with the inode of the file it
    points to. But if I don't know the file the hard link points to, how
    to know the file it points to?

    Another question: Is there a way to find the symbolic links and hard
    links that associates with a file?

    Thank you!
    olyathe Guest

  2. Similar Questions and Discussions

    1. Looking for hard to find mac software?
      I have a very extensive collection of ac Software and I have always believed in the concept of sharing, if you are in need of something just e-mail...
    2. Find Links in a html file
      Hello, Can anyone help me with the following problem I want to make an ASP page look at my index.html file, and give me a list of all the...
    3. Will File::Find::Rule follow symbolic links?
      Is there a way to tell File::Find::Rule to follow symbolic links? Neither "follow" nor "follow_fast" appear to be methods. I couldn't set...
    4. find the free hard disk space
      EXEC master..xp_fixeddrives "Abraham" <binu_ca@yahoo.com> wrote in message news:eT1ZmpYRDHA.304@tk2msftngp13.phx.gbl...
    5. ?Hard links, Soft links, & Aliases--Explain
      Hi All, Could some knowledgeable UNIX type please explain the differences between hard links, soft links, and traditional Mac aliases. Responses...
  3. #2

    Default Re: How to find hard links

    olyathe wrote:
    > For symbolic links, we can issue "ls -l" command to see the file the
    > link points to. Is there any command to see the file the hard link
    > point to?
    For all intents and purposes, you /are/ seeing the file the hard link points
    to. With hardlinks, there is no one 'original' or 'root' file; all hardlinks
    share that responsibility equally. In this view, the /inode/ is the 'file';
    the hardlinks are just alternate /routes/ to the file.
    > I know we can issue "ls -i" command to find the inode number of the
    > file with the hard links and compare with the inode of the file it
    > points to. But if I don't know the file the hard link points to, how
    > to know the file it points to?
    This is a nonsensical question; it assumes that there is some heirarchy of
    ownership between different directory entries pointing to the same inode.

    Perhaps you really mean "how do I find out what other directory entries
    point at the inode of a specific directory entry?".
    > Another question: Is there a way to find the symbolic links and hard
    > links that associates with a file?
    Recurse through the directory hierarchy, looking at files and symlinks,
    resolving each symlink into a file.

    If the file's inode matches the inode of the target file, then the test file
    points to the target file.



    > Thank you!

    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)

    Lew Pitcher Guest

  4. #3

    Default Re: How to find hard links

    olyathe wrote:
    > For symbolic links, we can issue "ls -l" command to see the file the
    > link points to. Is there any command to see the file the hard link
    > point to?
    >
    > I know we can issue "ls -i" command to find the inode number of the
    > file with the hard links and compare with the inode of the file it
    > points to. But if I don't know the file the hard link points to, how
    > to know the file it points to?
    >
    > Another question: Is there a way to find the symbolic links and hard
    > links that associates with a file?
    >
    Hard links don't "point to another file" the way symbolics links do.
    Hard links are the basic mechanism by which filenames point to the
    i-node where the file is kept. If you hard link a filename to another
    file, what you are actually doing is pointing that filename at the
    i-node. After the link is made, *both* filenames are equal--neither
    one points at the other, they both point to the same i-node. Delete
    either filename and the file will continue to exist under the other name.
    You can find all the filenames that point to a given i-node with the
    find command. Remember that i-node numbers are only unique within a
    filesystem (that's why hard links can't cross file systems), so you'll
    need to limit your find to the file system in question.

    Chris Mattern

    Chris Mattern Guest

  5. #4

    Default Re: How to find hard links

    For hard links, it is quite easy :

    - you note the inode number of the file
    - you 'cd' to the mount point of the file system
    - and you use : find . -inum <your inode number>
    (with '-xdev' if you have embedded mount points in this filesystem)

    To find hard links to regular files :

    - find . -type f -links +1

    Finding hard links to directories is much harder and I don't know how to do
    it with the shell only. But, making hard links to directory is certainly the
    worst idea a Unix admin can have and there shouldn't be any on your system.

    To find all the symbolic links that point to a specific file :

    - Note the inode number of your file. If your file is itself a symbolic
    link, use 'ls -Ldi'
    - find / -follow -inum <your inum>

    The problem is that symbolic links can cross device boundaries and relying
    on the inode number is not enough because, with the previous command, you
    can get a file whose inode number is the same as your one, but in a
    different file system. An alternate method could be :

    file=<your target file>
    inum=`ls -Ldi $file | awk '{ print $1 }'`
    for i in `find / -type l`
    do
    [ $inum = `ls -Ldi $i | awk '{ print $1 }'` ] || continue
    echo $i
    done

    This way, you check only the symbolic links and the probability to find the
    same inode in a different filesystem is lower, but it is still not perfect,
    and it is much slower. The best solution would certainly be using the
    'realpath' facility in perl or C, or finding an other way to get the
    'st_dev' field of the stat(2) system call.

    And, if you like it, there is a much harder problem : how to find all the
    symbolic links of the system who depend on a given symbolic link for their
    final resolution ? This one, I think that it cannot be solved with a shell
    script and, even with a C or perl program, it is not simple.


    FLP Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139