为初学者准备的 ln 命令教程（5 个示例）
ln 链接 符号链接
![](/data/attachment/album/201803/31/233039giepnrek8uy73usq.jpg)


当我们在命令行上工作时，您可能需要在文件之间创建链接。这时，您可以可以借助一个专用命令，`ln`。本教程中，我们将通过一些简单易理解的例子来讨论此工具的基础知识。在此之前，值得一提的是，本教程所有例子都已在 Ubuntu 16.04 上测试通过。


### Linux ln 命令


正如你现在所了解的，`ln` 命令能够让您在文件之间创建链接。下面就是 `ln` 工具的语法（或者使用其他一些可行的语法）。



```
ln [OPTION]... [-T] TARGET LINK_NAME （第一种形式）
ln [OPTION]... TARGET （第二种形式）
ln [OPTION]... TARGET... DIRECTORY （第三种形式）
ln [OPTION]... -t DIRECTORY TARGET... （第四种形式）

```

下面是 `ln` 工具 man 文档描述的内容：



> 
> 在第一种形式下，为目标位置（TARGET）创建一个叫 LINK\_NAME 的链接。在第二种形式下，为目标位置（TARGET）在当前目录下创建一个链接（LCTT 译注：创建的为同名链接）。在第三和第四种形式中，在 DIRECTORY 目录下为每一个目标位置（TARGET）创建链接。默认创建硬链接，符号链接需要 `--symbolic` 选项。默认创建的每一个创建的链接（新链接的名字）都不能已经存在。当创建硬链接时，目标位置（TARGET）文件必须存在；符号链接可以保存任意文本，如果之后解析，相对链接的解析与其父目录有关。
> 
> 
> 


