Linkcheck featured online in, Shell Corner, August 2002.
Finding Broken Symbolic/Soft Links with 
by Bob Orlando

This Perl script,, locates and optionally removes broken links. We are not talking about web site links, but filesystem links. This kind of link is a file that refers or points to an object, either in the same directory or in a different directory. Unix has two kinds of links: hard links and symbolic or soft links.

According to the Unix Power Tools (Article 19.04), hard links point to the same inode and the same set of data blocks. Hard links are good, but they are limited for they can point only to files (no directories) and the files pointed to must reside in the same file system.

A symbolic or soft link is actually a file -- a special file, but a file nevertheless. As such, it has its own inode -- different from the object to which it points. A soft link can also point to other objects such as directories. Further, a soft link can cross file systems. To create a symbolic link you use the ln command. For example, to create a "perl" soft link in /usr/bin that points to the "/usr/local/bin/perl" you simply enter the following:

     ln -s /usr/proc/bin/ptree myptree

The command syntax seems somehow backward, so I always have to say to myself, link to an-actual-file by this-name, or in this case, link to /usr/proc/bin/ptree by myptree. Assuming /usr/proc/bin is not in my path, but my home directory (where I created the link) is, I can now call the ptree program just by typing "myptree". Granted this is a weak example for you can just as easily use an alias to accomplish the same thing in your interactive shell, but it does illustrate the point.)

A broken link occurs when the /usr/proc/bin directiory is deleted, or the ptree command there is removed.

Finally, according to the  ln  description in Unix in a Nutshell, you can see a soft link with  ls -l. To find hard links use  "find -inum"  to find a hard link. looks for all links on the local file system and tests each link's integrity -- to determine whether the link still valid, or if it points to an object that no longer exists. Only local filesystems are searched and this is ensured by testing each filesystem argument via  "df -l"  command.

Listing broken links is helpful, and you can certainly write a much shorter Perl program to do that, but comes with other useful options:

-a   Display All links
-H   Detailed documentation
-h   Usage brief
-l   Long list (e.g., ls -al)
-r   Remove broken links (use with caution)
-v   Verbose output
fs   Required filesystem for search (multiple filesystems may be specified)

Usage and program documentation are provided via -h and -H option, respectively. Verbosity is provided via -v option, and while it only turns on diagnostics and prints out a "Removed" message in this program, I code it as a matter of routine. Having a verbose option has proven indispensable in both Perl development and debugging.

The fs argument checks a specific filesystem, but doesn't waste time chasing down broken links on remotely mounted file systems. Options a, l, and r are production options. These provide a display of all links (including broken links), a long list display (e.g., ls -al), and broken link removal.

By default, displays links in short format (e.g., ftpusers -> /etc/ftpusers), but using the -l option, gives you something like this:

   Linked file: lrwxrwxrwx 1 root other 13 Nov 12 2001 ftpusers -> /etc/ftpusers

Since the preceding file link is not broken, the only way to see it is to specify the all (-a) option.

The linked file: preceding the long list quickly identifiesthe linked object. A -al / might yield something like the following:

   Linked dir:  lrwxrwxrwx 1 root root   9 Nov 12 2001 /bin -> ./usr/bin
   Linked char: lrwxrwxrwx 1 root other 25 Nov 12 2001 /AppsSrvr/dev/zero \
     -> devices/pseudo/mm@0:zero 
Broken link: lrwxrwxrwx 1 root root 17 Nov 12 2001 \ /usr/lib/locale/fr_CA.ISO8859-1/LC_MESSAGES \ -> ../fr/LC_MESSAGES

The list of link objects identified are as follows:

??? (possibly a SunOS door)

In conclusion, a recent run on some 50 Unix (SunOS and Linux) hosts uncovered nearly 3000 broken links. With the -r  option, removing the broken links was as easy as finding them. However, this option should be used with care.

Artificial  intelligence  is  no  match  for  natural  stupidity.
©Copyright Bob Orlando, 2002-2016
All rights reserved.
Last update:  Feb. 2, 2016
by Bob Orlando
Web Site of Bob Orlando: Instructor in Kuntao-Silat (Chinese kuntao and Dutch-Indonesian pukulan pentjak silat), author of two popular martial art books: "Indonesian Fighting Fundamentals" and "Martial Arts America: A Western Approach to Eastern Arts"; and producer of four martial art videos: Fighting Arts of Indonesia, Reflex Action, Fighting Footwork of Kuntao and Silat, Fighting Forms of Kuntao-Silat. Offering practical martial arts instruction to adults living in and throughout the Denver metropolitan area including, Lakewood, Littleton, Morrison, and Golden Colorado.