[LINUX] What is the difference between a symbolic link and a hard link?

Do you know the difference between symbolic links and hard links and how to use them properly? I didn't know much about it, so I searched for related articles.

This article was intuitively easy to understand as a figure. Each feature is also written in an easy-to-understand manner. https://eng-entrance.com/linux-permission-link

Symbolic link

Isn't it easy to understand the image of symbolic links as shortcuts? Consider creating a symbolic link symkink_fname that references a file called ʻoriginal_fname`.

./original_fname ./symlink_fname

here

cat ./symlink_fname

When you run

cat ./original_fname

Should mean the same operation as. In other words

rm ./symlink_fname

Is


rm ./original_fname

Will be equivalent to, and the original file ʻoriginal_fname` will be deleted.

Regarding this, the precautions when deleting symbolic links are described in this article, for example. https://mimirswell.ggnet.co.jp/blog-165 The point is that you can delete the link with the rm command, but it's dangerous, so let's use ʻunlink`.

Hard link

On the other hand, what about hard links?

./original_fname ./hardlink_fname

Each path will point to the same entity file. In this state


open ./hardkink_fname

When you execute

open <Entity file>

It means. What's interesting is that there's something like a reference counter to a real file,

rm ./hardkink_fname

Will delete the path ./hardkink_fname, but not the actual file.

Because, originally, the situation I'm thinking about now ./original_fname ./hardlink_fname Because there were two "references" in, even if one "reference" is deleted, one still remains.

And when the number of "references" becomes 0, the entity seems to be deleted.

inode and entity file

Until now, we have used the words "entity" and "reference" to grasp the image, but to explain it properly, we need to touch the inode.

Is this article easy to understand? https://qiita.com/katsuo5/items/fc57eaa9330d318ee342

How to use hard links

It seems that symbolic links are commonly used. So when are hard links used?

Read the answer to this question, I see! I think. https://q.hatena.ne.jp/1265361495

It is said that the destination is used to refer to the mail of multiple people on the IMAP server. It means that you don't have to copy it and it will be deleted properly.

...I learned a lot!

Recommended Posts

What is the difference between a symbolic link and a hard link?
What is the difference between `pip` and` conda`?
What is the difference between Unix and Linux?
[Introduction to Python] What is the difference between a list and a tuple?
What is the difference between usleep, nanosleep and clock_nanosleep?
I investigated the behavior of the difference between hard links and symbolic links
About the difference between "==" and "is" in python
Difference between ps a and ps -a
Difference between == and is in python
Differences between symbolic links and hard links
[Introduction to Infectious Disease Models] What is the difference between the April epidemic and this epidemic? .. .. ‼
[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)
The answer of "1/2" is different between python2 and 3
It's a Mac. What is the Linux command Linux?
About the difference between PostgreSQL su and sudo
Is there a contradiction between the party that protects the people from NHK and the party that protects NHK from the people?
What is a distribution?
What is a terminal?
What is a hacker?
What is a pointer?
Hard links and symbolic links
Consideration of the difference between ROC curve and PR curve
The rough difference between Unicode and UTF-8 (and their friends)
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
How to use argparse and the difference between optparse
What is a recommend engine? Summary of the types
Difference between process and job
Add "/" at the end when completing a directory symbolic link
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
What is the activation function?
What is the Linux kernel?
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
Difference between regression and classification
What is a decision tree?
What is a Context Switch?
[Python] Python and security-① What is Python?
What is a super user?
A rough summary of the differences between Windows and Linux
What is a system call
Difference between SQLAlchemy back_populates and backref and when neither is used
Difference between np.array and np.arange
[Definition] What is a framework?
The difference between foreground and background processes understood by the principle
Difference between MicroPython and CPython
What is the interface for ...
What is a callback function?
What is the Callback function?
The image is a slug
What is a python map?
Difference between return and print-Python
What to do if you cat or tail a binary file and the terminal is garbled
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
How to give and what the constraints option in scipy.optimize.minimize is
What is a dog? Volume of GET request and query parameters
[Ln] How to paste a symbolic link in a directory is complicated
[Free study] Is there a connection between Wikipedia updates and trends?
What is a dog? Django--Get Name and Date from URL Volume
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
Difference between Ruby and Python split
[Python] What is a zip function?
[Python] What is a with statement?