Find all links for a specific file on Linux

Overview In this tutorial, we will learn how to find all links for a specific file on Linux. We will use the command lsof to list all files that are opened by any process and then grep to filter out only those files that h**e a link to our target file. Wh...

Overview

In this tutorial, we will learn how to find all links for a specific file on Linux. We will use the command lsof to list all files that are opened by any process and then grep to filter out only those files that h**e a link to our target file.

What is a Link?

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 or directory. You can access the linked file through its alias instead of accessing the original file directly. For example, if you create a symbolic link called “test” pointing to /home/user/Desktop/file1.txt, you can access the file using test instead of Desktop/file1.txt.

Setup

We've got a dir1 folder and a file1.txt document inside it. And we've created several symbolic links from different folders to the text document and the folder using the ln command.

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

We want to identify all links that map to file1.txt or dir1.

You can use several different methods to achieve that. Now, we’ll look at each one in detail.

Using the find Command

The find option allows you to find all links within a document. Let’s now see if we can find any useful link targets by using those two filters.

Find by Exact Filename

To locate and follow all hyperlinked files named file1.txt, we add the −L option to the grep command −

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

With the −samefile switch, we add our file name or folder path to the command line. The search executes everywhere using the root (/) as the working dir.

Great! It worked. All file1.txt links h**e now been found.

We can use redirections to send error messages like permission denied to /dev/null.

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

Find by the Inode Number

When working on a Linux file system (such as ext4), it stores information about the file using inodes. We list the files in a directory by listing their in−memory locations (in−odes). Each in−ode has multiple links, which may be either symbolic or hard links. We can tell whether a file has been modified by looking at its inodes.

[tpoint@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 is stored at inode number 94804.

Let's use the 'find' command with the '−inum' option that refers to the inodes of the files we want to delete.

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

All links of file1.text h**e been found.

Find by Recursive Method

The −type option allows multiple file type specifications to be provided. If we specify the type as "small L", it shows us all soft links in the given directory.

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

We can then append the -ls option to show the full attributes of each link.

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

Finally, we can use the `grep` command to search for files matching a specific filename pattern (file1.txt or dir/dir1).

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

Conclusion

Here, we've covered the different methods to find all of the hard links for files or directories, and how to use find command line arguments to accomplish that.

评论0

首页 导航 会员 客服