03-19-2023, 10:48 AM
A hard link is another file that points to the same location as the original file. Use the command ln to create a hard link. The first argument is the existing file and the second argument is the new file. As shown in the command output, the file space.txt is linked to space.hard.txt and the link field now shows 2.
[user@linux ~]$ ln space.txt space.hard.txt
[user@linux ~]$
[user@linux ~]$ ls -l space*
-rw-r--r-- 2 user user 239 May 7 18:18 space.hard.txt
-rw-r--r-- 2 user user 239 May 7 18:18 space.txt
[user@linux ~]$
[user@linux ~]$ echo "Testing hard link" >> space.hard.txt
[user@linux ~]$
[user@linux ~]$ ls -l space*
-rw-r--r-- 2 user user 257 May 7 18:19 space.hard.txt
-rw-r--r-- 2 user user 257 May 7 18:19 space.txt
[user@linux ~]$
[user@linux ~]$ rm space.hard.txt
[user@linux ~]$
[user@linux ~]$ more space.txt
Space... The final frontier…
These are the voyages of the Starship Enterprise. Its continuing mission:
- To explore strange new worlds…
- To seek out new life; new civilizations…
- To boldly go where no one has gone before!
Testing hard link
[user@linux ~]$
Both files point to the same location in the file system. If you change one file, the other is changed, as well. The echo command is used to add some text to space.txt. Notice that the file size for both space.txt and space.hard.txt increased to 257 bytes. If you delete the space.hard.txt with the rm command (remove), the space.txt file still exists, as verified with the more space.txt command.
A symbolic link, also called a symlink or soft link, is similar to a hard link in that applying changes to the symbolic link will also change the original file. As shown in the command output below, use the ln command option -s to create a symbolic link.
[user@linux ~]$ echo "Hello World!" > test.txt
[user@linux ~]$
[user@linux ~]$ ln -s test.txt mytest.txt
[user@linux ~]$
[user@linux ~]$ echo "It's a lovely day!" >> mytest.txt
[user@linux ~]$
[user@linux ~]$ more test.txt
Hello World!
It's a lovely day!
[user@linux ~]$
[user@linux ~]$ rm test.txt
[user@linux ~]$
[user@linux ~]$ more mytest.txt
more: stat of mytest.txt failed: No such file or directory
[user@linux ~]$
[user@linux ~]$ ls -l mytest.txt
lrwxrwxrwx 1 user user 8 May 7 20:17 mytest.txt -> test.txt
[user@linux ~]$
Notice that adding a line of text to test.txt also adds the line to mytest.txt. However, unlike a hard link, deleting the original text.txt file means that mytext.txt is now linked to a file that no longer exists, as shown with the more mytest.txt and ls -l mytest.txt commands.
Although symbolic links have a single point of failure (the underlying file), symbolic links have several benefits over hard links:
[user@linux ~]$ ln space.txt space.hard.txt
[user@linux ~]$
[user@linux ~]$ ls -l space*
-rw-r--r-- 2 user user 239 May 7 18:18 space.hard.txt
-rw-r--r-- 2 user user 239 May 7 18:18 space.txt
[user@linux ~]$
[user@linux ~]$ echo "Testing hard link" >> space.hard.txt
[user@linux ~]$
[user@linux ~]$ ls -l space*
-rw-r--r-- 2 user user 257 May 7 18:19 space.hard.txt
-rw-r--r-- 2 user user 257 May 7 18:19 space.txt
[user@linux ~]$
[user@linux ~]$ rm space.hard.txt
[user@linux ~]$
[user@linux ~]$ more space.txt
Space... The final frontier…
These are the voyages of the Starship Enterprise. Its continuing mission:
- To explore strange new worlds…
- To seek out new life; new civilizations…
- To boldly go where no one has gone before!
Testing hard link
[user@linux ~]$
Both files point to the same location in the file system. If you change one file, the other is changed, as well. The echo command is used to add some text to space.txt. Notice that the file size for both space.txt and space.hard.txt increased to 257 bytes. If you delete the space.hard.txt with the rm command (remove), the space.txt file still exists, as verified with the more space.txt command.
A symbolic link, also called a symlink or soft link, is similar to a hard link in that applying changes to the symbolic link will also change the original file. As shown in the command output below, use the ln command option -s to create a symbolic link.
[user@linux ~]$ echo "Hello World!" > test.txt
[user@linux ~]$
[user@linux ~]$ ln -s test.txt mytest.txt
[user@linux ~]$
[user@linux ~]$ echo "It's a lovely day!" >> mytest.txt
[user@linux ~]$
[user@linux ~]$ more test.txt
Hello World!
It's a lovely day!
[user@linux ~]$
[user@linux ~]$ rm test.txt
[user@linux ~]$
[user@linux ~]$ more mytest.txt
more: stat of mytest.txt failed: No such file or directory
[user@linux ~]$
[user@linux ~]$ ls -l mytest.txt
lrwxrwxrwx 1 user user 8 May 7 20:17 mytest.txt -> test.txt
[user@linux ~]$
Notice that adding a line of text to test.txt also adds the line to mytest.txt. However, unlike a hard link, deleting the original text.txt file means that mytext.txt is now linked to a file that no longer exists, as shown with the more mytest.txt and ls -l mytest.txt commands.
Although symbolic links have a single point of failure (the underlying file), symbolic links have several benefits over hard links:
Locating hard links is more difficult. Symbolic links show the location of the original file in the ls -l command, as shown in the last line of output in the previous command output (mytest.txt -> test.txt).
Hard links are limited to the file system in which they are created. Symbolic links can link to a file in another file system.
Hard links cannot link to a directory because the system itself uses hard links to define the hierarchy of the directory structure. However, symbolic links can link to directories.