65) Linux hard link and soft link

1- Link

A file sharing method is a concept in POSIX, and mainstream file systems all support linking files.

The link is simply understood as a common shortcut in Windows (or an avatar in OS X), which is commonly used in Linux to solve some library version problems, and usually also links some files with a deeper directory level to a more accessible In the directory.

2- Soft link:

  1. Soft links exist in the form of paths. Similar to the shortcut in the Windows operating system
  2. Soft links can cross file systems, hard links cannot
  3. Soft links can link to a file name that does not exist
  4. Soft links can link to directories
  5. The content stored in the file user data block is the path name of another file
  6. Have their own file attributes and permissions;
  7. Can create soft links to non-existing files or directories;
  8. Soft link can cross file system;
  9. Soft links can be created for files or directories;
  10. Deleting a soft link does not affect the file pointed to, but if the original file pointed to is deleted, the related soft link is called a dead link (that is, dangling link. If the path file pointed to is recreated, the dead link can be restored to normal Soft link).

3- Hard links

  1. It exists as a copy of the file. But it does not take up actual space.
  2. Do not allow hard links to the directory
  3. Can only be created in the same file system
  4. Files with the same inode number but different file names have the same inode and data block;
  5. Only existing files can be created;
  6. Cannot cross the file system to create hard links;
  7. The directory cannot be created, only files can be created;
  8. Deleting a hard-linked file does not affect other files with the same inode number.

4- ln command

Its function is to establish a synchronous link for a certain file in another location. When we need to use the same file in different directories, we do not need to put a file that must be the same in each required directory , We only need to put the file in a certain directory, and then link it with the ln command in other directories, without having to repeatedly occupy the disk space.
Command format:
ln [参数][源文件或目录][目标文件或目录]

Command parameters:

必要参数:
-b 删除,覆盖以前建立的链接
-d 允许超级用户制作目录的硬链接
-f 强制执行
-i 交互模式,文件存在则提示用户是否覆盖
-n 把符号链接视为一般目录
-s 软链接(符号链接)
-v 显示详细的处理过程
选择参数:
-S “-S<字尾备份字符串> ”或 “--suffix=<字尾备份字符串>”
-V “-V<备份方式>”或“--version-control=<备份方式>”
--help 显示帮助信息
--version 显示版本信息

note:

  • The ln command will maintain the synchronization of each linked file, that is, no matter which place you change, other files will change the same;
  • Soft link: ln -s [source file] [target file], it will only generate a mirror image of the file at the location you selected, and will not occupy disk space,
  • Hard link: ln [source file] [target file], without the parameter -s, it will generate a file of the same size as the source file at the location you selected.
  • The ln instruction is used to link files or directories. If more than two files or directories are specified at the same time, and the final destination is an existing directory, all the files or directories specified above will be copied to the directory. If multiple files or directories are specified at the same time, and the final destination is a non-existent directory.

5- inode

In Linux, the inode structure exists in the system memory and disk, which can be divided into VFS inode and the inode of the actual file system. VFS inode is an abstraction of the inode in the actual file system, which defines the structure inode and its related operations inode_operations (see kernel source code include / linux / fs.h).

The file has a file name and data, which is divided into two parts on Linux: user data (user data) and metadata (metadata).

  • User data, that is, a file data block (data block), the data block is where the real content of the file is recorded;
  • Metadata: Additional attributes of the file, such as file size, creation time, owner, etc.

In Linux, the inode number in the metadata (inode is part of the file metadata but it does not contain the file name, the inode number is the inode number) is the file's unique identifier rather than the file name. The file name is just to facilitate people's memory and use, the system or program finds the correct file data block by inode number

Guess you like

Origin www.cnblogs.com/lemanlai/p/12678816.html