Wednesday, October 10, 2012

UNIX command to find symbolic link or soft link in Linux

There are two ways you can find symbolic link or soft link in UNIX based operating system e.g. Linux, Solaris, BSD or IBM AIX. First way is by using  ls command in UNIX which display files, directories and links in any directory and other way is by using UNIX find command which has 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 command for finding soft link in any directory. If you are new to UNIX operating system and not familiar with concept of soft link and hard link, I would recommend to get a good hand on it, as it is one of the most powerful feature of UNIX based system. One more concept which is quite important to learn is file and directory permissions, because UNIX provides 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. Any ways Let's start with first example of How to find symbolic link in UNIX using “ls” command:


ls command to find 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 files and directories, if you look carefully for links that String starats with small  L ( l for link). If you combine output of ls command with grep and use regular expression to find all entries which starts with small L  than you can easily find all soft link on any directories. here is full UNIX command example of finding 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/

^ character is a special regular expression which means start of line.

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 supports find, its not only available but most powerful way to find soft link in any directory or sub directory. When you use find command with option type and specify type as small L ( l for link), it display all soft link in specified path. for example following example of find command will show all soft link in current directory and its sub directory :

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

Here dot (.) denotes current directory. if you want to limit this search only with current directory and not to sub directory 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

These were two ways to find 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 they way if you guys know any other ways to find, show or display soft link in any directory or PATH then please share.

Other UNIX command tutorials you may like

No comments:

Post a Comment

Java67 Headline Animator