符号链接symlink
A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows.
符号链接(也称为符号链接)是Linux中一种文件类型,它指向计算机上的另一个文件或文件夹。 符号链接类似于Windows中的快捷方式。
Some people call symlinks "soft links" – a type of link in Linux/UNIX systems – as opposed to "hard links."
有人称符号链接为“软链接”(Linux / UNIX系统中的一种链接),而不是“硬链接”。
软链接和硬链接之间的区别 (Difference Between a Soft Link and a Hard Link)
Soft links are similar to shortcuts, and can point to another file or directory in any file system.
软链接类似于快捷方式,并且可以指向任何文件系统中的另一个文件或目录。
Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.
硬链接也是文件和文件夹的快捷方式,但是无法为其他文件系统中的文件夹或文件创建硬链接。
Let's look at the steps involved in creating and removing a symlink. We'll also see what broken links are, and how to delete them.
让我们看看创建和删除符号链接所涉及的步骤。 我们还将看到什么是断开的链接,以及如何删除它们。
如何创建符号链接 (How to Create a Symlink)
The syntax for creating a symlink is:
创建符号链接的语法为:
ln -s <path to the file/folder to be linked> <the path of the link to be created>
ln
is the link command. The -s
flag specifies that the link should be soft. -s
can also be entered as -symbolic
.
ln
是链接命令。 -s
标志指定链接应为软链接。 -s
也可以作为-symbolic
输入。
By default, ln
command creates hard links. The next argument is path to the file (or folder)
that you want to link. (That is, the file or folder you want to create a shortcut for.)
默认情况下, ln
命令创建硬链接。 下一个参数是您要链接path to the file (or folder)
。 (即,您要为其创建快捷方式的文件或文件夹。)
And the last argument is the path to link
itself (the shortcut).
最后一个参数是path to link
自身的path to link
(快捷方式)。
如何为文件创建符号链接–示例命令 (How to Create a Symlink for a File – Example Command)
ln -s /home/james/transactions.txt trans.txt
After running this command, you will be able to access the /home/james/transactions.txt
with trans.txt
. Any modification to trans.txt
will also be reflected in the original file.
运行此命令后,您将能够使用/home/james/transactions.txt
访问trans.txt
。 对trans.txt
任何修改也将反映在原始文件中。
Note that this command above would create the link file trans.txt
in your current directory. You can as well create a linked file in a folder link this:
请注意,上面的命令将在当前目录中创建链接文件trans.txt
。 您也可以在以下文件夹链接中创建链接文件:
ln -s /home/james/transactions.txt my-stuffs/trans.txt
There must be a directory already called "my-stuffs" in your current directory – if not the command will throw an error.
当前目录中必须已经有一个名为“ my-stuffs”的目录-如果没有,该命令将引发错误。
如何为文件夹创建符号链接–示例命令 (How to Create a Symlink for a Folder – Example Command)
Similar to above, we'd use:
与上述类似,我们将使用:
ln -s /home/james james
This would create a symlinked folder called 'james' which would contain the contents of /home/james
. Any changes to this linked folder will also affect the original folder.
这将创建一个名为'james'的符号链接文件夹,其中包含/home/james
的内容。 对此链接文件夹的任何更改也会影响原始文件夹。
如何删除符号链接 (How to remove a symlink)
Before you'd want to remove a symlink, you may want to confirm that a file or folder is a symlink, so that you do not tamper with your files.
在删除符号链接之前,您可能需要确认文件或文件夹是符号链接,以免篡改文件。
One way to do this is:
一种方法是:
ls -l <path-to-assumed-symlink>
Running this command on your terminal will display the properties of the file. In the result, if the first character is a small letter L ('l'), it means the file/folder is a symlink.
在终端上运行此命令将显示文件的属性。 结果,如果第一个字符是小写字母L('l'),则表示文件/文件夹是符号链接。
You'd also see an arrow (->) at the end indicating the file/folder the simlink is pointing to.
您还将在末尾看到一个箭头(->),指示simlink指向的文件/文件夹。
There are two methods to remove a symlink:
有两种方法可以删除符号链接:
如何使用取消链接删除符号链接 (How to Use Unlink to Remove a Symlink)
The syntax is:
语法为:
unlink <path-to-symlink>
This deletes the symlink if the process is successful.
如果过程成功,这将删除符号链接。
Even if the symlink is in the form of a folder, do not append '/', because Linux will assume it's a directory and unlink
cannot delete directories.
即使符号链接是文件夹形式,也不要附加'/',因为Linux将假定它是目录,并且unlink
无法删除目录。
如何使用rm删除符号链接 (How to use rm to Remove a Symlink )
As we've seen, a symlink is just another file or folder pointing to an original file or folder. To remove that relationship, you can remove the linked file.
如我们所见,符号链接只是指向原始文件或文件夹的另一个文件或文件夹。 要删除该关系,可以删除链接的文件。
Hence, the syntax is:
因此,语法为:
rm <path-to-symlink>
For example:
例如:
rm trans.txt
rm james
Note that trying to do rm james/
would result an error, because Linux will assume 'james/' is a directory, which would require other options like r
and f
. But that's not what we want. A symlink may be a folder, but we are only concerned with the name.
注意,尝试执行rm james/
会导致错误,因为Linux会假设'james /'是目录,这将需要其他选项,例如r
和f
。 但这不是我们想要的。 一个符号链接可能是一个文件夹,但是我们只关心名称。
The main benefit of rm
over unlink
is that you can remove multiple symlinks at once, like you can with files.
rm
优于unlink
的主要好处是,您可以一次删除多个符号链接,就像使用文件一样。
如何查找和删除断开的链接 (How to Find and Delete Broken Links)
Broken links occur when the file or folder that a symlink points to changes path or is deleted.
当符号链接指向的文件或文件夹更改路径或被删除时,链接断开。
For example, if 'transactions.txt' moves from /home/james
to /home/james/personal
, the 'trans.txt' link becomes broken. Every attempt to access to the file will result in a 'No such file or directory' error. This is because the link has no contents of its own.
例如,如果'transactions.txt'从/home/james
移到/home/james/personal
,则'trans.txt'链接将断开。 每次尝试访问该文件都将导致“无此文件或目录”错误。 这是因为链接没有自己的内容。
When you discover broken links, you can easily delete the file. An easy way to find broken symlinks is:
发现断开的链接时,可以轻松删除文件。 查找损坏的符号链接的简单方法是:
find /home/james -xtype l
This will list all broken symlinks in the james
directory – from files to directories to sub-directories.
这将列出james
目录中所有损坏的符号链接–从文件到目录再到子目录。
Passing the -delete
option will delete them like so:
传递-delete
选项将删除它们,如下所示:
find /home/james -xtype l -delete
结语 (Wrapping up)
Symbolic link are an interesting feature of Linux and UNIX systems.
符号链接是Linux和UNIX系统的一个有趣功能。
You can create easily accessible symlinks to refer to a file or folder that would otherwise not be convenient to access. With some practice, you will understand how these work on an intuitive level, and they will make you much more efficient at managing file systems.
您可以创建易于访问的符号链接来引用原本不方便访问的文件或文件夹。 通过一些练习,您将了解它们在直观的水平上是如何工作的,它们将使您更加有效地管理文件系统。
翻译自: https://www.freecodecamp.org/news/symlink-tutorial-in-linux-how-to-create-and-remove-a-symbolic-link/
符号链接symlink