LinuxCommandLibrary

lndir

Create shadow directory of symbolic links

TLDR

Create a shadow directory in the current directory

$ lndir [path/to/directory]
copy

SYNOPSIS

lndir [options] fromdir [todir]

fromdir: The source directory whose structure and contents will be mirrored.
todir: The target directory where the symbolic link hierarchy will be created. If omitted, `.` (the current directory) is assumed.

PARAMETERS

-silent
    Suppresses output messages for each link created or updated.

-nosym
    Creates hard links instead of symbolic links. Note: Hard links can only be created within the same filesystem and cannot link directories.

-ignorelinks
    Do not create links to symbolic links found in the source directory (fromdir). If a symlink is encountered in fromdir, its target will not be linked.

-unchanged
    Do not update entries in todir that already exist and point to the correct, up-to-date file in fromdir. This can speed up operations on large, stable trees.

-force
    Always overwrite existing links or files in todir if they don't match the source, even if the target entry appears to be newer.

-dryrun, -n
    Perform a trial run without making any changes to the filesystem. This shows what lndir would do.

-allow-absolute-links
    If a symbolic link in fromdir is absolute, create an absolute symbolic link in todir. By default, lndir attempts to create relative links where possible.

DESCRIPTION

lndir is a utility designed to create a parallel directory hierarchy where files are represented by symbolic links (symlinks) to their original counterparts. This tool is particularly useful in development environments, especially when building software, as it allows developers to compile and test code in a separate build tree without duplicating large source files. Instead of copying gigabytes of source code, lndir creates a "shadow" directory structure populated with symlinks pointing back to the original source files. This saves significant disk space and ensures that changes to the source files are immediately reflected in the build tree without requiring a full re-copy.

When lndir processes a directory, it recursively creates corresponding subdirectories and then creates symlinks for each file found in the source directory. If a target directory already exists, lndir intelligently updates it by creating links for new files and ensuring existing links point to the correct, most recent versions of files, typically by comparing modification times. It can also handle various linking strategies, such as creating hard links instead of symlinks, or ignoring existing symlinks in the source. Its primary benefit lies in providing a convenient, space-efficient method for managing separate build or test environments that rely on a common set of source files.

CAVEATS

lndir does not remove files or directories from todir that no longer exist in fromdir. This means todir can accumulate stale entries over time if files are deleted from the source.

Using `-nosym` for hard links limits functionality to a single filesystem and prevents linking of directories.

Care must be taken when using `-force` as it can overwrite existing files or links in the target directory, potentially causing data loss if todir contains non-link files that you wish to preserve.

UPDATE LOGIC

When updating an existing todir, lndir compares the modification time (mtime) of files. If a source file's mtime is newer than the linked file in todir, or if the link is broken/incorrect, lndir will recreate or update the symbolic link to point to the correct, newer source file. This intelligent update mechanism ensures consistency without unnecessary re-linking.

COMMON USE CASES

Beyond software development, lndir can be used to create lightweight, flexible read-only copies of configuration directories, or to set up environments where multiple projects share a common set of assets without duplicating them on disk. It's a space-saving alternative to `cp -r` for situations where changes to the source should be immediately reflected in the target.

HISTORY

lndir originated within the X Window System development community, specifically as part of the `imake` build system. It was crucial for managing large source trees and enabling developers to build X in a separate directory without duplicating the entire source code. Its utility for creating shadow build trees made it a staple in many open-source projects that adopted `imake` or similar build methodologies. While modern build systems often provide their own mechanisms for handling out-of-source builds, lndir remains a useful standalone utility for its specific purpose.

SEE ALSO

ln(1), cp(1), rsync(1), mkdir(1)

Copied to clipboard