Linux没有快捷方式,但它有符号链接——(Links –)或者通常所说的符号链接。(Symlinks)但符号链接到底是什么?如何创建一个?让我们来看看吧。
我们将在Ubuntu 安装(an Ubuntu installation)上尝试这些命令,尽管语法在所有 Linux 发行版(all Linux distros)上都相同。有趣的是,符号链接也可以在 Mac PC 上创建(symlinks can also be created on a Mac PC)。

什么是符号链接?
如果您希望能够从多个位置访问文件,那么简单地复制它并不是一个好的解决方案。您正在浪费磁盘空间,更不用说由于存在多个文件而难以跟踪更改。
在基于 GUI 的操作系统中,您可以通过创建快捷方式来克服这个问题。对于基于 CUI 的操作系统(如任何 Linux 发行版(any Linux distro)),答案是符号链接。在某些情况(some situations)下,甚至Microsoft Windows也可以利用符号链接。
符号链接只是指向另一个文件或目录的文件 - 即使在完全不同的文件系统或分区中也是如此。打开符号链接可以访问相同的原始文件,任何更改也会反映在主文件中。
这也适用于权限 - 使用chmod更改符号链接文件的权限实际上会修改目标文件的权限。
符号链接的类型
符号链接也称为软链接。顾名思义,它并不是Linux上唯一的文件链接系统类型。在某些情况下,硬(Hard)链接可以用作符号链接的替代品,但有许多限制。

首先,硬链接只是指向同一inode 的(inode)文件名。虽然这使得它们在存储和访问速度方面非常高效,但也将它们限制为同一文件系统或分区中的文件。由于Linux 中文件夹访问工作方式(how folder access works in Linux)的一个怪癖,您也不能对目录使用硬链接。
另一方面,软链接或符号链接更像桌面快捷方式。每个软链接本质上是一个指向原始数据的小文件,但实际上并不直接链接到它。这意味着软链接也可以轻松地指向其他分区和文件系统上的文件和目录,没有任何限制。
硬链接 VS 软链接 –(Links VS Soft Links – Which Type)使用哪种类型?
软链接是最通用的链接形式,因为它们可以不受任何限制地用于跨多个文件系统的文件和目录。但为什么我们还要有硬链接呢?与符号链接相比,它们有什么优势吗?
嗯,从理论上讲,硬链接效率更高。由于它们使用相同的 inode,因此不会占用额外的磁盘空间,并且访问速度比软链接快得多。这就是为什么许多系统管理员会建议尽可能使用硬链接。
然而,话虽如此,使用符号链接的缺点是最小的。现代计算机尤其不真正关心几千字节的额外存储,除非您用数百万个软链接填充系统,否则额外的文件大小很少重要。
创建符号链接
符号链接(或者更确切地说,一般的文件链接)是使用 ln 命令创建的。默认情况下,该命令创建硬链接。要创建符号链接,您需要将 -s 标志附加到 ln 命令。
语法很简单。ln 命令有两个参数——要链接的文件(或文件夹)的路径,以及要创建的链接文件的路径。
像这样:
ln -s user_names.txt 名称

这将创建一个名为名称的符号链接,连接到 user_names 文本文件。要查看符号链接是否实际创建,只需使用 ls 命令。

正如您所看到的,Ubuntu对不同类别的文件显示不同的颜色,目录用深蓝色表示,符号链接用浅蓝色(实际上是青色)表示。
我们也可以使用相同的语法为文件夹创建符号链接:
ln -s 示例 example_folder

要创建到其他目录中的文件和文件夹的符号链接,只需使用它们的完整路径即可。例如:
ln -s 示例/example3.txt 远程示例

符号链接的限制
符号链接是跨系统连接文件的好方法,而无需制作副本,但它们有其自身的局限性。是的,尽管软链接的限制比硬链接少,但仍然有一些事情需要记住。
首先,ln 命令实际上并不验证创建的链接。这意味着可以输入实际不存在的文件名,并且不会出现任何错误。确认链接的唯一方法是尝试使用它并查看它是否指向正确的文件。
其次,虽然访问或更改符号链接的权限会更改原始文件的属性,但文件删除却不会。您可以安全地删除符号链接(remove the symbolic link),而不会影响链接的文件或目录。
在 Linux 中创建符号链接
使用终端创建符号链接的最大痛苦是难以访问分布在各个目录中的重要文件。聪明的解决方案是从您的主目录本身创建指向此类文件的符号链接,例如桌面快捷方式。
与硬链接不同,符号链接可以轻松创建,没有任何限制,甚至可以连接到不同分区和文件系统中的目录或文件。请记住,这也意味着符号链接可能指向不存在的文件 - 您必须尝试访问该链接以验证该文件是否存在。
How to Create Symbolic Links (Symlinks) in Linux
Linux doesn’t have shortcuts, but it has Symbolic Links – or Symlinks, as they are usually called. But what exactly are symbolic links? And how do you create one? Let’s find out.
We will be trying out the commands on an Ubuntu installation, though the syntax works the same on all Linux distros. Interestingly enough, symlinks can also be created on a Mac PC.