通过下面问答风格的例子，可能会给你更好的理解。但是在此之前，建议您先了解 [硬链接和软链接的区别](https://medium.com/meatandmachines/explaining-the-difference-between-hard-links-symbolic-links-using-bruce-lee-32828832e8d3).


### Q1. 如何使用 ln 命令创建硬链接？


这很简单，你只需要像下面使用 `ln` 命令：



```
ln [file] [hard-link-to-file]

```

例如：



```
ln test.txt test_hard_link.txt

```

[![如何使用 ln 命令创建硬链接](/data/attachment/album/201803/31/233111emob0bgz50c9qobd.png)](https://www.howtoforge.com/images/command-tutorial/big/ln-hard-link.png)


如此，您便可以看见一个已经创建好的，名为 `test_hard_link.txt` 的硬链接。


### Q2. 如何使用 ln 命令创建软/符号链接？


使用 `-s` 命令行选项：



```
ln -s [file] [soft-link-to-file]

```

例如：



```
ln -s test.txt test_soft_link.txt

```

[![如何使用 ln 命令创建软/符号链接](/data/attachment/album/201803/31/233113cxk8xx2kieeex9ai.png)](https://www.howtoforge.com/images/command-tutorial/big/ln-soft-link.png)


`test_soft_link.txt` 文件就是一个软/符号链接，以天蓝色文本 [标识](https://askubuntu.com/questions/17299/what-do-the-different-colors-mean-in-ls)。


### Q3. 如何使用 ln 命令删除既存的同名目标文件？


默认情况下，`ln` 不允许您在目标目录下创建已存在的链接。


[![ln 命令示例](/data/attachment/album/201803/31/233114mzfcuxfxvvpqy4pt.png)](https://www.howtoforge.com/images/command-tutorial/big/ln-file-exists.png)


然而，如果一定要这么做，您可以使用 `-f` 命令行选项覆盖此行为。


[![如何使用 ln 命令创建软/符号链接](/data/attachment/album/201803/31/233137g6rkgt9nn62ktt0r.png)](https://www.howtoforge.com/images/command-tutorial/big/ln-f-option.png)


提示：如果您想在此删除过程中有所交互，您可以使用 `-i` 选项。


### Q4. 如何使用 ln 命令创建现有文件的同名备份？


如果您不想 `ln` 删除同名的现有文件，您可以为这些文件创建备份。使用 `-b` 即可实现此效果，以这种方式创建的备份文件，会在其文件名结尾处包含一个波浪号（`~`）。


[![如何使用 ln 命令创建现有文件的同名备份](/data/attachment/album/201803/31/233147q1bmau754ch56k7g.png)](https://www.howtoforge.com/images/command-tutorial/big/ln-b-option.png)


### Q5. 如何在当前目录以外的其它目录创建链接？


使用 `-t` 选项指定一个文件目录（除了当前目录）。比如：



```
ls test* | xargs ln -s -t /home/himanshu/Desktop/

```

上述命令会为（当前目录下的）所有 `test*` 文件创建链接，并放到桌面目录下。


### 总结


当然，尤其对于新手来说，`ln` 并不是日常必备命令。但是，这是一个有用的命令，因为你永远不知道它什么时候能够节省你一天的时间。对于这个命令，我们已经讨论了一些实用的选项，如果你已经完成了这些，可以查询 [man 文档](https://linux.die.net/man/1/ln) 来了解更多详情。




---


via: <https://www.howtoforge.com/linux-ln-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[CYLeft](https://github.com/CYLeft) 校对：[Locez](https://github.com/locez)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux ln Command Tutorial for Beginners (5 Examples)

### On this page

[Linux ln command](#linux-ln-command)[Q1. How to create a hard link using ln?](#q-how-to-create-a-hard-link-using-ln)[Q2. How to create soft/symbolic link using ln?](#q-how-to-create-softsymbolic-link-using-ln)[Q3. How to make ln remove existing destination files of same name?](#q-how-to-make-ln-remove-existing-destination-files-of-same-name)[Q4. How to make ln create a backup of existing files with the same name?](#q-how-to-make-ln-create-a-backup-of-existing-files-with-the-same-name)[Q5. How to create links in a directory other than the current directory?](#q-how-to-create-links-in-a-directory-other-than-the-current-directory)[Conclusion](#conclusion)

Sometimes, while working on the command line, you need to create links between files. This can be achieved using a dedicated command, dubbed **ln**. In this tutorial, we will discuss the basics of this tool using some easy-to-understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 22.04 machine.

## Linux ln command

As you have understood by now, the ln command lets you make links between files. Following is the syntax (or rather a different syntax available) for this tool:

ln [OPTION]... [-T] TARGET LINK_NAME (1st form)

ln [OPTION]... TARGET (2nd form)

ln [OPTION]... TARGET... DIRECTORY (3rd form)

ln [OPTION]... -t DIRECTORY TARGET... (4th form)

And here's how the tool's man page explains it:

In the 1st form, create a link to TARGET with the name LINK_NAME. In the 2nd form, create a link

to TARGET in the current directory. In the 3rd and 4th forms, create links to each TARGET in

DIRECTORY. Create hard links by default, symbolic links with --symbolic. By default, each

destination (name of new link) should not already exist. When creating hard links, each TARGET

must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is

interpreted in relation to its parent directory.

The following Q&A-styled examples will give you a better idea of how the ln command works. But before that, it's good for you to get an understanding of what's the [difference between hard links and soft links](https://medium.com/meatandmachines/explaining-the-difference-between-hard-links-symbolic-links-using-bruce-lee-32828832e8d3).

## Q1. How to create a hard link using ln?

That's pretty straightforward - all you have to do is to use the *ln* command in the following way:

ln [file] [hard-link-to-file]

For example:

ln test.txt test_hard_link.txt

So you can see a hard link was created with the name *test_hard_link.txt*.

## Q2. How to create soft/symbolic link using ln?

For this, use the -s command line option.

ln -s [file] [soft-link-to-file]

For example:

ln -s test.txt test_soft_link.txt

The test_soft_link.txt file is a soft/symbolic link, as [confirmed](https://askubuntu.com/questions/17299/what-do-the-different-colors-mean-in-ls) by its sky-blue text color.

## Q3. How to make ln remove existing destination files of same name?

By default, ln won't let you create a link if a file of the same name already exists in the destination directory.

However, if you want, you can make ln override this behavior by using the **-f** command line option.

**Note**: You can use the **-i** command line option if you want to make all this deletion process interactive.

## Q4. How to make ln create a backup of existing files with the same name?

If you don't want ln to delete existing files of the same name, you can make it create a backup of these files. This can be achieved using the **-b** command line option. Backup files created this way will contain a tilde (~) toward the end of their name.

## Q5. How to create links in a directory other than the current directory?

A particular destination directory (other than the current one) can be specified using the **-t** command line option. For example:

ls test* | xargs ln -s -t /home/himanshu/Desktop/

The aforementioned command will create links to all test* files (present in the current directory) and put them in the Desktop directory.

## Conclusion

Agreed, **ln** isn't something that you'll require on daily basis, especially if you're a newbie. But it's a helpful command to know about, as you never know when it'd save your day. We've discussed some useful command line options the tool offers. Once you're done with these, you can learn more about ln by heading to its [man page](https://linux.die.net/man/1/ln). Another interesting command you might want to check out is the [tee](https://www.howtoforge.com/linux-tee-command/) command.