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
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.