What Are Symbolic Links?
If you want to be able to access a file from multiple locations, simply copying it over is not a good solution. You are wasting disk space, not to mention making it difficult to track changes due to there being multiple files.
In a GUI-based operating system, you would get over this by creating shortcuts. For a CUI-based OS (like any Linux distro), the answer is a symlink. Even Microsoft Windows can leverage symlinks in some situations.
A symlink or a symbolic link is just a file pointing to another file or directory – even in a completely different file system or partition. Opening the symlink gives you access to the same original file, with any changes reflected in the main file as well.
This also goes for permissions – using chmod to change the permissions of a symlink file will actually modify the permissions of the target file.
Types of Symlinks
Symbolic links are also known as soft links. As the name might suggest, it isn’t the only type of file-linking system on Linux. Hard links can be used as an alternative to symlinks in certain scenarios, though comes with a number of restrictions.

To begin with, hard links are simply file names pointing to the same inode. While this makes them very efficient in terms of storage and access speed, it also limits them to files in the same file system or partition. And due to a quirk of how folder access works in Linux, you cannot use hard links for directories either.
Soft links or Symbolic links, on the other hand, are more like desktop shortcuts. Each soft link is essentially a small file that points to the original data, without actually being directly linked to it. This means soft links can easily point to files and directories on other partitions and file systems as well without any restrictions.
Hard Links VS Soft Links – Which Type to Use?
Soft links are the most versatile form of links since they can be used on both files and directories across multiple file systems without any restrictions. But why then do we even have hard links? Do they afford you any advantages over symlinks?
Well, on paper, hard links are more efficient. Because they use the same inodes, they do not take up additional disk space and can be accessed much faster than soft links. This is why many system administrators will recommend using hard links whenever you can.
That being said, however, the cons of using symbolic links are minimal. Modern computers especially don’t really care about a few kilobytes of extra storage, and unless you are filling the system with millions of soft links, the additional file size rarely matters.
Creating Symbolic Links
Symbolic links – or rather, file links in general – are created using the ln command. By default, the command creates hard links. To create a symbolic link, you need to append the -s flag to the ln command.
The syntax is simple enough. The ln command takes two parameters – the path of the file (or folder) to be linked to, and the path of the link file to be created.
Like this:
ln -s user_names.txt names

This will create a symlink called names connected to the user_names text file. To see if the symbolic link was actually created, just use the ls command.

As you can see, Ubuntu shows different colors for different categories of files, with directories denoted by dark blue and symlinks with a lighter blue color (cyan, actually).
We can use the same syntax to create a symlink for the folder as well:
ln -s examples example_folder

For creating symlinks to files and folders in other directories, simply use their full path. For example:
ln -s examples/example3.txt distant_example

Limitations of Symlinks
Symbolic links are a great way of connecting files across your system without having to resort to making copies, but they have their own limitations. Yes, even though soft links are less restrictive than hard links, there are still some things to keep in mind.
First, the ln command doesn’t actually verify the links created. This means it is possible to enter a filename that doesn’t actually exist, and you will not get any errors. The only way to confirm the link is to try using it and see if it leads to the correct file.
Second, while accessing or changing the permissions of the symlink changes the properties of the original file, the same doesn’t go for file deletion. You can safely remove the symbolic link without affecting the linked file or directory.
Creating Symlinks in Linux
The biggest pain in using the terminal to create symbolic links is the difficulty in accessing important files spread out across various directories. The smart solution is to create symbolic links to such files from your home directory itself, like desktop shortcuts.
And unlike hard links, symlinks can be made easily without any restrictions, even for connecting to directories or files in different partitions and file systems. Keep in mind that this also means that a symlink might point to a non-existent file – you will have to try accessing the link to verify if the file exists.