Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What do soft links and hard links mean in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what is the meaning of soft links and hard links in Linux", the content is simple and clear, hoping to help you solve your doubts, the following let the editor lead you to study and learn what soft links and hard links mean in Linux "this article.

Brief introduction

Our modern operating system needs to prevent the loss of information caused by a program crash and needs to store the information in a file. And the file can be read by multiple processes at the same time. All the resources and peripherals in Linux are abstracted into files, so there is a "everything is a file" feature in Linux. Of course, it is certainly not enough to have documents. We cannot manage all the documents together. It is really too messy and difficult to manage and maintain. Linux introduced the concept of directory, which can be called a folder in Windows. The introduction of directories makes Linux's root file system look like a hierarchical directory tree. As shown below:

What is the catalogue?

In Linux, "everything is a file", so this question is easy to answer: directories are also files. What is recorded in the directory is the file name under the directory. Since it is a file, if you create a directory with the same name as the file or directory in the current directory, the creation will definitely fail.

Well, now that we've figured out the concept of the catalog, let's move on.

The difference between hard links and soft links?

The way a file is represented in Linux is represented by two parts: user data (user data) and metadata (meta data). What is user data? User data is the data that records the contents of the real file, which is stored in data block blocks of the same size.

What about metadata? In fact, it is the additional information attributes of this file, such as: creation time, file size, and so on. We use index numbers (inode) to represent the metadata for each different file (of course, inode is also part of the metadata). In Linux, inode is the identity of the file, not the file name. The following figure shows how to find the contents of a file by its name. Note: the file name is not included in the metadata. The file name is just for the user to identify and remember.

What command can I use to view the inode number?

You can view it using the stat and ls-I commands, as shown in the following figure:

What is a hard link?

Hard links refer to connections through index nodes. That is, there are multiple file names pointing to the same inode. In this way, you can establish hard links to important files to prevent "accidental deletion".

Command:

Link oldfile newfile

You can create hard links. The hard-linked inode is the same, but the file name is different, so it has some features:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

The file has the same inode and data blocks

Cannot create a hard link to a file that does not exist

Cannot be created across file systems (because inode is * under their respective file systems, inode duplication occurs when they cross file systems)

Directories cannot be created, only files can be created

If you delete a hard link file, it will not affect the other inode files (there is a link counter in inode, deleting a hard link is equivalent to minus one counter, and vice versa. Delete inode until 0)

For example:

# ls-li total 0 / / hard connections can only be created for existing files # link test.file test_hard.link link: cannot create link `test_hard.link' to `test.file': No such file or directory # echo "This is an original file" > test.file # cat test.file This is an original file # stat test.file File: `test.file' Size: 25 Blocks: 8 IO Block: 4096 regular file Device: 807h/2055d Inode: 660650 Links: 2 Access: (0644) Uid: (0 / root) Gid: (0 / root). / / the file has the same inode number and data block # link test.file test_hard.link | ls-li total 8 660650-rw-r--r-- 2 root root 25 Sep 1 17:44 test_hard.link 660650- Rw-r--r-- 2 root root 25 Sep 1 17:44 test.file / / cannot cross file system # ln / dev/input/event5 / root/bfile.txt ln: failed to create test_hardlink `/ root/bfile.txt' = >` / dev/input/event5': Invalid cross-device link / / cannot create hard connection to directory # mkdir-p test.dir/test # ln test.dir/test _ hardlink.dir Ln: `test.dir/': test_hard link not allowed for directory # ls-iF 660650 test_hard.link 657948 test.dir/ 660650 test.file

For a specific explanation, you can refer to the 5-point features of hard links.

What is a soft link?

A soft connection is completely different from a hard link, which is the point to which the path name of another file is recorded in the user data (data blocks). It can be understood that the soft link is actually an ordinary file, but its content is very special. So SoftConnect has its own inode number and data blocks. Then let me summarize the characteristics of soft connections:

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Soft connections have their own file properties

Can be created for files that do not exist

Soft links can span file systems

Soft links can be created for directories

Soft link creation does not cause link counters to increase, because it is not the same inode

If the linked file is deleted, the link is meaningless, but it can also be recreated.

The following figure shows the access process of the soft link:

For example:

# ls-li total 0 / / soft links can be created for files that do not exist # ln-s test.file test_soft.link # ls-liF total 0 789467 lrwxrwxrwx 1 root root 8 Sep 1 18:00 test_soft.link-> test.file / / because the directed file does not exist The soft link test_soft.link at this time is the dead link # cat test_soft.link cat: test_soft.link: No such file or directory / / create the file test.file to be pointed to Test_soft.link reverts to the normal soft link # echo "This is an original file_A" > > test.file # cat test_soft.link This is an original file_A / / A pair of directories that do not exist create a soft link # ln-s test.dir test_soft.link.dir # mkdir-p test.dir/test # tree. -F-- inodes. ├── [789497] test.dir/ │ └── [789498] test/ ├── [789495] test.file ├── [789495] test_soft.link-> test.file └── [789497] test_soft.link.dir-> test.dir/ all the contents of this article "what are soft connections and hard links in Linux"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report