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 tell if two files are hard-linked from the command line? e.g. something link this:

$ ls
fileA fileB fileC

$ is-hardlinked fileA fileB
yes

$ is-hardlinked fileA fileC
no
share|improve this question
add comment

3 Answers

up vote 16 down vote accepted

On most filesystems¹, a file is uniquely determined by its inode number, so all you need to check is whether the two files have the same inode number and are on the same filesystem.

Ash, ksh, bash and zsh have a construct that does the check for you: the file equality operator -ef.

[ fileA -ef fileB ] && ! [ fileA -ef fileC ]

For more advanced cases, ls -i /path/to/file lists a file's inode number. df -P /path/to/file shows what filesystem the file is on (if two files are in the same directory, they're on the same filesystem). If your system has the stat command, it can probably show the inode and filesystem numbers (stat varies from system to system, check your documentation). If you want a quick glance of hard links inside a directory, try ls -i | sort (possibly piped to awk).

¹ All native unix filesystems, and a few others such as NTFS, but possibly not exotic cases like CramFS.

share|improve this answer
    
And definitely not on anything FAT-based, where it would be detected as a "cross-linked" file. –  Ignacio Vazquez-Abrams Nov 8 '11 at 0:16
3  
Note that fileA -ef fileB also returns 0 (success) if fileA is a symlink to fileB, or vice versa, or they both link to the same file. –  janmoesen Nov 8 '11 at 16:50
add comment
function is-hardlinked() {
    r=yes
    [ "`stat -c '%i' $1`" != "`stat -c '%i' $2`" ] && r=no
    echo $r
}
share|improve this answer
    
Note that this can be a false positive if the two files are on different filesystems but happen to have the same inode. You need to test the device number as well (stat -c %d). And if you're on Linux (given your stat command), your shell has the [ fileA -ef fileB ] to do all this directly. Also, your command gratuitously breaks with file names containing whitespace or \[?*, or begins with -: always put double quotes around command susbtitutions ("$(stat -c %i -- "$1")"). –  Gilles Jan 29 '13 at 15:41
add comment

As the first poster suggest, you can write a script based on something like this on Linux:

stat -c '%i' fileA fileB fileC
share|improve this answer
    
This isn't enough: you'll get the same number for the two files if are on different filesystems but happen to have the same inode. You need to test the device number as well (stat -c %d). And if you're on Linux (given your stat command), your shell has the [ fileA -ef fileB ] to do all this directly. –  Gilles Jan 29 '13 at 15:42
add comment

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.