ln (在文件之间创建链接,将文件名与文件数据相关联)

rose1 发表于 2020-09-22 09:09
浏览次数:
在手机上阅读

在类似Unix的操作系统上,ln命令在文件之间创建链接,将文件名与文件数据相关联。 本文档介绍ln的GNU / Linux版本。

查看英文版

目录

1 ln 运行系统环境

2 ln 说明

3 什么是link?

4 ln 语法

5 ln 例子

ln 运行系统环境

Linux

ln 说明

ln创建指向名称为LINKNAME的文件TARGET的链接。如果省略LINKNAME,则在当前目录中创建指向TARGET的链接,并使用TARGET的名称作为LINKNAME。

ln默认创建硬链接,如果指定-s(-- symbolic)选项,则创建符号链接。创建硬链接时,每个TARGET必须存在。

ln creates a link to file TARGET with the name LINKNAME. If LINKNAME is omitted, a link to TARGET is created in the current directory, using the name of TARGET as the LINKNAME.

ln creates hard links by default, or symbolic links if the -s (--symbolic) option is specified. When creating hard links, each TARGET must exist.

查看英文版

查看中文版

什么是link?

在讨论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中引用该文件。如果重命名文件,则文件的内容不会更改;仅指向它的信息。文件名和文件数据是两个单独的实体。

这是文件名和数据的图解,以帮助您形象化:

file1.txt's link and data components

使用链接命令

什么是链接命令的作用是让我们手动创建一个链接到已存在的文件数据。因此,让我们使用link创建我们自己的指向最近创建的文件数据的链接。本质上,我们将为已经存在的数据创建另一个文件名。

我们将其称为新链接file2.txt。我们如何创建它?

link命令的一般形式是:“ link file_name linkname ”。我们的第一个参数是我们要链接到其数据的文件的名称;第二个参数是我们正在创建的新链接的名称。

link file1.txt file2.txt

现在,file1.txtfile2.txt都指向磁盘上的相同数据:

cat file1.txt
This is a file.
cat file2.txt
This is a file.

要意识到的重要一点是我们没有复制这些数据。这两个文件名都指向磁盘上相同的数据字节。这是帮助您形象化的例证:

file1.txt and file2.txt both linked to the same data

如果我们更改其中一个文件指向的数据的内容,则另一个文件的内容也将更改。让我们使用>>运算符向其中之一添加一行:

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。与我们的硬链接示例相反,以下示例可帮助您可视化我们的符号链接:

file2.txt symlinked to 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的文件。

让我们创建一个指向名为doxdocuments的符号链接。这个命令可以解决问题:

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:

file1.txt's link and data components

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:

file1.txt and file2.txt both linked to the same data

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:

file2.txt symlinked to file1.txt

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.

查看英文版

查看中文版

ln 语法

ln [OPTION]... TARGET [...] [LINKNAME [...]]

选件

这是可以传递给ln命令的选项。

--backup[=CONTROL] 使用此选项可以额外创建每个现有目标文件的备份。备份的样式可选地由CONTROL的值定义。有关更多信息,请参见下文。
-b 该功能类似于--backup,但是您不能指定CONTROL;使用默认样式(simple)。
-d-F--directory 该选项允许超级用户尝试硬链接目录(尽管由于系统限制,即使超级用户也可能失败)。
-f--force 如果目标文件已存在,请覆盖它们。
-i--interactive 在覆盖目标文件之前提示用户。
-L--logical 取消引用作为符号链接的TARGET。换句话说,如果您尝试创建到符号链接的链接(或符号链接),请链接到其链接的对象,而不是符号链接本身。
-n--no-dereference 治疗LINKNAME作为一个正常的文件,如果它是一个符号链接到一个目录。
-P--physical 将硬链接直接链接到符号链接,而不是取消引用。
-r--relative 创建相对于链接位置的符号链接。
-s--symbolic 进行符号链接而不是硬链接。
-S--suffix=SUFFIX 使用文件后缀SUFFIX而不是默认后缀“ 〜 ”。
-t--target-directory=DIRECTORY 指定要在其中创建链接的目录。
-T--no-target-directory 始终将LINKNAME视为普通文件。
-v--verbose 冗长地操作; 打印每个链接文件的名称。
--help 显示帮助消息,然后退出。
--version 显示版本信息,然后退出。

--backup选项

