1. Overview

Creating symbolic links allows us to access files more flexibly, even if the target files are in a different file system. In some cases, we need to find all those links to troubleshoot disk space issues or even to fix the broken ones.

In this tutorial, we’ll have a look at how to find all of the soft links for a specific file.

2. Setup

Let’s say we have a dir1 directory and a file1.txt file under the current working directory. Also, we’ve created multiple symbolic links on multiple directories pointing to the file and the directory using ln command :

[mogamal@server1:~/test]$ ls -lrth
total 12K
-rw-r--r-- 1 mogamal mogamal   11 Jun 11 16:50 file1.txt
drwxr-xr-x 4 mogamal mogamal 4.0K Jun 11 16:50 dir1
[mogamal@server1:/tmp]$ ln -s ~mogamal/test/file1.txt filelink
[mogamal@server1:/tmp]$ ln -s ~mogamal/test/dir1 dirlink
[mogamal@server1:/tmp]$ ls -lrth
total 49M
lrwxrwxrwx 1 mogamal mogamal   28 Jun 11 16:52 filelink -> /home/mogamal/test/file1.txt
lrwxrwxrwx 1 mogamal mogamal   28 Jun 11 16:52 dirlink -> /home/mogamal/test/dir1
...

Our goal is to find all links that mapped to file1.txt and dir1.

There are several ways to achieve that. Next, let’s see them in detail.

3. Using the find Command

The find command provides multiple options to find all links. Next, let’s try to find links for target files and directories using those options.

3.1. Find by Exact Filename

We can find and follow all links mapped to file1.txt  by adding the -L and -samefile options to the find command:

[mogamal@server1:~/test]$ find -L / -samefile file1.txt
/home/mogamal/test/file1.txt
/tmp/filelink
/opt/filelink2
/srv/filelink3
find: ‘/etc/polkit-1/localauthority’: Permission denied

As you see above, with option -samefile we add our filename or directory.
The search executed everywhere using root directory ” / ”  as the working directory.

Good, it works. All file1.txt links have been found.

For better readability, we can use redirections to redirect errors like “permission denied ” to /dev/null space:

[mogamal@server1:~/test]$ find -L / -samefile file1.txt 2> /dev/null
/home/mogamal/test/file1.txt
/tmp/filelink
/opt/filelink2
/srv/filelink3

3.2. Find by the Inode Number

When we work on a Linux file system, it uses inode numbers to store information about the files. When we list the files in a folder, we see links to inodes. An inode can have more than one link, and those can be symbolic or hard links. We can use stat on a file and see which inode it refers to:

[mogamal@server1:~/test]$ stat file1.txt
  File: file1.txt
  Size: 11              Blocks: 8          IO Block: 4096   regular file
Device: 810h/2064d      Inode: 94804       Links: 1

This file1.txt refers to inode 94804.

Let’s use the find command with the -inum action that refers to the inode of the file:

[mogamal@server1:~/test]$ find -L / -inum 94804 2> /dev/null 
/home/mogamal/test/file1.txt 
/tmp/filelink
/opt/filelink2
/srv/filelink3

Great, all links of file1.txt have been found.

3.3. Find by Recursive Method

The find command provides a -type option that allows multiple types to be specified. When we specify the type as small L ( l for the link), it displays all soft links in the specified path:

[mogamal@server1:~/test]$ find / -type l
/home/mogamal/test/dir1/certs/Buypass_Class_2_Root_CA.pem
/home/mogamal/test/dir1/certs/3fb36b73.0
/home/mogamal/test/dir1/certs/0f5dc4f3.0
...

Then we can append  -ls option to list the full attributes of the links:

[mogamal@server1:~/test]$ find / -type l -ls 2> /dev/null | more
    94809      0 lrwxrwxrwx   1 mogamal  mogamal        23 Jun 11 17:11 /tmp/dirlink -> /home/mogamal/test/dir1
    94805      0 lrwxrwxrwx   1 mogamal  mogamal        28 Jun 11 16:52 /srv/filelink -> /home/mogamal/test/file1.txt
    94808      0 lrwxrwxrwx   1 mogamal  mogamal        28 Jun 11 17:00 /tmp/filelink2 -> /home/mogamal/test/file1.txt
    94810      0 lrwxrwxrwx   1 mogamal  mogamal        24 Jun 11 17:11 /srv/dirlink2 -> /home/mogamal/test/dir1/
...

And finally, we can use the grep command to match the filename pattern which is file1.txt or dir1:

[mogamal@server1:~/test]$ find / -type l -ls 2> /dev/null | grep dir1
    94809      0 lrwxrwxrwx   1 mogamal  mogamal        23 Jun 11 17:11 /tmp/dirlink -> /home/mogamal/test/dir1
    94810      0 lrwxrwxrwx   1 mogamal  mogamal        24 Jun 11 17:11 /srv/dirlink2 -> /home/mogamal/test/dir1/
...

4. Conclusion

In this article, we’ve learned the different ways to find all of the soft links for files or directories. We also learned how to use find command options to achieve that.