Hard to connect

A hard link is a link through an index node. In Linux, multiple files pointing to the same index node are allowed, and links like this are hard links. Hard links can only be used to link files in the same file system, but cannot be used to create directories. If you delete the source file corresponding to the hard link, the hard link file still exists and its original content is saved. In this way, the hard link file is prevented from being deleted due to misoperations. Hard links are files with the same inode number but different file names. Therefore, deleting a hard link file does not affect other files with the same inode number.

  • throughln rumenz.txt rumenz123.txtcreate
  • Hard links cannot be created for directories, but only for files.
  • Exists as a copy of a file, but does not take up real space.
  • The file name has the same inode and data block.
  • A hard link can be created only on the same file system. Hard links cannot be created across file systems.
  • Deleting one hard-linked file does not affect other files with the same inode number.
  • Only existing files can be created.

Applicable scenario

Mirroring data files to prevent accidental deletion

Soft connection

Soft links (also called symlinks) are different from hard links. The content stored in the user data block of a file is a point to the path name of another file. A soft link is a normal file, but the data block content is a bit special. Soft links can be created for files or directories. Soft links are mainly used in the following two aspects: One is convenient for management. For example, a file in a complex path can be linked to a simple path for users to access. Another aspect is to solve the problem of insufficient file system disk space. For example, a file system has run out of space, but a new directory must be created under the file system to store a large number of files. In this case, you can link the directory in another file system that has more free space to the file system to solve the space shortage problem. Deleting a soft link does not affect the file being pointed to. However, if the original file being pointed to is deleted, the soft link becomes dead.

  • ln -s rumenz.txt rumenz123.txt
  • Exists as a path to another file.
  • You can create cross-file systems, but hard links cannot.
  • Directories can be linked.
  • Have their own file attributes and permissions, etc.
  • Soft links can be created to files or directories that do not exist.
  • Soft links can be created for files or directories.
  • When a soft link is created, the link count i_nlink is not increased.
  • When a soft link is deleted, it does not affect the file. However, when the original file is deleted, the soft link is referred to as a dead link.

Neither hard link nor soft link will make a copy of the original file and will only take up a very small amount of disk space.

Applicable scenario

  • It facilitates file management and links files from complex paths to simple paths.
  • Solve the problem of insufficient directory space.

How to view a file hardlink file

> ls -il
total 8
1806727 -rw-r--r-- 2 root root 137 May  6 20:14 one.txt
1806727 -rw-r--r-- 2 root root 137 May  6 20:14 tow.txt
Copy the code

The inode number of hardwired files is the same. TXT and tow. TXT are hard links to each other, so their inode numbers are the same.

> find / -inum 1806727
Copy the code

See which files in the entire file system are hardwired to one.txt.

How to view a file soft link

> find -type l -exec ls -l {} \; | grep 'rumenz.txt'Copy the code

Know a soft link file, how to find the real path of the file

// RealPath soft link file > realPath /etc/rc.local /etc/rc.d/rc.localCopy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station