Checking for broken links in directory structures
Dead End
Broken links can wreak havoc in directory structures. This article shows you how to use scripts to avoid having your links lead to a dead end.
During a restore process, nothing is more disappointing than discovering that some of the links in your previously backed up data no longer work. Although the link is still there, the target no longer exists, resulting in the link pointing nowhere. These broken data structures can also cause problems when you are developing software and publishing it in the form of an archive, or if you need to install different versions of an application.
Finding and fixing broken links manually takes a lot of effort. You can avoid this scenario by using scripts and Unix/Linux tools to check for broken links in directory structures. In this article, I'll look at several ways to check the consistency of these data structures and detect broken links. Read on to avoid hiccups for you and your users.
Sample Data
As an example, I will use the directory structure shown in Figure 1, which is similar to a piece of software or a project directory that you might encounter in the wild. You can easily create a tree such as Figure 1 with the tree
command [1].
The directory tree in Figure 1 contains two versions of the software. There are three links: One points to the old version (named old
), one to the current version (named current
), and the third to a data file named dataset3
(which is missing).
Options
A small, manageable project structure like Figure 1 can be tested and checked manually. With larger projects, however, this quickly leads to errors because you are bound to overlook something. To automate the testing procedure, I rummaged around in my Unix/Linux toolbox and came up with four options that are suitable for everyday use: a shell script as a combination of a recursive function and a for
loop over all the files and directories, a special call to find
, a Python script, and the tools symlinks
, FSlint, rmlint
, and chase
.
Shell Script
The shell script (Listing 1) uses a recursive function named check()
. check()
only expects one parameter: the directory you want it to check for broken links (line 16). In the function, a for
loop iterates across all entries (lines 2 to 14).
Listing 1
find-broken-links.sh
01 function check { 02 for entry in $1/*; do 03 # echo "check $entry ... " 04 if [ -d "$entry" ]; then 05 check $entry 06 else 07 if [ -L "$entry" ]; then 08 target=$(readlink "$entry") 09 if [ ! -e "$target" ]; then 10 echo "broken link: from $entry to $target" 11 fi 12 fi 13 fi 14 done 15 } 16 check $1
For each entry, check()
first checks whether the entry actually is a directory (line 4). If so, the function is called again with this directory as a parameter (line 5). Otherwise, two more tests are made: Is it a link (line 7), and, if so, where does it point to (lines 8 and 9)? In line 8, the readlink
command returns the target to which the link points and stores the result in the local variable target
.
In line 9, the script checks if the link target exists. If not, the function sends an error message to that effect to stdout
(line 10). The routine ignores entries in the directory that are not links. Once the entire list has been processed, check()
returns to the call point. After processing the entire original directory, the script exits.
If you now call the script, you will see output similar to that in Listing 2. I made the call using a period (.
) for the current directory as the starting point. The output includes two lines because current
points to version2
and my function follows the link.
Listing 2
Output of find-broken-links.sh
$ ./find-broken-links.sh . broken link: from ./project/current/data/dataset3 to project/version1/data/dataset3 broken link: from ./project/version2/data/dataset3 to project/version1/data/dataset3
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
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.