4

How can you tell how many hardlinks a directory has in OSX from terminal?

In OSX you can make hardlinks to directories. This is how Time Machine works. I would like to know which directories have hardlinks to them so I can see which directories are new to Time Machine.

I have tried ls -l and stat -f "%l %N" * but neither seem to give correct answers.

Anyone know how to do this?

jasongregori
  • 11,451
  • 5
  • 43
  • 41

2 Answers2

2

ls -l is the correct way to display hardlink files in a directory.

From Wikipedia’s article on hard links:

Most modern operating systems don't allow hard links on directories to prevent endless recursion. In addition, hard links on directories would lead to inconsistency on parent directory entries. A notable exception to this is Mac OS X v10.5 (Leopard) which uses hard links on directories for the Time Machine backup mechanism only. Symbolic links and NTFS junction points are generally used instead for this purpose.

Dumb Code
  • 438
  • 3
  • 12
  • 3
    When you copy contents from another source, please make it clear that you are not the author of the text, give credit where credit is due, and provide a link to the original source of information. Stack Overflow [doesn’t support plagiarism.](http://meta.stackexchange.com/questions/54558/would-it-be-wrong-to-copy-paste-questions-from-quora/54559#54559) –  Jun 13 '11 at 06:40
  • Thanks Bavarious for notifying, i will take care of that. – Dumb Code Jun 13 '11 at 06:42
  • `ls -l` works fine for hardlink **files** in a directory but it seems me bogus results for hardlink **directories**. For example if I create a folder `ls -l` shows that it has 2 links to it. Is that correct? Am I missing something here? – jasongregori Jun 15 '11 at 04:30
  • 1
    actually, I'm pretty sure that number is the number of files in the directory. – jasongregori Jun 15 '11 at 04:39
1

I don't think even Time Machine can hard link across file systems, by definitions. I believe making a hard link just creates a file (or directory) with the same inode number as the original, and inodes are only unique within a given file system.

I don't think there's any way to count how many symbolic links there are to a file or directory, since there can be symbolic links to files that are on unmounted volumes.

Update: When you create a directory, there are automatically two hard links to it. One from the directory itself (".") and one from its parent ("..") Doing an ls -ld on a directory will give you the number of hard links to it.

Eric
  • 843
  • 6
  • 15
  • I'm not talking about hard links across file systems. It's within one file system. – jasongregori Jun 15 '11 at 04:32
  • You mentioned Time Machine, which requires the backup to be on a separate filesystem, and so can't use hard links. So I thought maybe you meant symbolic links. – Eric Jun 15 '11 at 15:40
  • Sorry for the late reply. I'm not sure what you mean. Time Machine does use hard links but they all point to the other folders in the same filesystem. So you have folders on your Time Machine drive pointing to other folders on that drive. – jasongregori Jul 12 '11 at 22:35
  • A directory link count represents the number of "contained" items including invisible items and `.` and `..`. The command `ls -la` will show the contained items including _invisible items_ and `.` and `..`. – marc-medley Dec 27 '17 at 06:20