UNIX command to find symbolic link or soft link in Linux

There are two ways you can find a symbolic link or soft link in UNIX based operating system e.g. Linux, Solaris, BSD or IBM AIX. The first way is by using ls command in UNIX which displays files, directories, and links in any directory and 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 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 developer level interview. Anyways Let's start with the first example of How to find a symbolic link in UNIX using “ls” command:




ls command to find a symbolic link in UNIX systems

How to find soft link in UNIX and Linuxwhen we do ls -lrt in any directory it prints permission details of each file and directories, if you look carefully for links that String starts with 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 Linux Command Line Interface (CLI) Fundamentals, a free course from Pluralsight, to learn more options of ls and other essential UNIX commands. 




Showing soft link using Find command in Unix

Another way to find all soft links or symbolic links is by using 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 most powerful way to find a soft link in any directory or subdirectory.

When you use find command with option type and specify the type as small L ( l for the link), it displays all soft link 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. 

Ho to find symbolic link or soft link in Linux


If you want to limit this search only with current directory and not to subdirectory than you can specify maximum depth as 1 by using -maxdepth option of find command as shown in below example :

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

As I said before, find is 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.

If you are eager to learn more about advanced options of find and other essential commands, I suggest you check Linux System Administration Fundamentals, an online course to learn Linux commands from Pluralsight. You can get this free when you sign up for 10-day free trial.

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.


Linux System Administration Fundamentals

Thanks for reading this UNIX tutorial, if you like this example and my explanation then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

No comments:

Post a Comment