Skip to content

Instantly share code, notes, and snippets.

@mesheets
Created January 13, 2026 12:41
Show Gist options
  • Select an option

  • Save mesheets/c02c9dfb42cfb896cf4438d21af7a293 to your computer and use it in GitHub Desktop.

Select an option

Save mesheets/c02c9dfb42cfb896cf4438d21af7a293 to your computer and use it in GitHub Desktop.
Create a Deep Shadow Link Tree
#! /bin/sh
# lndir - create deep shadow link tree
#
# Time stamp <1989/11/28 18:56:54 gildea>
# By Stephen Gildea <gildea@bbn.com> based on
# XConsortium: lndir.sh,v 1.1 1988/10/20 17:37:16 jim Exp
#
# Modified slightly for ImageMagick by Bob Friesenhahn, 1999
# * https://github.com/ImageMagick/ImageMagick/blob/main/config/lndir.sh
# * https://github.com/ImageMagick/WizardsToolkit/blob/main/config/lndir.sh
#
# Updated and modified by Matthew Sheets, 2026-01-10
#
# Used to create a copy of the a directory tree that has links for all
# non-directories. If you are building the distribution on more than
# one machine, you should use this script.
#
# A key difference between this script and the "lndir" executable from the
# X.org project is that when this script encounters a linked directory, it
# will create a new directory in the shadow link tree and then continue
# linking files recursively. X.org's lndir executable, on the other hand,
# will merely create a link to the linked directory and stop further recursively
# processing on that particular path.
#
# USAGE EXAMPLE:
# If your main sources are located in /usr/local/src/X and you would like
# your link tree to be in /usr/local/src/new-X, do the following:
#
# % mkdir /usr/local/src/new-X
# % cd /usr/local/src/new-X
# % lndir ../X
#
# Improvements over R3 version:
# Allows the fromdir to be relative: usually you want to say "../dist"
# The name is relative to the todir, not the current directory.
#
# Bugs in R3 version fixed:
# Do "pwd" command *after* "cd $DIRTO".
# Don't try to link directories, avoiding error message "<dir> exists".
# Barf with Usage message if either DIRFROM *or* DIRTO is not a directory.
#
# Changes over ImageMagick version:
# ShellCheck'd.
# More robust capturing of script name for when called recursively.
# Now also links hidden files (those beginning with ".").
# Resolves encountered file links to create a link directly to the target
# (like X.org's lndir).
SCRIPTNAME="$(basename "$0")"
SCRIPTPATH="$(realpath "$0")"
USAGE="Usage: $SCRIPTNAME fromdir [todir]"
if [ $# -lt 1 ] || [ $# -gt 2 ]
then
echo "$USAGE"
exit 1
fi
DIRFROM="$1"
if [ $# -eq 2 ];
then
DIRTO="$2"
else
DIRTO=.
fi
if [ ! -d "$DIRTO" ]
then
echo "$SCRIPTNAME: \"$DIRTO\" is not a directory"
echo "$USAGE"
exit 2
fi
cd "$DIRTO" || exit 1
if [ ! -d "$DIRFROM" ]
then
echo "$SCRIPTNAME: \"$DIRFROM\" is not a directory"
echo "$USAGE"
exit 2
fi
pwd="$(pwd)"
if [ "$(cd "$DIRFROM" ; pwd)" = "$pwd" ]
then
echo "$pwd: FROM and TO are identical!"
exit 1
fi
for file in .* *
do
if [ ! -d "$DIRFROM/$file" ]
then
test -r "$file" || ln -s "$(realpath --relative-to=. "$DIRFROM/$file")" .
else
#echo "$file":
test -d "$file" || mkdir "$file" && chmod 777 "$file"
(
cd "$file" || exit 1
pwd="$(pwd)"
case "$DIRFROM" in
/*) ;;
*) DIRFROM="../$DIRFROM" ;;
esac
if [ "$(cd "$DIRFROM/$file" ; pwd)" = "$pwd" ]
then
echo "$pwd: FROM and TO are identical!"
exit 1
fi
"$SCRIPTPATH" "$DIRFROM/$file"
)
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment