ln (Create Link to File and Directory)
The ln
(LiNk) command is used to create links to files or directories. There are two types of links in Linux – symbolic link and hard link.
Symbolic Link
The symbolic link is an alias on Mac OS or a shortcut on Windows OS. It is linked with the original file or directory. Thus, when you delete the original file or directory, the symbolic link no longer works.
Creating a symbolic link
To create a symbolic link, use the ln
command with the -s option
. To see an example, create file_a first using the cat command.
cat > file_a
test test test
After quitting the typing mode with Ctrl + C and running the ln
command to create a symbolic link to file_a.
ln -s file_a file_symbolic
When you run the ls -l
command, you can see file details. The first letter l
in the second row means that it is a symbolic link and you can see that file_symbolic is linked with file_a under the file name column.
ls -l
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan 9 06:47 file_a
lrwxrwxrwx 1 ubuntu ubuntu 6 Jan 9 11:37 file_symbolic -> file_a
Hard Link
The concept of the hard link is a bit more complex. In the Linux file system, the actual data of each file is stored in a certain location on the hard disk. The location is described in the inode of each file. When you create a hard link of file_a, the link is connected to the inode of file_a directly. Thus, even if you delete file_a, still the link can exist unless all the hard links that are linked with the inode are deleted and no processes are using the data.
Creating a hard link
To create a hard link, use the ln
command without any option. To create file_a's hard link named file_hard in the same directory, run the command below.
ln file_a file_hard
When you run the ls -l
command, you can see file details. This time, there is no letter “l”. Instead, the number after the access mode changed from 1 to 2. This shows that there are two hard links to the file: file_a and file_hard.
ls -l
total 0
-rw-rw-r-- 2 ubuntu ubuntu 0 Jan 9 06:47 file_a
-rw-rw-r-- 2 ubuntu ubuntu 0 Jan 9 06:47 file_hard
lrwxrwxrwx 1 ubuntu ubuntu 6 Jan 9 11:37 file_symbolic -> file_a
You cannot create hard links to directories
To avoid the circular reference issue, you cannot create a hard link to directories. If you try to make it, you'll encounter an error message.
ln dir_a dir_b
ln: dir_a: hard link not allowed for directory
Deleting the original file
When you delete the original file, you may see the difference between symbolic links and hard links. When you delete file_a, file_symbolic and file_hard are still shown in the current directory; however, when you check the contents of each file by running the cat command, you can see that file_symbolic no longer exists while file_hard shows the original contents of file_a.
rm file_a
ls
file_hard file_symbolic
cat file_symbolic
cat: file_symbolic: No such file or directory
cat file_hard
test test test
Note: inode and inode number
In the Linux file system, the actual contents of each file are stored at a certain disk block location on the hard disk. inode, or Index NODE, is a unique identifier used to keep track of all the files’ and directories' data locations. Each inode is managed by an inode number. When you make a hard link, it records the inode number of the original file.