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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.