linux 创建符号链接_如何在Linux上创建和使用符号链接(也称为符号链接)

linux 创建符号链接

linux 创建符号链接

Linux allows you to create symbolic links, or symlinks, that point to another file or folder on your machine. The best way to do this is with the ln terminal command—though there are some graphical file managers that can create symbolic links too.

Linux允许您创建指向机器上另一个文件或文件夹的符号链接或符号链接。 最好的方法是使用ln terminal命令-尽管有些图形文件管理器也可以创建符号链接。

什么是符号链接? (What Are Symbolic Links?)

Symbolic links are basically advanced shortcuts. A symbolic link you create will appear to be the same as the original file or folder it’s pointing at, even though it’s just a link.

符号链接基本上是高级快捷方式。 您创建的符号链接似乎与它指向的原始文件或文件夹相同,即使它只是一个链接。

For example, let’s say you have a program that needs its files stored at /home/user/.program. But you want to store those files on another partition, which is mounted at /mnt/partition. You can move the .program directory to /mnt/partition/.program, and then create a symbolic link at /home/user/.program pointing to /mnt/partition/.program. The program will try to access its folder at /home/user/.program, and the operating system will redirect it to /mnt/partition/.program.

例如,假设您有一个程序需要将其文件存储在/home/user/.program中。 但是您想将这些文件存储在另一个分区上,该分区位于/ mnt / partition。 您可以将.program目录移动到/mnt/partition/.program,然后在/home/user/.program上创建一个指向/mnt/partition/.program的符号链接。 该程序将尝试访问其位于/home/user/.program的文件夹,并且操作系统会将其重定向到/mnt/partition/.program。

This is entirely transparent to the operating system and the programs you use. If you browse to the /home/user/.program directory in a file manager, it will appear to contain the files inside /mnt/partition/.program.

这对于您使用的操作系统和程序是完全透明的。 如果在文件管理器中浏览到/home/user/.program目录,则该目录似乎包含/mnt/partition/.program中的文件。

In addition to “symbolic links”, also known as “soft links”, you can instead create a “hard link”. A symbolic or soft link points to a path in the file system. For example, let’s say you have a symbolic (or “soft”) link from /home/examplefile pointing to /var/examplefile. If you move the file at /var/examplefile, the link at /home/examplefile will be broken. However, if you create a “hard link”, it will actually point to the underlying inode on the file system. So, if you created a hard link from /home/examplefile pointing to /var/examplefile and later moved /var/examplefile, the link at /home/examplefile would still point to the file, no matter where you moved it to. The hard link works at a lower level.

除了“符号链接”(也称为“软链接”)之外,您还可以创建“硬链接”。 符号或软链接指向文件系统中的路径。 例如,假设您有一个来自/ home / examplefile的符号(或“软”)链接指向/ var / examplefile。 如果将文件移至/ var / examplefile,则/ home / examplefile的链接将断开。 但是,如果创建“硬链接”,则它实际上将指向文件系统上的基础索引节点。 因此,如果您从/ home / examplefile创建了指向/ var / examplefile的硬链接,然后又将/ var / examplefile移到了该目录,则无论将其移至何处,/ home / examplefile上的链接仍将指向该文件。 硬链接在较低级别上起作用。

You should generally use standard symbolic links, also known as “soft links”, if you’re not sure which to use.

如果不确定使用哪个符号链接,通常应该使用标准的符号链接,也称为“软链接”。

如何使用ln创建符号链接 (How to Create Symbolic Links with ln)

To create a symbolic link with the ln command, you’ll first need to open a terminal window. Once you have, run the ln command in the following form:

要使用ln命令创建符号链接,首先需要打开一个终端窗口。 拥有之后,以以下形式运行ln命令:

ln -s /path/to/original /path/to/link

You can specify either a path to a directory or file in the command. It will “just work”, whatever you enter.

您可以在命令中指定目录或文件的路径。 无论您输入什么,它都将“正常工作”。

So, if you wanted to create a symbolic link of your Downloads folder located on your Desktop, you’d run the following command:

因此,如果要创建位于桌面上的“下载”文件夹的符号链接,请运行以下命令:

ln -s /home/name/Downloads /home/name/Desktop

The -s in the command creates a symbolic link. If you wanted to create a hard link instead—again, this is something you usually wouldn’t want to do unless you have a specific reason to do so—you’d exclude the -s from the command.

命令中的-s创建符号链接。 如果您想创建一个硬链接(同样,除非您有特殊原因,否则通常不希望这样做),您可以从命令中排除-s

Using our example, if we look inside our Desktop folder, we find a “Downloads” folder that appears to contain all the same files as our main Downloads folder.

以我们的示例为例,如果我们查看Desktop文件夹中的内容,则会发现一个“ Downloads”文件夹,其中似乎包含与主Downloads文件夹相同的所有文件。

如何删除符号链接 (How to Delete Symbolic Links)

To remove symbolic links, you can just delete them normally. For example, you could right-click them and delete them using a graphical file manager, or use the following command, which is used to delete (or “remove”) any type of file:

要删除符号链接,您可以正常删除它们。 例如,您可以右键单击它们,然后使用图形文件管理器删除它们,或使用以下命令删除或删除任何类型的文件:

rm /path/to/link

如何使用图形工具创建符号链接 (How to Create Symbolic Links with a Graphical Tool)

Many Linux file managers offer the ability to create symbolic links graphically. If yours does, you can generally do this by right-clicking a folder or file and selecting “Copy”, and then right-clicking inside another folder and selecting “Make Link”, “Paste as Link”, or a similarly named option.

许多Linux文件管理器都提供了以图形方式创建符号链接的功能。 如果是这样,通常可以通过右键单击一个文件夹或文件并选择“复制”,然后在另一个文件夹中右键单击并选择“进行链接”,“粘贴为链接”或类似命名的选项来执行此操作。

The Nautilus file manager included with GNOME and Ubuntu’s Unity desktops doesn’t have this menu option anymore, but it does have a shortcut that’ll do the same thing. To create a symbolic link in Nautilus, press and hold the Ctrl and Shift keys on your keyboard. Drag and drop a file or folder to another location. Nautilus will create a symbolic link to the original file or folder at the location you drop the file or folder rather than moving the original file or folder.

GNOME和Ubuntu的Unity桌面随附的Nautilus文件管理器不再具有此菜单选项,但是它确实具有可以执行相同操作的快捷方式。 要在Nautilus中创建符号链接,请按住键盘上的Ctrl和Shift键。 将文件或文件夹拖放到另一个位置。 Nautilus将在您放置文件或文件夹的位置创建指向原始文件或文件夹的符号链接,而不是移动原始文件或文件夹。

翻译自: https://www.howtogeek.com/287014/how-to-create-and-use-symbolic-links-aka-symlinks-on-linux/

linux 创建符号链接

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值