原文: Linux ln – How to Create a Symbolic Link in Linux [Example Bash Command]

符号链接(symbolic)是 Linux 中指向其他文件或目录(文件夹)的一种文件类型。

你可以通过在命令行中使用 ln 命令来创建一个符号链接。

符号链接很有用,因为它们是一个文件或目录的快捷方式。

在这篇文章中,我将介绍如何使用 ln 命令来创建一个文件或目录的符号链接。

Linux 中软链接和硬链接的区别是什么

一个软链接或符号链接将指向你系统中的原始文件。硬链接将创建一个文件的副本。

软链接可以指向不同文件系统上的其他文件或目录,而硬链接不能。

如何创建一个文件的符号链接

你可以使用 Mac 上的终端程序或 Windows 上的命令提示符找到命令行。

下面是在终端中创建文件的符号链接的基本语法。

ln -s existing_source_file optional_symbolic_link

你使用 ln 命令来创建文件的链接,并使用 -s 选项来指定这将是一个符号链接。如果你省略了 -s 选项,那么将创建一个硬链接。

existing_source_file 表示你要为其创建符号链接的计算机上的文件。

optional_symbolic_link 参数是你想要创建的符号链接的名称。如果省略,那么系统将在你当前所在的目录中为你创建一个新的链接。

让我们来看看一个例子,以更好地理解它是如何工作的。

在我的桌面上有一个叫作 example_fcc_file.txt 的文件。

Screen-Shot-2022-02-19-at-7.48.02-PM

我将需要首先打开我的终端,然后确保我在 Desktop 目录中。我可以运行 cd Desktop 命令来导航到我的桌面。

运行该命令后,你应该看到你现在是在桌面上。

jessicawilkins@Dedrias-MacBook-Pro-2 ~ % cd Desktop
jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % 

然后我可以使用 ln 命令创建一个新的符号链接,名为 fcc_link.txt

ln -s example_fcc_file.txt fcc_link.txt

当你在终端运行该命令时,你会注意到没有任何返回。这是因为当 ln 命令成功时,不会有任何输出。

jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % ln -s example_fcc_file.txt fcc_link.txt


jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % 

要检查你的符号链接是否成功,你可以使用 ls 命令。`ls` 命令将列出文件的信息,-l 标志代表符号链接。

ls -l fcc_link.txt

当你运行该命令时,你应该在终端看到这种类型的结果。

lrwxr-xr-x  1 jessicawilkins  staff  20 Feb 19 19:56 fcc_link.txt -> example_fcc_file.txt

输出中的 fcc_link.txt -> example_fcc_file.txt 部分显示,符号链接指向名为 example_fcc_file.txt 的文件。

你也应该看到这个新的符号链接出现在你的目录中。

Screen-Shot-2022-02-19-at-8.11.09-PM

如何为一个目录创建符号链接

在这个例子中,我们要创建一个名为 my_music 的符号链接,它将指向我的电脑 home 目录下的 Music 文件夹。

首先,确保你是在 home 目录下。你可以在命令行中运行 cd 来回到你的 home 目录。

jessicawilkins@Dedrias-MacBook-Pro-2 Desktop % cd
jessicawilkins@Dedrias-MacBook-Pro-2 ~ % 

然后你可以使用 ln 命令来创建一个到 Music 目录的符号链接。

ln -s /Users/jessicawilkins/Music ~/my_music

如果成功,你应该在 home 目录中看到它。

Screen-Shot-2022-02-19-at-8.38.14-PM

如何删除符号链接

要删除符号链接,你可以使用 unlinkrm 命令。

如果我们想删除我们先前创建的 fcc_link.txt 符号链接,那么我们可以使用这两个命令中的任何一个:

rm fcc_link.txt
unlink fcc_link.txt

现在我们应该看到,符号链接已经从我们的目录中被删除。

Screen-Shot-2022-02-19-at-8.47.30-PM

如何覆盖符号链接

如果我们试图创建一个新的名为 fcc_link.txt 的符号链接,那么会导致错误,因为它已经被使用并指向另一个文件。

ln: fcc_link.txt: File exists

你可以通过使用强制(-f)选项覆盖这个错误。

ln -sf example_fcc_file.txt fcc_link.txt

如何学习更多关于 ln 命令的知识

如果你想了解更多关于 ln 命令的信息,那么你可以在 man 页面(Linux 命令使用手册)中阅读它。

在你的终端运行 man ln,你应该可以看到 ln 命令的手册页面。

LN(1)                     BSD General Commands Manual                    LN(1)

NAME
     link, ln -- make links

SYNOPSIS
     ln [-Ffhinsv] source_file [target_file]
     ln [-Ffhinsv] source_file ... target_dir
     link source_file target_file

DESCRIPTION
     The ln utility creates a new directory entry (linked file) which has the same modes as the original file.  It is
     useful for maintaining multiple copies of a file in many places at once without using up storage for the
     ``copies''; instead, a link ``points'' to the original copy.  There are two types of links; hard links and sym-
     bolic links.  How a link ``points'' to a file is one of the differences between a hard and symbolic link.

     The options are as follows:

     -F    If the target file already exists and is a directory, then remove it so that the link may occur.  The -F
           option should be used with either -f or -i options.  If none is specified, -f is implied.  The -F option
           is a no-op unless -s option is specified.

     -h    If the target_file or target_dir is a symbolic link, do not follow it.  This is most useful with the -f
           option, to replace a symlink which may point to a directory.

     -f    If the target file already exists, then unlink it so that the link may occur.  (The -f option overrides

总结

符号链接(symbolic)是 Linux 中指向其他文件或目录(文件夹)的一种文件类型。你可以通过在命令行中使用 ln 命令来创建一个符号链接。

符号链接很有用,因为它们是一个文件或目录的快捷方式。

下面是使用终端创建一个文件的符号链接的基本语法:

ln -s existing_source_file optional_symbolic_link

下面是使用终端创建一个目录的符号链接的基本语法:

ln -s path_to_existing_directory name_of_symbolic_link

要删除符号链接,你可以使用 unlinkrm 命令:

rm name_of_symbolic_link
unlink name_of_symbolic_link

如果你需要删除一个符号链接,那么你可以使用这个命令:

ln -sf path_to_existing_directory name_of_symbolic_link

我希望你喜欢这篇关于符号链接的文章,祝你在编程之旅中好运。