I wanted to know the difference between hard links and symbolic links, so I looked it up.
The environment is here.
# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
A hard link is one of the functions of a file system, in which a file or directory entity is given multiple names so that each of them functions equally as an actual file name / directory name. Creating a hard link will have the same file. The difference from a symbolic link is that a symbolic link creates a file with a path leading to the specified file, but a hard link has the same data as the specified file itself.
A file with the specified file path is created. Therefore, it is newly created as a file.
You can create a hard link using the ln command. ln Original file hard link name If you create a hard link, you can see that the original file and the hard link have the same inode number (leftmost number). Here, it is 8409199. Since the inode number is the actual state of the file, it looks like there are two files, but in reality it can be said that it is one file.
# echo "hogehoge2" > test
# ln test hardlink
# ls -li
8409199 -rw-r--r--2 root root 10 June 24 21:13 hardlink
8409199 -rw-r--r--2 root root 10 June 24 21:13 test
Next, let's create a symbolic link. You can create a symbolic link with ln -s. From the results below, you can see that the files with different inode numbers were created. It can be considered that a file has been created to refer to the specified file.
# ls -il test symblink hardlink
8409199 -rw-r--r--2 root root 10 June 24 21:13 hardlink
8409209 lrwxrwxrwx 1 root root 5 June 24 21:53 symblink -> test
8409199 -rw-r--r--2 root root 10 June 24 21:13 test
By the way, if you look at the contents of each file, you can see that they are all the same. Because they all refer to the same file!
# cat test
hogehoge2
# cat hardlink
hogehoge2
# cat symblink
hogehoge2
Even if you change the contents of the symbolic link file, all the files will be changed. Because they all refer to the same file! !!
# echo "add hoge" >> symblink
# cat test
hogehoge2
add hoge
# cat hardlink
hogehoge2
add hoge
# cat symblink
hogehoge2
add hoge
Hard links are assigned unique inode numbers within the file system, and hard links cannot be created on different file systems. Let's see the behavior.
First, try creating a file.
# touch fileA
# ls -i fileA
8409208 fileA
Check the file system with the df command. Since I was working in my home directory, the filesystem with fileA has a mount position of /. This time, let's see the behavior when a hard link is created under / dev / shm.
# df
Filesys 1K-Block can be used can be used%Mount position
devtmpfs 495352 0 495352 0% /dev
tmpfs 507412 0 507412 0% /dev/shm
tmpfs 507412 6876 500536 2% /run
tmpfs 507412 0 507412 0% /sys/fs/cgroup
/dev/mapper/centos-root 6486016 1198776 5287240 19% /
/dev/sda1 1038336 139372 898964 14% /boot
tmpfs 101484 0 101484 0% /run/user/0
Creating a hard link causes an error. It turns out that this makes it impossible to create hard links on different filesystems. In the case of symbolic links, you can create links on different file systems. However, the link is broken.
# ln fileA /dev/shm/fileA-2
ln: `/dev/shm/fileA-2'From`fileA'Failed to create a hard link to:Invalid cross-device link
# ln -s fileA /dev/shm/fileA-2
# cat /dev/shm/fileA-2
cat: /dev/shm/fileA-2:Too many layers of symbolic links
# file /dev/shm/fileA-2
/dev/shm/fileA-2: broken symbolic link to `fileA'
Create three hard links for fileA and move one of them to a different file system.
# ln fileA fileA-2
# ln fileA-2 fileA-3
# ls -li fileA*
8409208 -rw-r--r--3 root root 0 June 24 21:35 fileA
8409208 -rw-r--r--3 root root 0 June 24 21:35 fileA-2
8409208 -rw-r--r--3 root root 0 June 24 21:35 fileA-3
Of these, try moving file-3 under / dev / shm. From the result of ls -l, we can see that the number of hard links has decreased after moving. Before moving) 8409208 -rw-r--r-- 3 root root 0 June 24 21:35 fileA-2 ↑ This is 3, and it is stated that there are three hard links. After moving) 8409208 -rw-r--r-- 2 root root 0 June 24 21:35 fileA-2 file-1 and file-2 have 2 hardware numbers. 18911 -rw-r--r-- 1 root root 0 June 24 21:35 / dev / shm / fileA-3 There is one for file-3.
# mv fileA-3 /dev/shm/
# ls -li fileA*
8409208 -rw-r--r--2 root root 0 June 24 21:35 fileA
8409208 -rw-r--r--2 root root 0 June 24 21:35 fileA-2
# ls -li /dev/shm/fileA-3
18911 -rw-r--r--1 root root 0 June 24 21:35 /dev/shm/fileA-3
When I edit the contents of the file, the changes are not reflected in the file (fileA-3) moved to a different file system.
# echo "hello" > fileA
# cat fileA
hello
# cat fileA-2
hello
# cat /dev/shm/fileA-3
((Nothing is displayed !!))
From this, it was found that when the hard link is moved by the mv command, it is deleted from the original file system and a new file is created in the specified file system.
This time, I investigated hard links and symbolic links and checked their behavior. There are many things I still don't understand the basics, but I will study one by one.
reference) https://qiita.com/lnznt/items/6178e1c5f066f22fe9c2#%E7%95%B0%E3%81%AA%E3%82%8B%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0%E3%81%B8%E3%81%AE-mv
Recommended Posts