Home > Operation and Maintenance > Linux Operation and Maintenance > Is there a difference between soft links and hard links in Linux?

Is there a difference between soft links and hard links in Linux?

青灯夜游
Release: 2021-11-22 18:13:17
Original
18105 people have browsed it

There is a difference between soft links and hard links in Linux. The differences are: 1. Soft links exist in the form of paths, and hard links exist in the form of file copies; 2. Soft links can cross file systems, but hard links cannot. ;3. Soft links can link to directories, but hard links cannot.

Is there a difference between soft links and hard links in Linux?

#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 synchronized link for a certain file in another location. When we need to use the same file in different directories, we There is no need to put a file that must be the same in every required directory. We only need to put the file in a fixed directory, and then use the ln command to link it in other directories. There is no need to repeat it. of disk space occupied.

1. Command format:

 ln [参数][源文件或目录][目标文件或目录]
Copy after login

2. Command function:

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

Soft link:

  • Soft link exists in the form of a path. Similar to shortcuts in the Windows operating system

  • Soft links can cross file systems, but hard links cannot

  • Soft links can cross a different file system Link existing file names

  • Soft links can link directories

Hard links:

  • Hard links exist in the form of file copies. But it doesn't take up actual space.

  • It is not allowed to create hard links to directories

  • Hard links can only be created in the same file system

There are two points to note here:

First, the ln command will maintain the synchronization of each linked file, that is to say, no matter where you change, other files will also change. Same change;

Second, ln links are divided into soft links and hard links. Soft links are ln -s source file target files, which will only generate a file at the location you selected. Mirror, will not occupy disk space, hard link ln source file target file, without parameter -s, it will generate a file with the same size as the source file at the location you selected, whether it is a soft link or a hard link, the file All keep changing in sync.

The ln command is used to link files or directories. If two or more files or directories are specified at the same time, and the final destination is an existing directory, all previously specified files or directories will be copied to in this directory. If multiple files or directories are specified at the same time, and the final destination is not an existing directory, an error message will appear.

3. Command parameters:

Required parameters:

-b Delete and overwrite previously established links

-d Allow super users to create hard links to directories

-f Forces execution of

-i interactive mode, prompts the user whether to overwrite the file if it exists

-n Treats symbolic links as normal directories

-s Soft link (symbolic link)

-v Display detailed processing process

Select parameters:

-S “-S” or “ --suffix=

-V “-V” or “--version-control=

--help Display help information

--version Display version information

4. Usage example:

Example 1: Create a soft link to a file

In order to explain the relative path problem of creating a soft link in more detail, we first create a few in-depth points Folder

mkdir -p /app/a{1..3}/b{c..f}
Copy after login

You can look at the approximate hierarchical structure. The app directory and etc directory are both under the root directory

Assume we give the root directory Download app/a1/bc and make a soft link pointing to the location of /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
Copy after login

Many students will be confused about this target path How to write the relative path to a file or folder, the relative path to a file or folder should be based on the folder where the soft link is located, and how to indicate the path to the system relative to this file.

In this example, the directory where the soft link is located is app/a1/bc in the root directory. Then this bc folder is the base folder. From here, go up one level and return to a1. The syntax is ../, and then go up one level. Go to the app folder and add another ../. Because magedu is in the root directory, you need to return to the root directory one level, which is three ../. The root directory knows how to express it. The next step is to paste the points to the source files sequentially. The absolute path address. If you know how to create multi-layer directories, then there is no need to talk about shallow directories.

How to check whether there is any problem with the link you created, just go to the soft link directory and check ls to see if the file with the soft link name is light blue.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of Is there a difference between soft links and hard links in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template