top of page
Writer's pictureThe Tech Platform

How to find symbolic link or soft link in Linux - ls + find command example

There are two ways you can find a symbolic link or soft link in UNIX based operating system like Linux, Solaris, BSD, or IBM AIX. The first way is by using the ls command in UNIX which displays files, directories, and links in any directory and the other way is by using UNIX find command which has the ability to search any kind of files e.g. file, directory, or link. In this UNIX command tutorial, we will see examples of both of these UNIX commands for finding a soft link in any directory. If you are new to UNIX operating system and not familiar with the concept of the soft link and hard link, I would recommend getting a good hand on it, as it is one of the most powerful features of UNIX based system. One more concept which is quite important to learn is file and directory permissions because UNIX provides a different level of permissions and link inherit permission of destination folder where they are pointing out. This is also a very popular Linux interview question asked on both system admin as well as the developer-level interview. Anyways Let's start with the first example of how to find a symbolic link in UNIX using the “ls” command:



ls command to find a symbolic link in UNIX systems

when you run the ls -lrt command in any directory it prints permission details of each file and directories, if you look carefully for links that String starts with a small L ( l for the link). If you combine the output of ls command with grep and use a regular expression to find all entries which start with small L than you can easily find all soft link on any directories.

Here is the full UNIX command example of finding the symbolic link using ls and grep command:

java67@blogspot:~ ls -lrt 
total 3.0K 
-rw-r--r--  1 java67 Domain Users 79 Jul 19 2011 test.txt 
drwxr-xr-x+ 1 java67 Domain Users  0 Jun 15 12:07 unix/ 
drwxr-xr-x+ 1 java67 Domain Users  0 Sep 19 12:30 java/ 
lrwxrwxrwx  1 java67 Domain Users  4 Sep 19 12:31 version_1.0 -> java/ 
lrwxrwxrwx  1 java67 Domain Users  4 Sep 21 13:59 os -> unix/
   
java67@blogspot:~ ls -lrt | grep ^l 
lrwxrwxrwx  1 java67 Domain Users  4 Sep 19 12:31 version_1.0 -> java/ 
lrwxrwxrwx  1 java67 Domain Users  4 Sep 21 13:59 os -> unix/

The ^ character is a special regular expression which means the start of the line. You can further check Learn Linux in 5 Days and Level Up Your Career, an Udemy course to learn more options of ls and other essential Linux commands.


Showing soft link using Find command in Unix

Another way to find all soft links or symbolic links is by using the find command in UNIX. Since all UNIX based operating system e.g. Linux, Solaris, or IBM AIX support find, it's not only available but the most powerful way to find a soft link in any directory or subdirectory. When you use the find command with option type and specify the type as small L ( l for the link), it displays all soft links in the specified path. For example, following the example of find command will show all soft link in the current directory and its subdirectory :

java67@blogspot:~ find . -type l 
./java/version_1.0 
./os 
./version_1.0

Here the dot (.) character denotes a current directory. If you want to limit this search only with the current directory and not to a subdirectory than you can specify maximum depth as 1 by using -maxdepth option of find command as shown in the below example :

java67@blogspot:~ find . -maxdepth 1 -type l 
./os 
./version_1.0

As I said before, find it very useful command but most of the UNIX user doesn't use find to its full potential. They are not even aware of some of its useful options like this one.


These were two ways to find a soft link or symbolic links in UNIX operating system e.g. Linux, Solaris, or IBM AIX. I am still searching UNIX command which can find all broken links and will update this example when I got that. By the way, if you guys know any other ways to find, show or display the soft link in any directory or PATH then please share.



Source: java67


The Tech Platform

0 comments

Comments


bottom of page