Home > System Tutorial > LINUX > body text

How to Create Symbolic Links in Linux [Complete Guide]

WBOY
Release: 2024-02-09 14:21:23
forward
907 people have browsed it

Symbolic links, also known as soft links or Symlinks, are a special type of file. It is similar to a shortcut in Windows, pointing to another file or directory. Creating a symbolic link is equivalent to creating an alias for the actual file.

When you access a symbolic link, you are actually accessing the target file that the symbolic link points to. Changes to the contents of the linked file affect the contents of the actual target file.

Use the ls command with the -l option to display symbolic links. They are as follows:

lrwxrwxrwx 1 linuxmi linuxmi     11  2月 1日 18:58 linuxmi.md -> linuxmi.dat
Copy after login
如何在 Linux 中创建符号链接 [完整指南]

Symbolic links provide a convenient way to organize and share files. They provide fast access to long and confusing directory paths. They are heavily used in Linux for linking libraries.

Now that you know something about symbolic links, let's see how to create them.

How to create a symbolic link in Linux

To create a symbolic link from link name to a target file, you can use the ln command with the -s option as follows:

ln -s target_file link_name
Copy after login
The

-s option is important here. Determine the link as a soft link. If you don't use it, it creates a hard link. I will explain the difference between soft links and hard links in another article.

How to track symbolic links

To know which real file the link actually points to, use the realpath command:

realpath link_name
Copy after login

There are other ways to track soft links to their source files, but realpath is the simplest.

如何在 Linux 中创建符号链接 [完整指南]

How to delete symbolic links

There is no special command to delete symbolic links in Linux. You can use the same rm command that you use to delete files and directories.

rm link_name
Copy after login
如何在 Linux 中创建符号链接 [完整指南]

Deleting a link will not delete the source file it links to.

You can also delete multiple symbolic links in one command:

rm link1 link2
Copy after login

There is also an unlink command. But unlike the impression given by the name, the unlink command is not specifically used to delete links. Of course, it can delete files, folders and links. However, it has certain limitations and the rm command is a better choice even for removing links.

💡If you want to use the find command to list symbolic links, you can use the *-type l option. *

Notes on symbolic links

Symbolic links can sometimes be confusing, so there are a few things you should be aware of.

Changes made to the link are reflected in the original file

After all, that’s the whole purpose of links. You can access the target file by accessing the link. You can make changes to the target file through links. Let's see examples.

I have a file prog.py in newdir/test_dir. It has the following properties:

┌──(linuxmi㉿linuxmi)-[~]
└─$ ln -s /home/linuxmi/linuxmi.com/linuxmi/linuxmi.py test
Copy after login
如何在 Linux 中创建符号链接 [完整指南]

以下是新创建链接的属性:

lrwxrwxrwx 1 linuxmi linuxmi     44  2月 1日 19:24 test -> /home/linuxmi/linuxmi.com/linuxmi/linuxmi.py
Copy after login

注意到行首的 l(是 L,不是一 1)了吗?如果你熟悉 Linux 中的文件权限,你就会知道’l’表示链接,因此它告诉你这个文件实际上是一个链接。提个醒,- 表示文件,d 表示目录。

现在,如果我使用此链接更改内容或属性,同样会反映在目标文件中。例如,我在软链接上使用 touch 命令,你会注意到它改变了目标文件的时间戳。

┌──(linuxmi㉿linuxmi)-[~/linuxmi]
└─$ touch test
                                                                           
┌──(linuxmi㉿linuxmi)-[~/linuxmi]
└─$ ls -l /home/linuxmi/linuxmi.com/linuxmi/linuxmi.py
-rw-r--r-- 1 linuxmi linuxmi 1926  2月 1日 19:27 /home/linuxmi/linuxmi.com/linuxmi/linuxmi.py
Copy after login

它链接到文件还是目录?你可能不知道!

您如何知道链接指向的是文件还是目录?在您按照路径访问目标文件本身之前,您无法知道这一点。

您可以创建指向不存在的文件或目录的链接

是的,这完全有可能。这就是为什么在 Linux 中创建软链接时应该小心的原因。您链接到的目标文件不需要存在。创建指向不存在的文件/目录的链接时,您不会收到任何错误或警告。

ls 命令仍然有效。仅当您尝试通过链接或单独访问目标文件时才会出现错误。

┌──(linuxmi㉿linuxmi)-[~/linuxmi]
└─$ ln -s non_existant_dir link_dir
                                                                           
┌──(linuxmi㉿linuxmi)-[~/linuxmi]
└─$ less link_dir
link_dir: 没有那个文件或目录
Copy after login
如何在 Linux 中创建符号链接 [完整指南]

符号链接是使用 777 权限创建的,但它没有任何意义

您是否注意到符号链接上的文件权限?符号链接始终使用 777 权限 (rwxrwxrwx) 创建。对于常规文件,这意味着任何人都可以访问该文件。但链接并非如此。

lrwxrwxrwx 1 linuxmi linuxmi 11 2月 1日 18:58 linuxmi.md -> linuxmi.dat
如果链接上的文件权限按原样处理,任何用户都可以创建指向安全文件的符号链接并自由访问它。那将是一个主要的安全问题。值得庆幸的是,这不会发生。因为目标文件的权限很重要,而不是链接的权限。
Copy after login

您可以使用 chmod 命令更改链接的权限,但它会更改链接文件的权限,而不是链接本身。

您可以链接到一个链接的链接,也就是链式符号链接

您可以创建指向另一个链接的符号链接等等。这称为链式符号链接。最好避免使用它们,因为它会造成更多混乱。

嗯,就是这样。我假设您现在对软链接有了更好的了解,并且您知道如何在 Linux 中创建符号链接。您也可以查看 symlinks 命令,它可以帮助您在 Linux 中找到损坏的符号链接并轻松管理它们。

The above is the detailed content of How to Create Symbolic Links in Linux [Complete Guide]. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!