Fundamentals 7 min read

Master Linux File Linking: Hard & Soft Links with Practical Examples

Learn how to use the Linux 'ln' command to create and manage hard and symbolic links, understand their differences, and follow step‑by‑step examples for creating, modifying, and deleting links, including useful options and backup handling.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux File Linking: Hard & Soft Links with Practical Examples

ln Command Help

Usage: ln [OPTION]... [-T] TARGET LINK_NAME
   or:  ln [OPTION]... TARGET
   or:  ln [OPTION]... TARGET... DIRECTORY
   or:  ln [OPTION]... -t DIRECTORY TARGET...

Create a link to TARGET with the name LINK_NAME (first form).
Create a link to TARGET in the current directory (second form).
Create links to each TARGET in DIRECTORY (third and fourth forms).
Hard links are created by default; use <code>--symbolic</code> or <code>-s</code> for symbolic links.
The destination name must not already exist unless <code>--force</code> is used.

Common Link Options

--backup[=CONTROL]

– create a backup of each existing destination file. -b – same as --backup but without argument. -d, --directory – create a hard link to a directory (super‑user only). -f, --force – remove existing destination files. -i, --interactive – prompt before overwriting. -L, --logical – dereference symbolic links. -n, --no-dereference – treat a symlink to a directory as a normal file. -P, --physical – make hard links to symbolic links. -r, --relative – create symbolic links relative to the link location. -s, --symbolic – create symbolic (soft) links instead of hard links. -S, --suffix=SUFFIX – override the default 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. -v, --verbose – print each linked file name. --help – display help and exit. --version – display version information and exit.

Creating Hard and Soft Links

Hard Link : Allows a file to have multiple valid path names, protecting against accidental deletion. ln [source_file] [link_file] Soft (Symbolic) Link : Works like a shortcut, containing 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.txt

Hard Link Example

[root@Servera test]# ln testFiles/1.txt 2.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
└── testFiles
    └── 1.txt

Soft Link Example

[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.txt

Modify a 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.txt

Delete a 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.txt

For more details, refer to the GNU coreutils online help or the local info page info '(coreutils) ln invocation'.

Linux ln command illustration
Linux ln command illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxHard LinkSymbolic Linkln
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.