Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 823
jasongregori Avatar asked Oct 28 '25 09:10

jasongregori


2 Answers

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.

like image 121
Dumb Code Avatar answered Oct 30 '25 06:10

Dumb Code


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.

like image 37
Eric Avatar answered Oct 30 '25 07:10

Eric