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
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.