Checking for broken links in directory structures

find

Old hands will now object to the complexity of the shell script option and contend that a far more elegant solution is available with the find tool. I'm happy to field this objection. With find, I will use the -xtype l switch, which is intended precisely for detecting broken links. Listing 3 shows that this option also works.

Listing 3

Searching with find

01 $ find . -xtype l
02 ./project/version2/data/dataset3

Now I know which link is broken, but not yet where it points. In Listing 4, I will now combine find with a for loop (shortening Listing 1 by half). In line 1, I let find do all the work and get a list of all the broken links found below the starting directory. In the for loop (lines 2 to 5), readlink then determines the respective link target.

Listing 4

find-broken-links2.sh

01 entrylist=$(find "$1" -xtype l)
02 for entry in $entrylist; do
03   target=$(readlink "$entry")
04   echo "broken link: from $entry to $target"
05 done

If you now call the script from Listing 4, the output is reduced to the single broken reference in the example project directory (Listing 5).

Listing 5

Output from find-broken-links2.sh

$ ./find-broken-links2.sh .
broken link: from ./project/version2/data/dataset3 to project/version1/data/dataset3

Python Script

If you don't like to use the shell for programming, Python may be a better option. Listing 6 shows a Python script that is very similar in operation to Listing 1. It uses functions from the two standard modules os and sys. Line 1 imports them into the current namespace. Lines 3 to 19 define a function named walk() that walks the directory passed in as the top parameter and checks all entries in it. The call to walk() is made in line 22, after previously evaluating the directory passed in as a parameter in line 21.

Listing 6

find-broken-links.py

01 import os,sys
02
03 def walk(top):
04   try:
05     entries = os.listdir(top)
06   except os.error:
07     return
08
09   for name in entries:
10     path = os.path.join(top, name)
11     if os.path.isfile(path):
12       pass
13     if os.path.isdir(path):
14       walk(path)
15     if os.path.islink(path):
16       destination = os.readlink(path)
17       if not os.path.exists(path):
18         print("broken link: from %s points to %s" % (path, destination))
19   return
20
21 startingDir = sys.argv[1]
22 walk(startingDir)

First, the program creates and validates a directory listing (lines 4 to 7). In case of an error, the walk() function terminates here. Then the code checks each entry in the directory to see if it is a file (line 11), a directory (line 13), or a link (line 15). The routine skips files. For directories, the walk() function is called recursively, again with the directory name as parameter.

For a link, however, the readlink() function from the os module in line 16 finds the target. If it is empty, it is a broken link, and the function returns an error message to that effect. After checking all the entries in the directory, the function returns to the call point. If you call the script in the directory tree in my example, you will see output with two of the entries shown in Listing 2.

symlinks

The symlinks [2] tool is designed to clean up symbolic links, for example, by converting absolute links to relative links and removing broken links. There are two parameters, -r and -v, that let you tell symlinks to recursively search a directory structure and output detailed information about the links.

Listing 7 shows the call for the example project directory: symlinks finds one link that it classifies as broken ("dangling") and two relative links. A look at the runtime shows no significant difference between Listing 1 and Listing 3. To filter out only the broken links, just combine the symlinks call with egrep (Listing 8).

Listing 7

symlinks

$ symlinks -rv .
dangling: /home/frank/project/version2/data/dataset3 -> project/version1/data/dataset3
relative: /home/frank/project/old -> project/version1
relative: /home/frank/project/current -> project/version2

Listing 8

symlinks and egrep

$ symlinks -rv . | egrep "^dangling:"
dangling: /home/frank/project/version2/data/dataset3 -> project/version1/data/dataset3

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus