linux 硬链接 软链接_Linux中的软链接–完整参考

linux 硬链接 软链接

A link in Linux systems are pointers to a file or a directory. There are two types of links in Linux, namely soft and hard links.

Linux系统中的链接是指向文件或目录的指针。 Linux中有两种类型的链接,即软链接和硬链接。

In this article, we will examine soft links in detail. Similar to shortcuts in Windows, soft links, also known as symbolic links, point to a file without storing the file’s contents. Any changes made to either the file or the soft link, are reflected in both the versions of the file.

在本文中,我们将详细研究软链接。 与Windows中的快捷方式类似,软链接(也称为符号链接 )指向文件而不存储文件内容。 对文件或软链接的任何更改都会反映在文件的两个版本中。

Linux中软链接的表示 (Representation of Soft Links in Linux)

After understanding the concept of soft links, we need to know how to spot a soft link in a file-system.

了解了软链接的概念之后,我们需要知道如何在文件系统中发现软链接。

配色方案 (Color scheme )

The 'ls' command provides a color scheme for every different component in the Linux file-system. Soft links are denoted by 'cyan' color.

'ls'命令为Linux文件系统中的每个不同组件提供了配色方案。 软链接用'cyan'颜色表示。

Soft Link Ls Cyan
Soft links – ‘program’ and ‘desktop’
软链接–“程序”和“桌面”

In the above output, 'desktop' and 'program' are soft links. It may happen that some systems have modified their default color schemes and therefore are unable to figure out the soft links.

在上面的输出中, 'desktop''program'是软链接。 某些系统可能已经修改了其默认配色方案,因此无法找出软链接。

Using the ls -l command, we can clearly find out links present in a directory.

使用ls -l命令,我们可以清楚地找到目录中存在的链接。

Soft Links in Linux Ls L Edited 1
Contents of the current directory
当前目录的内容

Not only does it specifies links in the directory, but also displays the original file location or directory for a soft link.

它不仅指定目录中的链接,而且还显示软链接的原始文件位置或目录。

GUI表示 (GUI representation)

Similar to shortcuts in Windows, Linux provides a hint in the icons of soft links. The GUI icons for soft links contains arrow signs at the bottom-right corner.

与Windows中的快捷方式类似,Linux在软链接的图标中提供了提示。 软链接的GUI图标在右下角包含箭头符号。

Soft Link Gui Edited
GUI Icons for soft links
软链接的GUI图标

It is quite evident from the figure that soft link named 'desktop' is a pointer to a directory whereas 'program' points to a ‘.cpp’ file.

从图中可以明显看出,名为'desktop'软链接是指向目录的指针,而'program'指向“ .cpp”文件。



如何在Linux中创建软链接? (How to create a soft link in Linux?)

Now that we have seen the methods of spotting a soft link, we will learn how to create soft links in Linux. This is done with the help of 'ln' command.

现在,我们已经看到了发现软链接的方法,我们将学习如何在Linux中创建软链接。 这是在'ln'命令的帮助下完成的。


ln -s <PATH>/<ORIGINAL_FILE> <LINK_NAME>
Soft Link Create
Creating a soft link
创建软链接

The 'ln' command is specifically used to create a link in Linux. The '-s' option used in the above command represents the creation of a soft link.

'ln'命令专门用于在Linux中创建链接。 上面命令中使用的'-s'选项表示创建软链接。

Using the 'ls -l' command, we can check whether the creation of a soft link was successful or not.

使用'ls -l'命令,我们可以检查软链接的创建是否成功。



编辑原始文件 (Editing the original file)

Since a soft link is just a symbol for the original file, any changes made in the original file will be reflected in the soft link as well. Let us demonstrate the changes:

由于软链接只是原始文件的符号,因此在原始文件中所做的任何更改也将反映在软链接中。 让我们演示一下更改:

  • Original File – “my_program.cpp” in the Documents folder

    原始文件 – Documents文件夹中的“ my_program.cpp”
  • Soft Link – “program” on the Desktop

    软链接 –桌面上的“程序”

We will use the sed command to edit the original file.

我们将使用sed命令编辑原始文件。


sed -i "s/main/disdain/g" my_program.cpp
Soft Link Edit Original
Editing original file
编辑原始文件

The above command simply finds all the occurrences of the word “main” and replaces each one of them with the word “disdain”.

上面的命令只是查找单词“ main”的所有出现,并将其中的每个单词替换为“ disdain”。

The 'program' file present on the Desktop is a soft link, therefore has to reflect the changes made in the original file.

桌面上存在的'program'文件是一个软链接,因此必须反映原始文件中所做的更改。

Soft Link Edit Original Done
Changes reflected in the soft link
更改反映在软链接中


通过软链接编辑内容 (Editing the Content through a Soft Link)

Editing the contents of a soft link reflect changes in the original file as well. This can be demonstrated by the following screenshot:

编辑软链接的内容也会反映原始文件中的更改。 以下屏幕截图可以证明这一点:

Soft Link Edit Link
Editing a soft link
编辑软链接

As previously mentioned, 'program' is a soft link. Using 'echo' command, we append the word “Edited” to the soft link. We can clearly see the changes that happen in the original file 'my_program.cpp' as well.

如前所述, 'program'是一个软链接。 使用'echo'命令,我们在软链接后面附加“已编辑”一词。 我们也可以清楚地看到原始文件'my_program.cpp'中发生的更改。

Note: While editing the soft link we did not use 'sed -i' command, as in the process, the soft link is removed and a new file is created with the same name.

注意:在编辑软链接时,我们没有使用'sed -i'命令,因为在此过程中,软链接将被删除,并使用相同的名称创建一个新文件。

We will see later that removing the original file and placing it back, preserves the link.

稍后我们将看到删除原始文件并将其放回原处会保留链接。



识别Linux中断开的软链接 (Identify Broken Soft Links in Linux)

The soft links break when we delete the original file. When using the ‘ls’ command, broken links are displayed in red color with a black background.

当我们删除原始文件时,软链接断开。 使用“ ls”命令时,断开的链接以红色显示,并带有黑色背景。

Soft Link Broken
Broken Link
断链

In the above figure, we move the original file to the current directory. When we remove the original file from their original location, we can see the change in color on a soft link.

在上图中,我们将原始文件移动到当前目录。 当我们从原始位置删除原始文件时,可以在软链接上看到颜色的变化。



修复断开的链接 (Fix broken links)

Every soft link points to an originating file. We can easily fix a broken link by replacing the original file with another file of the same name. I’ve demonstrated the same below.

每个软链接都指向原始文件。 我们可以通过用另一个同名文件替换原始文件来轻松修复断开的链接。 我已经在下面演示了相同的内容。

Soft Link Unbreaking
Fixing a broken link
修复断开的链接


在Linux中删除软链接 (Removing a soft link in Linux)

The easiest way to remove a soft link is using the 'rm' command followed by the link name.

删除软链接的最简单方法是使用'rm'命令,后跟链接名称。


rm <LINK_NAME>
Soft Link Rm
Removing link using ‘rm’
使用“ rm”删除链接

There is one another way to remove links in Linux. It is done by 'unlink' command.

在Linux中还有另一种删除链接的方法。 这是通过'unlink'命令完成的。


unlink <LINK_NAME>
Soft Link Unlink
Soft Link Unlink
软链接取消链接


软链接的软链接 (Soft link of a soft link)

Using the 'ln' command, let us create a soft link to our previously created soft link.

使用'ln'命令,让我们创建一个到先前创建的软链接的软链接。


ln -s <LINK_NAME> <NEW_LINK_NAME>
Soft Links
Soft link of a soft link
软链接的软链接

It is quite clear that these links form a chain. A change in any one of the links will be reflected in every one of the files.

很明显,这些链接构成了一条链。 每个链接中的任何更改都会反映在每个文件中。

Since, the continuous links form a chain, removing any of the in-between links, will break the child links. For instance, if we remove the first soft link 'program', the child link will break.

由于连续的链接形成一条链,因此删除中间的任何链接都会破坏子链接。 例如,如果我们删除第一个软链接'program' ,则子链接将中断。

Soft Link Breakchain
Breaking the chain of soft links
打破软链接链

When we break the link in the middle, the complete chain breaks.

当我们在中间断开链接时,完整的链条也会断开。



结论 (Conclusion)

Soft links are a common Linux feature that links libraries and files in Linux file-systems. This article covers up the creation, properties, and removal of soft links in Linux.

软链接是一种常见的Linux功能,用于链接Linux文件系统中的库和文件。 本文介绍了Linux中软链接的创建,属性和删除。

We hope the article was easy for you to understand. Feel free to comment below for queries or suggestions.

我们希望这篇文章对您来说很容易理解。 如有任何疑问或建议,请在下面发表评论。

翻译自: https://www.journaldev.com/42150/soft-links-in-linux

linux 硬链接 软链接

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值