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
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.