Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find all links for a specific file on Linux
In Linux, finding all links for a specific file is a common administrative task. A link in Unix/Linux systems is an association between two different files or directories. When you create a link, it creates a new name for the original file. There are two types of links: hard links (multiple names for the same inode) and symbolic links (shortcuts that point to another file path).
Setup Example
Let's start with a practical example. We have a file1.txt document and several symbolic links pointing to it from different locations:
[tpoint@server1:~/test]$ ls -lrth total 12K -rw-r--r-- 1 tpoint tpoint 11 Jun 11 16:50 file1.txt drwxr-xr-x 4 tpoint tpoint 4.0K Jun 11 16:50 dir1 [tpoint@server1:/tmp]$ ln -s /home/tpoint/test/file1.txt filelink [tpoint@server1:/tmp]$ ln -s /home/tpoint/test/dir1 dirlink [tpoint@server1:/tmp]$ ls -lrth total 49M lrwxrwxrwx 1 tpoint tpoint 28 Jun 11 16:52 filelink -> /home/tpoint/test/file1.txt lrwxrwxrwx 1 tpoint tpoint 28 Jun 11 16:52 dirlink -> /home/tpoint/test/dir1
Method 1 Using find with -samefile
The most direct approach is using the find command with the -samefile option. The -L flag tells find to follow symbolic links:
find -L / -samefile file1.txt 2> /dev/null
/home/tpoint/test/file1.txt /tmp/filelink /opt/filelink2 /srv/filelink3
This searches the entire filesystem starting from root (/) and finds all files that refer to the same inode as file1.txt. We redirect error messages to /dev/null to avoid permission denied warnings.
Method 2 Using Inode Numbers
Every file in Linux has an inode number, a unique identifier within the filesystem. First, get the inode number:
stat file1.txt
File: file1.txt Size: 11 Blocks: 8 IO Block: 4096 regular file Device: 810h/2064d Inode: 94804 Links: 1
Now find all files with the same inode number:
find -L / -inum 94804 2> /dev/null
/home/tpoint/test/file1.txt /tmp/filelink /opt/filelink2 /srv/filelink3
Method 3 Finding All Symbolic Links
To find all symbolic links in the system and filter for specific targets:
find / -type l -ls 2> /dev/null | grep "file1.txt"
94805 0 lrwxrwxrwx 1 tpoint tpoint 28 Jun 11 16:52 /srv/filelink3 -> /home/tpoint/test/file1.txt 94806 0 lrwxrwxrwx 1 tpoint tpoint 28 Jun 11 16:52 /tmp/filelink -> /home/tpoint/test/file1.txt 94808 0 lrwxrwxrwx 1 tpoint tpoint 28 Jun 11 17:00 /opt/filelink2 -> /home/tpoint/test/file1.txt
The -type l option finds only symbolic links, -ls provides detailed output, and grep filters for our target file.
Comparison of Methods
| Method | Use Case | Finds Hard Links | Finds Symbolic Links |
|---|---|---|---|
| -samefile | All types of links | Yes | Yes (with -L) |
| -inum | Same inode references | Yes | Yes (with -L) |
| -type l + grep | Only symbolic links | No | Yes |
Conclusion
Finding all links for a specific file can be accomplished using several find command variations. The -samefile option is most straightforward, while -inum works with inode numbers. For symbolic links only, use -type l with grep filtering.
