> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/ln.1.md).

# link.1

`ln` — 链接文件

## 名称

`ln`, `link`

## 概要

`ln [-L | -P | -s [-F]] [-f | -iw] [-hnv] source_file [target_file]`

`ln [-L | -P | -s [-F]] [-f | -iw] [-hnv] source_file ... target_dir`

`link source_file target_file`

## 描述

`ln` 实用程序为 `target_file` 指定的文件名创建一个新的目录项（链接文件）。`target_file` 将以与 `source_file` 相同的文件模式创建。它可用于在多处同时维护一个文件的多个副本，而无需为"副本"消耗存储空间；相反，链接"指向"原始副本。链接有两种类型：硬链接和符号链接。链接如何"指向"一个文件，正是硬链接与符号链接的区别之一。

选项如下：

**`-F`** 如果目标文件已存在且是一个目录，则将其移除以便进行链接。`-F` 选项应与 `-f` 或 `-i` 选项一起使用。如果未指定 `-f` 或 `-i`，则隐含 `-f`。除非指定了 `-s`，否则 `-F` 选项不执行任何操作。

**`-L`** 当为符号链接创建硬链接时，创建指向符号链接所指向目标的硬链接。这是默认行为。此选项取消 `-P` 选项的效果。

**`-P`** 当为符号链接创建硬链接时，创建指向符号链接本身的硬链接。此选项取消 `-L` 选项的效果。

**`-f`** 如果目标文件已存在，则取消链接以便进行链接。（`-f` 选项覆盖之前所有的 `-i` 和 `-w` 选项。）

**`-h`** 如果 `target_file` 或 `target_dir` 是符号链接，则不跟随它。这在配合 `-f` 选项替换可能指向目录的符号链接时最为有用。

**`-i`** 如果目标文件已存在，则使 `ln` 向标准错误写入提示。如果来自标准输入的响应以字符 `y` 或 `Y` 开头，则取消目标文件的链接以便进行链接。否则，不尝试链接。（`-i` 选项覆盖之前所有的 `-f` 选项。）

**`-n`** 与 `-h` 相同，用于与其他 `ln` 实现兼容。

**`-s`** 创建符号链接。

**`-v`** 使 `ln` 在处理文件时显示详细信息。

**`-w`** 如果符号链接的源当前不存在，则发出警告。

默认情况下，`ln` 创建*硬*链接。文件的硬链接与原始目录项无法区分；对文件所做的任何更改都与用于引用该文件的名称无关。目录不能被硬链接，且硬链接不能跨越文件系统。

符号链接包含它所链接到的文件的名称。对链接执行 [open(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/open.2.md) 操作时，将使用被引用的文件。对符号链接执行 [stat(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/stat.2.md) 将返回被链接的文件；必须执行 [lstat(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/lstat.2.md) 才能获取有关链接本身的信息。可以使用 [readlink(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/readlink.2.md) 调用来读取符号链接的内容。符号链接可以跨越文件系统，并且可以指向目录。

给定一个或两个参数时，`ln` 创建到现有文件 `source_file` 的链接。如果给出了 `target_file`，则链接使用该名称；`target_file` 也可以是放置链接的目录；否则链接放在当前目录中。如果仅指定了目录，则链接将指向 `source_file` 的最后一个路径分量。

给定两个以上参数时，`ln` 在 `target_dir` 中为所有指定的源文件创建链接。所创建的链接与被链接的文件同名。

当该实用程序以 `link` 名称调用时，必须提供恰好两个参数，且两个参数都不能指定目录。在此简单操作模式下不能提供任何选项，它使用传入的两个参数执行 [link(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/link.2.md) 操作。

## 实例

创建名为 **/home/src** 的符号链接并指向 **/usr/src**：

```sh
# ln -s /usr/src /home/src
```

将 **/usr/local/bin/fooprog** 硬链接到文件 **/usr/local/bin/fooprog-1.0**：

```sh
# ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog
```

作为练习，尝试以下命令：

```sh
# ls -i /bin/[
11553 /bin/[
# ls -i /bin/test
11553 /bin/test
```

注意两个文件具有相同的 inode；即 **/bin/\[** 本质上是 [test(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/test.1.md) 命令的别名。此硬链接的存在使得 [test(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/test.1.md) 可以从 shell 脚本中调用，例如使用 `if [ ]` 结构。

在下一个实例中，第二次调用 `ln` 移除原始的 `foo` 并创建一个指向 `baz` 的替换链接：

```sh
# mkdir bar baz
# ln -s bar foo
# ln -shf baz foo
```

如果不使用 `-h` 选项，这将会使 `foo` 继续指向 `bar`，并在 `foo` 内部创建一个指向自身的新符号链接 `baz`。这是目录遍历导致的结果。

一个易于记忆的规则是，`ln` 的参数顺序与 [cp(1)](/man/man1/cp.1.md) 相同：第一个参数需要存在，第二个参数是创建的。

## 兼容性

`-h`、`-i`、`-n`、`-v` 和 `-w` 选项是非标准的，不推荐在脚本中使用。它们仅为与其他 `ln` 实现兼容而提供。

`-F` 选项是 FreeBSD 扩展，不应在可移植脚本中使用。

## 参见

[link(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/link.2.md), [lstat(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/lstat.2.md), [readlink(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/readlink.2.md), [stat(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/stat.2.md), [symlink(2)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man2/symlink.2.md), [symlink(7)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man7/symlink.7.md)

## 标准

`ln` 实用程序遵循 ISO/IEC 9945-2:1993（"POSIX.2"）标准。

简化的 `link` 命令遵循 Version 2 of the Single UNIX Specification（"SUSv2"）标准。

## 历史

`ln` 命令出现于 Version 1 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/ln.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
