Master Linux File Linking: Hard & Symbolic Links Explained with Real Examples
This guide walks you through the Linux ln command, detailing its help output, common options, how to create and modify both hard and symbolic links, and provides step‑by‑step examples for creating, updating, and removing links safely.
Software Help Text
Usage: ln [OPTION]... [-T] TARGET LINK_NAME
or: ln [OPTION]... TARGET
or: ln [OPTION]... TARGET... DIRECTORY
or: ln [OPTION]... -t DIRECTORY TARGET...
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.
--backup[=CONTROL] create a backup of each existing target file
-b same as --backup but takes no argument
-d, -F, --directory create a hard link to a directory (super‑user only)
-f, --force force removal of any existing target file
-i, --interactive prompt whether to remove destinations
-L, --logical dereference TARGETs that are symbolic links
-n, --no-dereference treat LINK_NAME as a normal file if it is a symbolic link to a directory
-P, --physical make hard links directly to symbolic links
-r, --relative create symbolic links relative to link location
-s, --symbolic make symbolic links instead of hard links
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY specify the DIRECTORY in which to create the links
-T, --no-target-directory treat LINK_NAME as a normal file always
-v, --verbose print name of each linked file
--help display this help and exit
--version output version information and exitCommon Link Commands
Create Hard and Soft Links
Hard link : Allows a file to have multiple valid path names, useful for protecting important files from accidental deletion. ln [source_file] [link_file] Soft (symbolic) link : Similar to a Windows shortcut; it stores the path to another file.
ln -s [source_file] [link_file]Examples
Prerequisite
[root@Servera test]# pwd
/var/test
[root@Servera test]# tree
.
├── soft_link
└── testFiles
└── 1.txtHard Link
[root@Servera test]# ln testFiles/1.txt 2.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
└── testFiles
└── 1.txtSoft Link
[root@Servera test]# ln -s testFiles/1.txt soft_link/3.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
│ └── 3.txt -> testFiles/1.txt
└── testFiles
└── 1.txtModify Soft Link
[root@Servera test]# ln -snf testFiles/10.txt soft_link/3.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
│ └── 3.txt -> testFiles/10.txt
└── testFiles
├── 10.txt
└── 1.txtDelete Soft Link
[root@Servera test]# rm soft_link/3.txt
rm: remove symbolic link 'soft_link/3.txt'? yes
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
└── testFiles
├── 10.txt
└── 1.txtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
