Where is my Broken Link
Post first published in nixtip
This task bugs me quite often, there many methods to perform this job but want to share the options I consider safer.
Having this scenario:
Find
-type l
True if is a symbolic link file type.
-exec test ! -e {}
Test if the file where the link points DOES NOT exists.
-print
Show the broken link
Outputs
Perl + bash
Outputs
Let’s explain the mini Perl
program that makes the trick.
-s
enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of –). Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl program. The following program prints “1” if the program is invoked with a -xyz switch, and “abc” if it is invoked with -xyz=abc.
-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the >argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to use >semicolons where you would in a normal program.
So… we use –s
to pick link parameter and –e
to execute the program.
Program
exit 5 unless (-e readlink($link))
returns where is the symbolic link pointing.
Arguments
--
marks the end of options and disables further option processing. Any arguments after the —-
are treated as filenames and arguments (bash
man).
-link=$link
pass link variable to Perl
program (see –s
flag)
-e
test if the file exists.