Unix 简明教程
Linux - File Links
What are File Links in Linux?
Linux 链接提供了一种机制,可以为现有的文件或目录创建一个快捷方式或备用名称,这意味着您可以为 Linux 中的同一文件或目录创建多个名称,从而根据您的要求非常轻松地访问和使用它们。
Linux links provide a mechanism to create a shortcut or alternative name for an existing file or directory, which means you can create multiple names for the same file or directory in Linux to make their access and usage very easy based on your requirements.
Linux/Unix 提供两种类型的链接:
Linux/Unix provides two types of links:
-
Soft links or Symbolic Links
-
Hard Links
Symbolic Links
Linux 中的符号链接也称为软链接或符号链接。这是 Linux 中的一个特殊文件,指向另一个文件或目录。此原始文件或目录可以在同一文件系统或不同文件系统上可用。符号链接类似于 Windows 中的快捷方式,其中包含原始文件而不是内容的路径。
A symbolic in Linux is also called Soft Link or Symlink. This is a special file in Linux which points to another file or a directory. This original file or directory could be available on either the same file system or different file system. Symlink is like a shortcut in Windows which contains the path of the original file and not the contents.
您可以使用此文件上的任何可用符号链接访问原始文件数据,并且移除符号链接不会影响目标文件。但是,如果您删除原始文件,则此文件上的所有符号链接将会中断,您的数据将不再可访问。
You can access original file data using any of the available Symbolic link on this file and removing the symlink does not affect the target file. But If you delete the original file, then all the Symbolic links on this file will be broken and your data will not be accessible any more.
How to Create a Symbolic Link
以下是在现有文件上创建符号链接的语法:
Following is the syntax to create a symbolic link on an existing file:
$ ln -s file/directory symlink
此处文件或目录可以是完整的文件路径或目录,符号链接将是符号链接的名称,该名称将指向该文件或目录。
Here file or directory can be a full file path or directory and symlink will be the name of symbolic link which will point to the file or directory.
首先,让我们按以下方式创建一个简单文件 file.txt :
To start with, lets create a simple file file.txt as follows:
$ echo 'Hello, World!' > file.txt
现在,我们可以使用以下命令在此文件上创建一个软链接:
Now we can create a soft link on this file using the following command:
$ ln -s file.txt soft-link
$ ls -l
total 4
-rw-r--r-- 1 root root 14 May 1 20:11 file.txt
lrwxrwxrwx 1 root root 8 May 1 20:11 soft-link -> file.txt
在此处,请注意权限字符串 lrwxrwxrwx 的第一个字符,它 是 l ,这意味着此文件是符号链接。现在,您可以使用 soft-link 访问文件 file.txt 的内容并更新其内容,该内容将在原始 file.txt 中反映出来。
Here notice the first character of permission string lrwxrwxrwx, it is l which means this file is a symbolic link. Now you can access the content of file file.txt using soft-link and update it’s content which will reflect in original file.txt.
How to remove a Symbolic Link
您可以使用 unlink 或 rm 命令来移除现有的符号链接。只要您正在移除符号链接,它将不会影响原始文件,但如果您删除原始文件,它将会创建该文件上可用的中断符号链接。
You can use either unlink or rm command to remove an existing symbolic link. As long as your are removing symbolic link, its not going to impact the original file but if you delete the original file, then it will create broken symbolic links available on that file.
以下命令删除我们先前部分建立的软链接:
Following command will delete soft-link which we had create in previous section:
$ unlink soft-link
$
Finding Broken Symbolic Links
你可以使用以下查找命令,找出所有已中断了的符号链接:
You can use the following find command to find all the broken symbolic links:
$ find /path/to/directory -xtype l
此命令将列出 /path/to/directory 目录中的所有已中断了的符号链接。你可以使用 -delete 选项删除找到的已中断链接。
This will list all broken symlinks in the /path/to/directory directory. You can use -delete option to delete the found broken links
$ find /path/to/directory -xtype l -delete
Hard Links
硬链接也是文件的快捷方式,但无法为不同文件系统上的文件或文件夹创建硬链接。
Hard links are also shortcuts to files but a hard link cannot be created for a folder or file available on a different file system.
硬链接是原始文件的一个镜像副本。删除原始文件不会产生任何影响,因为硬链接文件会作为原始文件的镜像副本。
Hard link is a mirror copy of the original file. Deleting the original file will not impact anything, because the hard link file, will act as a mirror copy of the original file.
How to Create a Hard Link
以下为在现有文件中创建硬链接的语法:
Following is the syntax to create a hard link on an existing file:
$ ln filepath hardlink
此处 filepath 可以是原始文件的完整路径,而 hardlink 将是硬链接的名称,指向该文件。我们使用以下命令在 file.txt 文件上创建一个硬链接:
Here filepath can be a full file path of the original file and hardlink will be the name of hard link which will point to the file. Let’s create a hard link on file.txt file using the following command:
$ ln file.txt hard-link
$ ls -li
total 8
72744984 -rw-r--r-- 2 root root 14 May 1 20:11 file.txt
72744984 -rw-r--r-- 2 root root 14 May 1 20:11 hard-link
请注意,inode 编号 72744984 对于这两个文件都相同。权限字符串之后的数字 2 表示这两个文件的副本。如果你将为该文件 file.txt 创建另一个硬链接,则此数字将变成 3。
Here notice the inode number 72744984 which is same for both the files. Number after permission string is 2 which means, there are two copies of the same file. If you will create one more hard link on this file file.txt, then this number will become 3.
现在,你可以使用 hard-link 访问文件 file.txt 的内容并更新其内容,这将反映在 file.txt 中。
Now you can access the content of file file.txt using hard-link and update its content which will reflect in file.txt.
How to remove a Hard Link
你可以使用 unlink 或 rm 命令来删除现有的硬链接。删除硬链接或原始链接不会影响其他文件,因为硬链接会创建原始文件的镜像副本。
You can use either unlink or rm command to remove an existing hard link. Removing hard link or original link will not impact another file because hard link creates a mirror copy of the original file.
以下命令删除我们先前部分建立的软链接:
Following command will delete soft-link which we had create in previous section:
$ unlink hard-link
$ ls -li
total 4
72744984 -rw-r--r-- 1 root root 14 May 1 20:11 file.txt
Symbolic Links Vs Hard Links
Linux 文件系统的两种类型为硬链接和软链接。这两种链接类型之间的差异很大,我们在这里列出:
The two types of Linux filesystem links are hard and soft. The difference between these two types of links is significant which we have listed down here:
-
Symbolic links can be created on directories as well as files where as hard links can be created only on files and not on directories.
-
Symbolic links can be created on files or directories even if they are available on difference file systems where as hard links can be created on the same file system.
-
Symbolic links don’t inherit the original permissions from the original file. That means any permission changes made in the original file aren’t reflected in the symbolic link. Files that are hard-linked together share the same inode number. When changes are made to one file, the other reflects those changes and the permissions, link count, ownership, timestamps. Though file content remain the exact same across all the linked files in both the cases.
-
Symbolic links are very small in size because they are just pointer to the original content and not being a mirror of the original file, its size is only the number of bytes necessary to compose the name of the file or directory. Where as hard links consume more space because they create a mirror copy of the original file.