Builds and scripts often expect to find a file at a fixed path. On Linux, a symbolic link gives an existing file or directory another path without making a copy.

A relative target is resolved from the directory that holds the link. It can keep working if the link and target move together and retain the same layout. An absolute target starts at the filesystem root.

Creating a symbolic link requires write access to the directory that will hold it. Anyone who uses the link still needs permission to access the target.

  1. Create a symbolic link at an unused path for your file or directory.

    Both commands run from a project directory with docs/notes.txt and a writable links/ directory. The -s option creates a symbolic link. With -T, an occupied link name is an error, even if it names a directory.

    For a file link, replace ../docs/notes.txt with your target and links/notes.txt with your new link path. Relative targets start in the link's parent directory, links/ here.

    $ ln -sT -- \
        ../docs/notes.txt \
        links/notes.txt

    For a directory link, replace "$PWD/docs" with your target and links/current-docs with your new link path. Here, $PWD/docs expands to the full path of docs/.

    $ ln -sT -- \
        "$PWD/docs" \
        links/current-docs
  2. Match the path after -> to the target you supplied.

    For a file link, replace links/notes.txt with the link path you chose. The file example shows ../docs/notes.txt after the arrow.

    $ ls -ld -- links/notes.txt

    For a directory link, replace links/current-docs with the link path you chose, without a trailing slash. The directory example shows /tmp/project/docs when run from /tmp/project.

    $ ls -ld -- \
        links/current-docs

    The leading l identifies a symbolic link. The readlink command with your link path prints just the stored target.

  3. Check that your link reaches the intended file or directory.

    For a file link, replace links/notes.txt with the link path you chose. A successful read shows the target file's contents.

    $ cat -- links/notes.txt
    Deployment notes

    For a directory link, replace links/current-docs/ with the link path you chose and keep the trailing slash to list the target directory's contents.

    $ ls -- links/current-docs/
    notes.txt

    A missing target can still appear after the -> arrow. Opening the link exposes that error.

Share this guide