Skip to main content

ln Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for creating hard links and symbolic links with ln in Linux

The `ln` command creates hard links and symbolic (soft) links between files and directories. This cheatsheet covers symbolic and hard link creation, link management options, and how to inspect and verify links.

Basic Syntax

Core ln command forms.

CommandDescription
ln TARGET LINK_NAMECreate a hard link
ln -s TARGET LINK_NAMECreate a symbolic (soft) link
ln -sf TARGET LINK_NAMECreate or overwrite a symbolic link
ln TARGET... DIRECTORYCreate hard links to multiple targets in a directory
ln -s TARGET... DIRECTORYCreate symbolic links to multiple targets in a directory

Create and manage soft links that point to a path.

CommandDescription
ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/appEnable an Nginx virtual host
ln -s /usr/bin/python3 /usr/local/bin/pythonCreate a python alias
ln -s /opt/myapp /usr/local/bin/myappLink a binary into PATH
ln -s TARGET .Create a symlink to TARGET in the current directory
ln -sf NEW_TARGET LINK_NAMEUpdate an existing symlink to point to a new target

Create additional directory entries pointing to the same inode.

CommandDescription
ln source.txt hardlink.txtCreate a hard link to a file
ln file1.txt file2.txt /backup/Create hard links to multiple files in a directory
ln -v source.txt link.txtShow each link as it is created

Hard links share the same inode and data. Removing one does not delete the data until all hard links are removed. Hard links cannot span filesystems and cannot be created for directories.

Flags for controlling overwrite, backup, and verbosity behavior.

OptionDescription
-s, --symbolicCreate a symbolic link instead of a hard link
-f, --forceRemove the destination file if it exists before linking
-b, --backupMake a backup of each existing destination file
-i, --interactivePrompt before removing an existing destination file
-n, --no-dereferenceTreat a symlink to a directory as a normal file
-v, --verbosePrint the name of each linked file
-r, --relativeCreate symbolic links relative to the link location

Check where links point and confirm they are valid.

CommandDescription
ls -l link_nameShow the link and its target
ls -la /path/List all entries including hidden symlinks
readlink link_namePrint the target of a symbolic link
readlink -f link_namePrint the fully resolved absolute path
stat link_nameShow full metadata including inode and link count
file link_nameIdentify whether a path is a symbolic link
find . -type lFind all symbolic links under the current directory
find . -xtype lFind broken symbolic links

Troubleshooting

Quick checks for common ln issues.

IssueCheck
File existsUse -f to force overwrite, or -i to confirm before replacing
Too many levels of symbolic linksA circular symlink chain exists; use readlink -f to trace the path
Invalid cross-device linkHard links cannot span different filesystems; use a symbolic link instead
Broken symlink (dangling)The target path no longer exists; update with ln -sf NEW_TARGET LINK_NAME
Symlink points to wrong targetRun readlink link_name to confirm the current target, then use ln -sf to correct it

Use these guides for full workflows and file management patterns.

GuideDescription
ln Command in LinuxFull ln tutorial with examples
How to Remove Symbolic Links in LinuxDelete symlinks safely
unlink Command in LinuxRemove a single link entry
ls Command in LinuxList and inspect files and links
chmod Command in LinuxSet permissions on files and links