≡ Menu

How to find all hard links in a directory on Linux

I have a file named /etc/passwd i and I would like to find all hard links to this file in a directory. How do I find out all other hard links to the /etc/passwd in /backups/ directory? How can find all hard links to a given file using Linux or Unix command line option?

You can use the find command on Linux, Apple MacOS, FreeBSD and other operating systems to find out all hard links.

How to find all hard links to a given file

A hard link is nothing but the specific location of physical data. You give various names that refer to the same file. File refers to the same inode as name. Hard links to foo file can be created as follows:
$ ln foo bar
$ ln foo dir2/foobar

In above example, bar and foobar become another name of foo file. The syntax is as follows to find all hard links for foo file in current directory:
find /dir/to/search/ -samefile /path/to/file/name
## To find out all hard links to foo, use this command:
find / -samefile foo
find / -xdev -samefile foo


If the file named /etc/passswd and you need to get all hard links to it that exists under /backups/ directory, run:
$ find /backups/ -samefile /etc/passwd
Sample outputs:


If you do not want to descend directories on other filesystems such as mounted once, try:
$ find /backups/ -xdev -samefile /etc/passwd

Find and delete all hard links to a file named foo in /tmp/ directory

Type the following command:
$ find /tmp/ -xdev -samefile foo -print0 | xargs -I {} -0 rm -v {}
Sample outputs:

removed ‘/tmp/dir2/foobar’
removed ‘/tmp/foo’
removed ‘/tmp/bar’

Help, -samefile option is not supported by my find command

Find find the file’s inode number, enter:
$ ls -li fileNameHere
$ ls -li /tmp/demo.txt

Sample outputs:

4065089 -rw-r--r-- 3 vivek vivek 8 Feb 26 02:19 /tmp/demo.txt

The first column in the above output displayes the file’s inode # 4065089. Now the alternate syntax is as follows to find all hard links for a file named /tmp/demo.txt using inode # 4065089:
$ find /tmp/ -inum 4065089
$ find /tmp/ -xdev -inum 4065089

Sample outputs:


To find and delete all all hard links to a file by an inode number # 4065089, run:
$ find /tmp/ -xdev -inum 4065089 | xargs rm -v
$ find /tmp/ -xdev -inum 4065089 -print0 | xargs -I {} -0 rm -v {}

Here is my sample session:

Fig.01: How to find all hard links to a given file name

Fig.01: How to find all hard links to a given file name

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:

{ 1 comment… add one }
  • Mojo Jojo February 26, 2017, 2:08 pm

    There may be filesystem-specific means to get that info, as well.

    This could be important if you’re dealing with filesystem-specific constructs that are like hardlinks but not (e.g. reflinks in btrfs) or there’s filesystem-specific tools that might chase down things in a faster/better way for your needs (e.g. xfs_db -c inodes on an unmounted fs).

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , ,