How to create a symlink


Create a symlink (symbolic link) to alias another file or directory:

ln -s <source> <destination>

Usage

Given the directory structure:

.
└── path
    └── to
        └── source

You can create a symlink like below:

ln -s path/to/source path/to/target

You’ll now have the following:

.
└── path
    └── to
        ├── target -> path/to/source
        └── source

But what if the target file already exists?

ln -s path/to/source path/to/target
ln: path/to/target: File exists

You can force an override with the -f option:

ln -sf path/to/source path/to/target

Also, if you don’t want the symlink to be relative, then make the path absolute:

ln -s "$(pwd)/path/to/source" path/to/target

Or use realpath:

ln -s "$(realpath path/to/source)" path/to/target

This ensures the symlink still points to the expected location even if it’s moved.

Check out the manual page for more information:

man ln


Please support this site and join our Discord!