在讨论ln命令之前,让我们首先讨论link命令,以及链接是什么以及它与我们所知道的文件之间的关系。
一个链接在你的项目文件系统其中一个连接文件名实际字节的数据的上盘。一个以上的文件名可以“链接”到相同的数据。这是一个例子。让我们创建一个名为file1.txt的文件:
echo "This is a file." > file1.txt
该命令回显字符串 “ This is a file ”。通常,这会回显到我们的终端,但是>运算符会将字符串的文本重定向到文件,在这种情况下为file1.txt。我们可以使用cat显示文件内容来检查它是否起作用:
cat file1.txt
This is a file.
创建此文件后,操作系统将字节写入磁盘上的某个位置,还将该数据链接到文件名file1.txt,以便我们可以在command和arguments中引用该文件。如果重命名文件,则文件的内容不会更改;仅指向它的信息。文件名和文件数据是两个单独的实体。
这是文件名和数据的图解,以帮助您形象化:
使用链接命令
什么是链接命令的作用是让我们手动创建一个链接到已存在的文件数据。因此,让我们使用link创建我们自己的指向最近创建的文件数据的链接。本质上,我们将为已经存在的数据创建另一个文件名。
我们将其称为新链接file2.txt。我们如何创建它?
link命令的一般形式是:“ link file_name linkname ”。我们的第一个参数是我们要链接到其数据的文件的名称;第二个参数是我们正在创建的新链接的名称。
link file1.txt file2.txt
现在,file1.txt和file2.txt都指向磁盘上的相同数据:
cat file1.txt
This is a file.
cat file2.txt
This is a file.
要意识到的重要一点是我们没有复制这些数据。这两个文件名都指向磁盘上相同的数据字节。这是帮助您形象化的例证:
如果我们更改其中一个文件指向的数据的内容,则另一个文件的内容也将更改。让我们使用>>运算符向其中之一添加一行:
echo "It points to data on the disk." >> file1.txt
现在让我们看一下file1.txt的内容:
cat file1.txt
This is a file.
It points to data on the disk.
...,现在让我们看第二个文件,我们使用链接命令创建的文件:
cat file2.txt
This is a file.
It points to data on the disk.
这两个文件都显示更改,因为它们共享磁盘上的相同数据。对这些文件之一的数据进行更改将更改另一个文件的内容。
但是,如果我们删除其中一个文件怎么办?两个文件都将被删除吗?
否。如果我们删除其中一个文件,则会删除到数据的链接之一。因为我们手动创建了另一个链接,所以仍然有一个指向该数据的指针。我们仍然有一种在用户级别上访问我们放入其中的数据的方法。因此,如果我们使用rm命令删除第一个文件:
rm file1.txt
...它不再作为具有该名称的文件存在:
cat file1.txt
cat: file1.txt: No such file or directory
...但是指向我们手动创建的数据的链接仍然存在,并且仍然指向该数据:
cat file2.txt
This is a file.
It points to data on the disk.
如您所见,即使在删除“文件”(实际上是数据的链接)之后,数据仍保留在磁盘上。只要有链接,我们仍然可以访问该数据。了解何时删除文件非常重要-“删除”文件通过取消链接使数据不可访问。数据仍然存在于系统无法访问的某个位置的存储介质上,并且磁盘上的该空间被标记为可供将来使用。
我们在这里使用的链接类型有时称为“硬”链接。硬链接及其链接到的数据必须始终存在于同一文件系统上。例如,您不能在一个分区上创建到存储在另一分区上的文件数据的硬链接。您也无法创建指向目录的硬链接。只有符号链接可以链接到目录。我们稍后会解决。
ln和link之间的区别
那么ln呢?那就是为什么我们在这里,对吗?
ln默认情况下会像link一样创建一个硬链接。因此,此ln命令:
ln file1.txt file2.txt
...与以下link命令相同:
link file1.txt file2.txt
...因为这两个命令都创建了一个名为file2.txt的硬链接,该链接链接到file1.txt的数据。
但是,我们也可以使用ln通过-s选项创建符号链接。所以命令:
ln -s file1.txt file2.txt
创建一个名为file2.txt的符号链接到file1.txt。与我们的硬链接示例相反,以下示例可帮助您可视化我们的符号链接:
什么是符号链接?
符号链接有时也称为“软”链接,与“硬”链接不同。它们没有链接到文件的数据,而是链接到另一个link。因此,在上面的示例中,file2.txt指向链接file1.txt,而链接又指向文件的数据。
这有几个潜在的好处。一方面,符号链接(简称“符号链接”)可以链接到目录。同样,符号链接可以跨越文件系统边界,因此到一个驱动器或分区上的数据的符号链接可以存在于另一驱动器或分区上。
您还应该意识到,与硬链接不同,删除符号链接指向的文件(或目录)会破坏链接。因此,如果我们创建file1.txt:
echo "This is a file." > file1.txt
...并创建指向它的符号链接:
ln -s file1.txt file2.txt
...我们能猫其中任一一个看到的内容:
cat file1.txt
This is a file.
cat file2.txt
This is a file.
...但是如果我们删除file1.txt:
rm file1.txt
...我们无法再通过符号链接访问其包含的数据:
cat file2.txt
cat: file2.txt: No such file or directory
首先,此错误消息可能会造成混淆,因为file2.txt仍存在于您的目录中。但是,这是一个断开的符号链接-指向不再存在的符号链接。操作系统尝试遵循符号链接到应该存在的文件(file1.txt),但是什么也没找到,因此它返回错误消息。
虽然硬链接是操作系统工作方式的重要组成部分,但符号链接通常更方便。您可以使用它们以任何方式引用磁盘上其他地方已经存在的信息。
创建目录的符号链接
要创建指向目录的符号链接,请指定目录名称作为目标。例如,假设我们有一个名为documents的目录,其中包含一个名为file.txt的文件。
让我们创建一个指向名为dox的documents的符号链接。这个命令可以解决问题:
ln -s documents/ dox
现在,我们有了一个名为dox的符号链接,可以将其称为目录documents。例如,如果我们使用ls列出目录的内容,然后列出符号链接目录的内容,则它们都将显示相同的文件:
ls documents
file.txt
ls dox
file.txt
现在,当我们在目录dox中工作时,实际上将在document中工作,但是在所有路径名中都将看到单词dox而不是document。
符号链接是用于快捷方式生成长而复杂的路径名的有用方法。例如,此命令:
ln -s documents/work/budgets/Engineering/2014/April aprbudge
...将为我们节省很多打字时间;现在,而不是使用以下命令更改目录:
cd documents/work/budgets/Engineering/2014/April
...我们可以这样做,而不是:
cd aprbudge
通常,使用rmdir命令删除目录(一旦目录为空)。但是我们的符号链接实际上不是目录:它是指向目录的文件。因此,要删除符号链接,我们使用rm命令:
rm aprbudge
这将删除符号链接,但是原始目录及其所有文件均不受影响。
Before we discuss the ln command, let's first discuss the link command, as well as what a link is and how it relates to files as we know them.
A link is an entry in your file system which connects a file name to the actual bytes of data on the disk. More than one file name can "link" to the same data. Here's an example. Let's create a file named file1.txt:
echo "This is a file." > file1.txt
This command echoes the string "This is a file". Normally this would echo to our terminal, but the > operator redirects the string's text to a file, in this case file1.txt. We can check that it worked by using cat to display the contents of the file:
cat file1.txt
This is a file.
When this file was created, the operating system wrote the bytes to a location on the disk and also linked that data to a file name, file1.txt so that we can refer to the file in commands and arguments. If you rename the file, the contents of the file are not altered; only the information that points to it. The file name and the file's data are two separate entities.
Here's an illustration of the file name and the data to help you visualize it:
Using the link command
What the link command does is allow us to manually create a link to file data that already exists. So, let's use link to create our own link to the file data recently created. In essence, we'll create another file name for the data that already exists.
Let's call our new link file2.txt. How do we create it?
The general form of the link command is: "link file_name linkname". Our first argument is the name of the file whose data we're linking to; the second argument is the name of the new link we're creating.
link file1.txt file2.txt
Now both file1.txt and file2.txt point to the same data on the disk:
cat file1.txt
This is a file.
cat file2.txt
This is a file.
The important thing to realize is that we did not make a copy of this data. Both file names point to the same bytes of data on the disk. Here's an illustration to help you visualize it:
If we change the contents of the data pointed to by either one of these files, the other file's contents are changed as well. Let's append a line to one of them using the >> operator:
echo "It points to data on the disk." >> file1.txt
Now let's look at the contents of file1.txt:
cat file1.txt
This is a file.
It points to data on the disk.
... and now let's look at the second file, the one we created with the link command:
cat file2.txt
This is a file.
It points to data on the disk.
Both files show the change because they share the same data on the disk. Changes to the data of either one of these files will change the contents of the other.
But what if we delete one of the files? Will both files be deleted?
No. If we delete one of the files, we're deleting one of the links to the data. Because we created another link manually, we still have a pointer to that data; we still have a way, at the user-level, to access the data we put in there. So if we use the rm command to remove our first file:
rm file1.txt
...it no longer exists as a file with that name:
cat file1.txt
cat: file1.txt: No such file or directory
...but the link to the data we manually created still exists, and still points to the data:
cat file2.txt
This is a file.
It points to data on the disk.
As you can see, the data stays on the disk even after the "file" (which is actually a link to the data) is removed. We can still access that data as long as there is a link to it. This is important to know when you're removing files — "removing" a file makes the data inaccessible by unlink-ing it. The data still exists on the storage media, somewhere, inaccessible to the system, and that space on disk is marked as being available for future use.
The type of link we've been working with here is sometimes called a "hard" link. A hard link and the data it links to must always exist on the same filesystem; you can't, for instance, create a hard link on one partition to file data stored on another partition. You also can't create a hard link to a directory. Only symbolic links may link to a directory; we'll get to that in a moment.
The difference between ln and link
So what about ln? That's why we're here, right?
ln, by default, creates a hard link like link does. So this ln command:
ln file1.txt file2.txt
...is the same as the following link command:
link file1.txt file2.txt
...because both commands create a hard link named file2.txt which links to the data of file1.txt.
However, we can also use ln to create symbolic links with the -s option. So the command:
ln -s file1.txt file2.txt
Create a symbolic link to file1.txt named file2.txt. In contrast to our hard link example, here's an illustration to help you visualize our symbolic link:
What are symbolic links?
Symbolic links, sometimes called "soft" links, are different than "hard" links. Instead of linking to the data of a file, they link to another link. So in the example above, file2.txt points to the link file1.txt, which in turn points to the data of the file.
This has several potential benefits. For one thing, symbolic links (also called "symlinks" for short) can link to directories. Also, symbolic links can cross file system boundaries, so a symbolic link to data on one drive or partition can exist on another drive or partition.
You should also be aware that, unlike hard links, removing the file (or directory) that a symlink points to will break the link. So if we create file1.txt:
echo "This is a file." > file1.txt
...and create a symbolic link to it:
ln -s file1.txt file2.txt
...we can cat either one of these to see the contents:
cat file1.txt
This is a file.
cat file2.txt
This is a file.
...but if we remove file1.txt:
rm file1.txt
...we can no longer access the data it contained with our symlink:
cat file2.txt
cat: file2.txt: No such file or directory
This error message might be confusing at first, because file2.txt still exists in your directory. It's a broken symlink, however — a symbolic link which points to something that no longer exists. The operating system tries to follow the symlink to the file that's supposed to be there (file1.txt), but finds nothing, and so it returns the error message.
While hard links are an essential component of how the operating system works, symbolic links are generally more of a convenience. You can use them to refer, in any way you'd like, to information already on the disk somewhere else.
Creating symlinks to directories
To create a symbolic link to a directory, specify the directory name as the target. For instance, let's say we have a directory named documents, which contains one file, named file.txt.
Let's create a symbolic link to documents named dox. This command will do the trick:
ln -s documents/ dox
We now have a symlink named dox which we can refer to as if it is the directory documents. For instance, if we use ls to list the contents of the directory, and then to list the contents of the symlinked directory, they will both show the same file:
ls documents
file.txt
ls dox
file.txt
When we work in the directory dox now, we will actually be working in documents, but we will see the word dox instead of documents in all pathnames.
Symbolic links are a useful way to make shortcuts to long, complicated pathnames. For instance, this command:
ln -s documents/work/budgets/Engineering/2014/April aprbudge
...will save us a lot of typing; now, instead of changing directory with the following command:
cd documents/work/budgets/Engineering/2014/April
...we can do this, instead:
cd aprbudge
Normally, you remove directories (once they're empty) with the rmdircommand. But our symbolic link is not actually a directory: it's a file that points to a directory. So to remove our symlink, we use the rm command:
rm aprbudge
This will remove the symlink, but the original directory and all its files are not affected.