使用--backup(或-b)选项时,备份的默认文件后缀为' 〜 '。但是,您可以使用--suffix选项或设置SIMPLE_BACKUP_SUFFIX 环境变量来更改此设置。

--backup选项的CONTROL参数指定版本控制方法。或者,可以通过设置VERSION_CONTROL环境变量来指定它。这是用于其中任何一个的值:

noneoff 切勿进行备份(即使给出了--backup)。
numberedt 进行编号备份。
existingnil 如果存在编号的备份,则编号,否则简单。
simplenever 始终进行简单的备份。

如果使用-b而不是--backup,则CONTROL方法始终很简单。

如果指定-s选项(符号链接),则ln忽略-L-P选项。否则(如果您正在建立硬链接),则当TARGET是符号链接时,指定的最后一个选项控制行为。缺省情况是充当-P被指定。

ln [OPTION]... TARGET [...] [LINKNAME [...]]

Options

Here are the options that can be passed to the ln command.

--backup[=CONTROL] Use this option to additionally create a backup of each existing destination file. The style of backup is optionally defined by the value of CONTROL. See below for more information.
-b This functions like --backup, but you cannot specify the CONTROL; the default style (simple) is used.
-d-F--directory This option allows the superuser to attempt to hard link directories (although it will probably fail due to system restrictions, even for the superuser).
-f--force If the destination file or files already exist, overwrite them.
-i--interactive Prompt the user before overwriting destination files.
-L--logical Dereference TARGETs that are symbolic links. In other words, if you are trying to create a link (or a symlink) to a symlink, link to what it links to, not to the symlink itself.
-n--no-dereference Treat LINKNAME as a normal file if it is a symbolic link to a directory.
-P--physical Make hard links directly to symbolic links, rather than dereferencing them.
-r--relative Create symbolic links relative to link location.
-s--symbolic Make symbolic links instead of hard links.
-S--suffix=SUFFIX Use the file suffix SUFFIX rather than the default suffix "~".
-t--target-directory=DIRECTORY Specify the DIRECTORY in which to create the links.
-T--no-target-directory Always treat LINKNAME as a normal file.
-v--verbose Operate verbosely; print the name of each linked file.
--help Display a help message, and exit.
--version Display version information, and exit.

The --backup option

When using the --backup (or -b) option, the default file suffix for backups is '~'. You can change this, however, using the --suffix option or setting the SIMPLE_BACKUP_SUFFIX environment variable.

The CONTROL argument to the --backup option specifies the version control method. Alternatively, it can be specified by setting the VERSION_CONTROL environment variable. Here are the values to use for either one:

noneoff Never make backups (even if --backup is given).
numberedt Make numbered backups.
existingnil Numbered if numbered backups exist, simple otherwise.
simplenever Always make simple backups.

If you use -b instead of --backup, the CONTROL method is always simple.

If you specify the -s option (which symlinks), ln ignores the -L and -P options. Otherwise (if you are making hard links), the last option specified controls behavior when a TARGET is a symbolic link. The default is to act as if -P was specified.

查看英文版

查看中文版

ln 例子

ln public_html/myfile1.txt

在当前目录中创建指向文件public_html / myfile1.txt的硬链接。

ln -s public_html/myfile1.txt

在当前目录中创建指向文件public_html / myfile1.txt的符号链接。

ln -s public_html/ webstuff

创建一个符号链接到名为webstuff的目录public_html。

ln -s -b file1.txt file2.txt

创建指向文件file1.txt的符号链接,名为file2.txt。如果file2.txt已经存在,则在创建新的file2.txt符号链接之前将其重命名为file2.txt〜。

ln public_html/myfile1.txt

Create a hard link to the file public_html/myfile1.txt in the current directory.

ln -s public_html/myfile1.txt

Create a symbolic link to the file public_html/myfile1.txt in the current directory.

ln -s public_html/ webstuff

Create a symbolic link to the directory public_html named webstuff.

ln -s -b file1.txt file2.txt

Creates a symbolic link to the file file1.txt named file2.txt. If file2.txt already exists, it is renamed to file2.txt~ before the new file2.txt symlink is created.

查看英文版

查看中文版

其他命令行

link | ldd | last | login | ls | lsmod | lpstat | losetup | lprm | lpr | lpq | lp | lpc | locate | logname | lpadmin |

如此好文,分享给朋友
发表评论
验证码:
评论列表
共0条