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

Is there any difference between soft links and hard links in linux

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you whether there is a difference between soft links and hard links in linux. I hope you will get something after reading this article. Let's discuss it together.

Linux soft links and hard links are different, the difference is: 1, soft links in the form of a path, hard links exist in the form of a copy of the file; 2, soft links can cross the file system, hard links can not; 3, soft links can be linked to the directory, hard links can not.

The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.

Ln is another very important command in linux. Its function is to establish a synchronous link for a file in another location. When we need to use the same file in a different directory, we don't need to put the same file in every directory we need, we just put the file in a fixed directory, and then link it with the ln command (link) in another directory, without repeatedly occupying disk space.

1. Command format:

Ln [parameter] [source file or directory] [target file or directory]

2. Command function:

Linux file system, there are so-called link, we can regard it as an alias for the file, and links can be divided into two types: hard link (hard link) and soft link (symbolic link), hard link means that an file can have multiple names, while the soft link way is to generate a special file, the content of the file is to point to the location of another file. Hard links exist in the same file system, while soft links can span different file systems.

Soft links:

Soft links exist in the form of paths. Similar to shortcuts in the Windows operating system

Soft links can cross file systems, but hard links cannot

A soft link can link to a file name that does not exist.

Soft links can link directories

Hard links:

Hard links, in the form of a copy of the file. But it doesn't take up real space.

Hard links to directories are not allowed

Hard links can only be created in the same file system

Here are two points to pay attention to:

First, the ln command keeps the synchronization of every linked file, that is, no matter where you change, the other files will change the same.

Second, ln links are divided into soft links and hard links, soft links are ln-s source file target files, it will only generate an image of a file in the location you choose, will not take up disk space, hard link ln source file target files, no parameter-s, it will generate a file of the same size as the source file in your selected location, whether soft link or hard link The files are kept synchronized.

The ln instruction is used to link files or directories, just as if you specify more than two files or directories, and the final destination is an existing directory, all previously specified files or directories will be copied to that directory. If you specify more than one file or directory at the same time, and the final destination is not an existing directory, an error message appears.

3. Command parameters:

Required parameters:

-b delete, overwrite previously established links

-d allows superusers to make hard links to directories

-f Enforcement

-I interactive mode, the presence of a file prompts the user whether to overwrite

-n treat symbolic links as general directories

-s soft links (symbolic links)

-v shows the detailed process

Select parameters:

-S "- S" or "--suffix="

-V "- V" or "--version-control="

-- help displays help information

-- version displays version information

4. Use an example:

Example 1: create a soft link to a file

To explain in more detail the relative path problem of creating soft links, let's first create a few deep-seated folders

Mkdir-p / app/a {1.. 3} / b {c.. f}

The general hierarchy can be seen that both the app directory and the etc directory are under the root directory.

Suppose we give app/a1/bc in the root directory a soft link to the location / magedu/ZD/2g/login.bat in the root directory, then the soft link should be written like this

Ln-s.. / magedu/ZD/2g/login.bat / app/a1/bc/softlink1

Many students will be confused about how to write the relative path of the target path. The relative path to the file or folder should be based on the folder where the soft link is located, and the relative file can show the system his path.

In this example, the directory where the soft link is located is the app/a1/bc under the root directory, then the bc folder is the base point folder. Return to A1 from here, the syntax is.. /, and then one level up to the app folder. /, because magedu is under the root directory, you need to return one layer to the root directory, that is, three.. /, the root directory knows how to express The next step is to sequentially paste the absolute path address to the source file. Multi-tier directory will be established, so the shallow directory is needless to say.

How to check whether there is a problem with the link you have established, go to the soft link directory ls to see if the file with the name of the soft link is light blue.

After reading this article, I believe you have a certain understanding of "is there any difference between soft links and hard links in linux". If you want to know more about it, welcome to follow the industry information channel, thank you for your reading!

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