Symlinks make it easy to create shortcuts to files & folders in Linux. Very often you may need to list the symlinks in a folder or those pointing towards a file/directory. In this article, we will look at how to find symlinks in a directory as well as those symlinks pointing towards other file/directory.
How to Find All Symlinks to File/Directory
We will use find command to list symlinks for our purpose.
1. Find symlinks to a folder/directory
Here is the command to find all symlinks pointing towards folder /home/data
$ sudo find . -lname /home/data
The above command will recursively search current directory for symlinks to /home/data.
If you want to search a directory /etc/project for symlinks to another folder /home/data, replace . above with /etc/project.
$ sudo find /etc/project -lname /home/data
If you want to search your entire disk for all symlinks to particular folder, use find / above
$ sudo find / -lname /home/data
The above command will search the entire system for symlinks to your folder. But please note, this is a time-consuming process since find command has to search all folders & subfolders recursively. This can be an exhaustive search and take up considerable amount of system time & resources. So please be careful before using the above command.
2. Find symlinks in a folder
Sometimes you may need to list all symlinks pointing away from one folder. If you want to list all symlinks in current folder, you can simply get this with the following command.
$ sudo find . -type l
-type l option lists only symlinks in its output. If you want to list all symlinks in a specific folder /home/data
$ sudo find /home/data -type l
If you want to list all symlinks on your system run the following command.
$ sudo find / -type l
That’s it. In this article, we have learnt how to list symlinks pointing towards one folder, as well as list symlinks pointing away from one folder. As you can see, find is quite useful in searching for symlinks on your system.
Also read :
Shell Script to Count Number of Files in Directory
Shell Script to Count Number of Words in File
How to Enable & Disable Services in Linux
NGINX Root vs Alias
How to Download File to Directory using wget
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.