Inodes are associated with precisely one directory entry at a time. However, with hard links, it is possible to associate multiple directory entries with a single inode. Each file on Linux or Unix comes with an inode containing metadata about the file. To create a hard link use the ln command as follows:
ls /path/to/source /path/to/link
ln old_dir new_link_dir
ls [options] /path/to/source /path/to/link
## create a hard link ##
ln /home/vivek/my-awesome-long-file.txt /home/vivek/file
## create a symbolic link ##
ln -s /etc/resolv.conf /tmp/test
ls -l /tmp/test /home/vivek/file
Above commands create a link to my-awesome-long-file.txt and /etc/resolv.conf as /home/vivek/file and /tmp/test respectively.
What is a Symbolic or soft link in Linux / Unix?
Symbolic links refer to:
A symbolic path indicating the abstract location of another file. It is like a shortcut in Microsoft Windows operating system.
What is a hard link in Linux or Unix?
Hard links refer to:
The specific location of physical data. It an essentially a label or name assigned to a file.
Hard link vs. Soft link in Linux or UNIX
- Hard links cannot link directories.
- Cannot cross file system boundaries.
Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:
- To create links between directories.
- Can cross file system boundaries.
These links behave differently when the source of the link is moved or removed.
- Symbolic links are not updated.
- Hard links always refer to the source, even if moved or removed.
You can create symbolic link with ln command:
ln -s source_file my_link
ln -s /path/to/file1.txt /path/to/file2.txt
ls -s /etc/hosts /tmp/file
ls -ali /tmp/file /etc/hosts
Above command will create a symbolic link to /etc/hosts as /tmp/file.
Symbolic link creation and deletion
Let us create a directory called foo using mkdir command
$ mkdir foo
$ cd foo
Copy /etc/resolv.conf file using the cp command, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
total 152 1048600 drwxr-xr-x 2 vivek vivek 4096 2008-12-09 20:19 . 1015809 drwxrwxrwt 220 root root 143360 2008-12-09 20:19 .. 1048601 -rwxr-xr-x 1 vivek vivek 129 2008-12-09 20:19 resolv.conf
Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali
total 152 1048600 drwxr-xr-x 2 vivek vivek 4096 2008-12-09 20:24 . 1015809 drwxrwxrwt 220 root root 143360 2008-12-09 20:19 .. 1048602 lrwxrwxrwx 1 vivek vivek 11 2008-12-09 20:24 alink.conf -> resolv.conf 1048601 -rwxr-xr-x 1 vivek vivek 129 2008-12-09 20:19 resolv.conf
The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali
If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.
To create a symbolic link in Linux and Unix, at the shell prompt, enter:
ln -s source new_soft_link
ls -ali source new_soft_link
And to create a hard link in Linux and Unix, at the shell prompt, enter:
ln source new_hard_link
ls -ali source new_hard_link
- Understanding UNIX / Linux File System
- Understanding UNIX / Linux filesystem directories
- Understanding the Linux file system directories / hierarchy
- Understanding UNIX / Linux filesystem Superblock
- Understanding filesystem Inodes
- What is a hard and symbolic (soft) link in Linux or Unix?
- Why isn't it possible to create hard links across file system boundaries?
- Surviving a Linux Filesystem Failures
- Linux / UNIX Delete or Remove Files With Inode Number
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
|Category||List of Unix and Linux commands|
|Firewall||Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04|
|Network Utilities||dig • host • ip • nmap|
|OpenVPN||CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04|
|Package Manager||apk • apt|
|Processes Management||bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time|
|Searching||grep • whereis • which|
|User Information||groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w|
|WireGuard VPN||Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04|