I investigated the behavior of the difference between hard links and symbolic links

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)

What is a hard link?

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.

What is a symbolic link?

A file with the specified file path is created. Therefore, it is newly created as a file.

I tried to create a hard link

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

I tried to create a symbolic link

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

I tried to create hard links and symbolic links on different file systems!

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'

Then try moving the created hard link to a different file system

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.

At the end

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

I investigated the behavior of the difference between hard links and symbolic links
Differences between symbolic links and hard links
Features of symbolic and hard links
What is the difference between a symbolic link and a hard link?
Hard links and symbolic links
Consideration of the difference between ROC curve and PR curve
I investigated the mechanism of flask-login!
I investigated the relationship between Keras stateful LSTM and hidden state
What is the difference between `pip` and` conda`?
About the behavior of copy, deepcopy and numpy.copy
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
About the difference between "==" and "is" in python
I read and implemented the Variants of UKR
Bayesian modeling-estimation of the difference between the two groups-
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?
The behavior of @property is different between class definition and old and new styles
The rough difference between Unicode and UTF-8 (and their friends)
I had Spotify analyze the difference between the Beatles song and my own song and plotted it
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
Difference between Ruby and Python in terms of variables
What is the difference between usleep, nanosleep and clock_nanosleep?
Indent behavior of json.dumps is different between python2 and python3
I checked out the versions of Blender and Python
I examined the data mapping between ArangoDB and Java
I checked the default OS and shell of docker-machine
Visualization of the connection between malware and the callback server
I investigated the calculation time of "X in list" (linear search / binary search) and "X in set"
How to use argparse and the difference between optparse
I investigated the reinforcement learning algorithm of algorithmic trading
Python module num2words Difference in behavior between English and Russian
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
A rough summary of the differences between Windows and Linux
I tried a little bit of the behavior of the zip function
I want to know the features of Python and pip
I tried to enumerate the differences between java and python
The difference between foreground and background processes understood by the principle
I displayed the chat of YouTube Live and tried playing
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Difference between np.array and np.arange
Difference between MicroPython and CPython
Difference between ps a and ps -a
Difference between return and print-Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I don't really understand the difference between modules, packages and libraries, so I tried to organize them.
[Introduction to Python] I compared the naming conventions of C # and Python.
[Python] I thoroughly explained the theory and implementation of logistic regression
[Python] I thoroughly explained the theory and implementation of decision trees
I investigated the X-means method that automatically estimates the number of clusters
I summarized how to change the boot parameters of GRUB and GRUB2
Python> Extract (unpack) the value of list> Add *> You taught me the difference between Python 2 and Python 3 regarding print (* mylist) / print ().
I tried hard to understand Spectral Normalization and singular value decomposition, which contribute to the stability of GAN.
I tried to take the difference of Config before and after work with pyATS / Genie self-made script
Difference between Ruby and Python split
Difference between java and python (memo)
The story of Python and the story of NaN
Difference between list () and [] in Python
Difference between SQLAlchemy filter () and filter_by ()
Difference between == and is in python