Master Linux File Links: Soft, Hard, and Symbolic Link Techniques
This guide explains Linux file linking concepts—including soft, hard, and symbolic links—detailing how to create, inspect, and remove them with commands like ln, ls, touch, cat, rm, and unlink, while highlighting inode behavior and directory restrictions.
Introduction
This guide introduces what file links are in Linux and how to use them effectively.
Viewing Link Documentation
Running man ln shows the basic usage of creating links between files, while man link describes the link creation function.
Soft Link
A soft link (symbolic link) creates a new reference to a file; the new file’s inode points to the original file.
Hard Link
A hard link makes the old and new files share the same inode number.
Symbolic Link
In many Unix/Linux systems, symbolic links and soft links are treated the same, but the new file may point to a different inode depending on implementation.
Note: The terms “symbolic link” and “soft link” are often interchangeable, but you must know when to use each.
Creating Hard Links
shashi@linuxtechi ~}$ man ln<br/>ln - make links between files shashi@linuxtechi ~}$ ln<br/>ln: missing file operand<br/>Try 'ln --help' for more information. shashi@linuxtechi ~}$ ls -la<br/>total 24<br/>drwx------ 4 root root 4096 Feb 6 15:23 .<br/>drwxr-xr-x 23 root root 4096 Jan 25 16:39 ..<br/>-rw-r--r-- 1 root root 3122 Jan 25 16:41 .bashrc<br/>drwx------ 2 root root 4096 Feb 6 15:23 .cache<br/>-rw-r--r-- 1 root root 0 Jan 25 16:41 .hushlogin<br/>-rw-r--r-- 1 root root 148 Aug 17 2015 .profile<br/>drwxr-xr-x 2 root root 4096 Jan 25 16:41 .ssh shashi@linuxtechi ~}$ touch 123.txt<br/>shashi@linuxtechi ~}$ ls -l<br/>total 0<br/>-rw-r--r-- 1 root root 0 Feb 6 15:51 123.txt shashi@linuxtechi ~}$ cat > 123.txt<br/>Welcome to this World!<br/>^C shashi@linuxtechi ~}$ ln 123.txt 321.txt<br/>shashi@linuxtechi ~}$ ls -l<br/>total 8<br/>-rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt<br/>-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt shashi@linuxtechi ~}$ ls -li<br/>794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 123.txt<br/>794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt shashi@linuxtechi ~}$ rm 123.txt<br/>shashi@linuxtechi ~}$ ls -l<br/>total 8<br/>-rw-r--r-- 2 root root 23 Feb 6 15:52 321.txt shashi@linuxtechi ~}$ ls -li<br/>794583 -rw-r--r-- 2 root root 23 Feb 6 15:52 321.txtHard links cannot be created across directories.
shashi@linuxtechi ~}$ mkdir abc<br/>shashi@linuxtechi ~}$ ln abc def<br/>ln: abc: hard link not allowed for directoryCreating Soft (Symbolic) Links
shashi@linuxtechi ~}$ touch src.txt<br/>shashi@linuxtechi ~}$ cat > src.txt<br/>Hello World<br/>^C<br/>shashi@linuxtechi ~}$ ls -l<br/>total 4<br/>-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ ln -s src.txt dst.txt<br/>shashi@linuxtechi ~}$ ls -l<br/>total 4<br/>lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt<br/>-rw-r--r-- 1 root root 12 Feb 6 16:32 src.txt shashi@linuxtechi ~}$ cat dst.txt<br/>Hello World shashi@linuxtechi ~}$ ls -li<br/>794584 lrwxrwxrwx 1 root root 7 Feb 6 16:33 dst.txt -> src.txt<br/>794583 -rw-r--r-- 1 root root 12 Feb 6 16:32 src.txtSymbolic links can also point to directories.
shashi@linuxtechi ~}$ mkdir abc<br/>shashi@linuxtechi ~}$ ln -s abc def<br/>shashi@linuxtechi ~}$ ls -l<br/>drwxr-xr-x 2 root root 4096 Feb 6 16:34 abc<br/>lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abcWhen the source directory receives new files, the linked target directory reflects the same changes.
shashi@linuxtechi ~}$ cd abc<br/>shashi@linuxtechi ~}$ touch 123.txt<br/>shashi@linuxtechi ~}$ vi 123.txt<br/>Hello<br/>:wq!<br/>shashi@linuxtechi ~}$ touch 456.txt<br/>shashi@linuxtechi ~}$ cd ..<br/>shashi@linuxtechi ~}$ ls -l<br/>drwxr-xr-x 2 root root 4096 Feb 6 16:36 abc<br/>lrwxrwxrwx 1 root root 3 Feb 6 16:34 def -> abc shashi@linuxtechi ~}$ cd def<br/>shashi@linuxtechi ~}$ ls -l<br/>-rw-r--r-- 1 root root 6 Feb 6 16:37 123.txt<br/>-rw-r--r-- 1 root root 0 Feb 6 16:36 456.txt<br/>shashi@linuxtechi ~}$ cat 123.txt<br/>HelloRemoving Soft Links
Use rm or unlink to delete a symbolic link or its directory.
# rm <soft-link-filename><br># unlink <soft-link-filename> # rm <soft-link-directory><br># unlink <soft-link-directory>Source: 鸠摩智首席音效师
Signed-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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
