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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Mhit
Mhit
6 years ago

Nice post dear… can we search on all hard disks

Aakanksha
Admin
Reply to  Mhit
6 years ago

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

mohit
mohit
6 years ago

is there any way to find all symlinks of a file…

Saket
Saket
Reply to  mohit
6 years ago

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…

Pratipal
Pratipal
6 years ago

Hello Saket,

Just a suggestion, if we need to null the error messages as well for the better visibility of output.

Aakanksha
Admin
Reply to  Pratipal
6 years ago

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]#