Principle and Usage Analysis of Linux Hard Link and Soft Link

  • 2021-08-12 04:01:16
  • OfStack

In the linux system, a kind of file is a linked file, which can be used to solve the problem of file sharing. There are two ways to link, one is hard link (Hard Link), and the other is soft link or symbolic link (Symbolic Link).

Hard link concept

A hard link (hard link, also known as a link) is one or more filenames of a file

Hard linking refers to linking through index nodes. In the Linux file system, whatever type of file is stored in the disk partition, it is assigned a number, which is called the index node number (Inode)

Index) or Inode, which is the only identification of a file or directory in a file system. The actual data of the file is placed in the data area (data block), which stores the important parameter information of the file, that is, metadata (metadata), such as creation time, modification time, file size, owner, user group, read-write authority, block number where the data is located, etc.

After the hard link is established, the source file and the link file are synchronized, and any file modified will be modified

Establishing links can save space, only need to maintain the link relationship, and do not need to copy files

Soft link concept

Soft link (also called symbolic link) is similar to the shortcut in windows system. Different from hard link, soft link is an ordinary file, but the content of data block is a bit special. The content stored in the data block of file user is pointed by the pathname of another file. In this way, the source file entity pointed by soft link can be quickly located. Soft links can be created for files or directories.

Soft link function:

It is convenient for file management, such as linking files under a complex path to a simple path for users to access. Save space to solve the problem of insufficient space. A file system has run out of space, but now it is necessary to create a new directory under the file system and store a large number of files. Then you can link the directory in another file system with more remaining space to the file system. Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, the related soft link becomes a dead link.

The essential difference between hard link and soft link

Hard link can be considered as one file with two file names; Soft link is a new link file, which points to the file it wants to refer to

Limitations of hard links

Soft links can cross file systems; Hard links are not allowed Soft link can link a file that does not exist; Hard links are not allowed Soft links can connect to directories, but hard links cannot Soft links overcome the limitations of hard links. Based on this, focus on soft links. Soft links are also called symbolic links, which are equivalent to shortcuts in windows.

Establish soft links

ln -s src_file ln_file

Delete Soft Link

rm ln_file

Note: For catalogue soft training level,

rm ln_dir is to delete the soft link

rm ln_dir/ is to delete the files in the directory ln_dir, of course, the files in the source directory will also be deleted (synchronous)


Related articles: