How to find all hard links to a file on your Linux system ?

We all as a system admin should know how to find all hard links to a file on our system.

So let us have a look, how to find this useful information.

There are two ways to do this task.

1. Using -samefile option of find command.
Here we will search the filesystem with -samefile option.

[root@nglinux ~]# ln /etc/passwd ./passwd_backup
 
[root@nglinux ~]# find / -samefile /etc/passwd | more
/root/passwd_backup
find: `/proc/23052/task/23052/fd/5': No such file or directory
find: `/proc/23052/task/23052/fdinfo/5': No such file or directory
find: `/proc/23052/fd/5': No such file or directory
find: `/proc/23052/fdinfo/5': No such file or directory
/etc/passwd
[root@nglinux ~]# 

2. Using inum option if samefile option is not available with find command.
Here we will search the filesystem using inode number.

[root@nglinux ~]# ls -il /etc/passwd
663401 -rw-r--r--. 2 root root 1920 Jan  6 01:54 /etc/passwd
[root@nglinux ~]# find / -inum 663401
/root/passwd_backup
find: `/proc/23188/task/23188/fd/5': No such file or directory
find: `/proc/23188/task/23188/fdinfo/5': No such file or directory
find: `/proc/23188/fd/5': No such file or directory
find: `/proc/23188/fdinfo/5': No such file or directory
/etc/passwd
[root@nglinux ~]# 

I hope you liked the article.

Please do comment and add something to this article.

6 Replies to “How to find all hard links to a file on your Linux system ?”

    • Aakanksha

      Hi, hard links can only persist accross a single hard disk or lun, we can’t create hard links across multiple hard disks….

    • Saket

      no there is no direct way available to find the symlinks, however you can pass ls -al command to find and then grep the original filename to get the symlinks…

    • Aakanksha

      Dear Pratipal, great suggestion.

      For this, we can redirect the error to null like below.

      [root@nglinux data]# find / -samefile /etc/passwd 2> /dev/null | more
      /root/passwd_backup
      /etc/passwd
      [root@nglinux data]#

Leave a Reply

Your email address will not be published.