Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I work out the link count of an inode number? If I know the inode number is, say, 592255 - what workings out can I do to find out the link count?

I know directories have a link count of at least 2, but don't know how to work it out.

share|improve this question

4 Answers 4

up vote 3 down vote accepted

Finding the link count using the name

You can use the stat command to get a link count on a given file/directory:

$ stat lib/
  File: ‘lib/’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 11666186    Links: 3
Access: (0755/drwxr-xr-x)  Uid: ( 1000/    saml)   Gid: ( 1000/    saml)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2014-03-21 18:16:10.521963381 -0400
Modify: 2014-01-13 17:16:49.438408973 -0500
Change: 2014-01-14 17:57:46.636255446 -0500
 Birth: -

Taking a look at the man page for stat:

%h     number of hard links
%i     inode number

So you can get just this value directly using stat's --printf or --format output capabilities:

$ stat --printf="%h\n" lib/
3

$ stat --format="%h" lib/
3

$ stat -c "%h" lib/
3

Finding the link count using the inode

If on the other hand you know the inode number only you can work backwards like so:

$ ls -id lib
11666186 lib

$ find -inum 11666186 -exec stat -c "%h" {} +
3

References

share|improve this answer
    
You've made the same mistake as me, the question is to find the link count from the inode number... –  Graeme Mar 21 at 22:23
    
@Graeme - fixed. –  slm Mar 21 at 22:27

Give the inode number to the find command and tell it to run the stat command on the file:

find . -inum #### -exec stat -c "%h" {} \;
share|improve this answer
    
For a good concise explanation of inodes and directories see: teaching.idallen.com/dat2330/04f/notes/links_and_inodes.html –  Michael Martinez Mar 21 at 22:28

For example

ls -l passwd
-rw-r--r-- 1 root root 1402 2008-03-30 17:49 passwd

This number (1) is actually the link-count of the file, when referring to a file, or the number of contained directory entries, when referring to a directory (including the . and .. entries).

Also for example

ls -i /etc/passwd
8654127 /etc/passwd

This is inode number for /etc/passwd. Now you can find how many links is actualy linked with this number of inode 8654127 with:

find /etc/ -inum 8654127
/etc/passwd

In my example it is only /etc/passwd, so you can do for your case:

find / -inum 592255
share|improve this answer

To get the link count from the inode without searching the full filesystem, on an ext2/ext3/ext4 filesystem you can use debugfs. The down side here is that you require to know the device and have root privileges. The upside of course is that it can be much, much faster. For a file with inode 5638454 on dev/sda2:

sudo debugfs -R 'stat <5638454>' /dev/sda2

On btrfs, given mount point /mnt instead of the device:

stat -c %h $(sudo btrfs inspect-internal inode-resolve 5638454 /mnt)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.