在Linux系统上创建软连接和硬连接的方法

 更新时间:2024年08月23日 09:12:33   作者:一口Linux  
这篇文章主要介绍了在Linux系统上创建软连接和硬连接的方法,通过执行 man ln 命令,可以看到这是在文件之间建立链接,而没有提及是软链接或硬链接,文中通过代码和图文介绍的非常详细,需要的朋友可以参考下

在本指南中,我将介绍什么是链接以及如何更好地使用它们以及所需的概念。

通过执行 man ln 命令,可以看到这是在文件之间建立链接,而没有提及是软链接或硬链接。

shashi@linuxtechi ~}$ man ln

类似地,命令 man link 描述为调用链接功能创建文件。

Soft Link

软链接顾名思义就是创建到新文件的新链接。在这种情况下,新文件的节点号将指向旧文件。

Hard Link

在这种情况下,旧文件和新文件都将指向相同的索引节点号。

Symbolic Link

在某些 Unix/Linux 系统中,符号和软链接都被视为相同的。但是实际的差异是,新的文件和旧文件将指向一个新的 Inode 号码,这将完全取决于实施。

注意: 在许多情况下,符号和软链接术语可以互换使用,但是人们必须知道什么时候用什么。

Creating Hard Link

(1) man ln 命令输出如下

shashi@linuxtechi ~}$ man ln
ln  - make links between files

(2) ln 命令无参数,输出如下

shashi@linuxtechi ~}$ln
ln: missing file operand
Try 'ln --help' for more information.

(3) 在两个文件之间创建硬链接,首先检查文件是否存在,否则将创建文件,然后链接它们。

shashi@linuxtechi ~}$ ls -la
total 24
drwx------  4 root root 4096 Feb  6 15:23 .
drwxr-xr-x 23 root root 4096 Jan 25 16:39 ..
-rw-r--r--  1 root root 3122 Jan 25 16:41 .bashrc
drwx------  2 root root 4096 Feb  6 15:23 .cache
-rw-r--r--  1 root root    0 Jan 25 16:41 .hushlogin
-rw-r--r--  1 root root  148 Aug 17  2015 .profile
drwxr-xr-x  2 root root 4096 Jan 25 16:41 .ssh

(4) 使用 touch 命令创建文件

shashi@linuxtechi ~}$ touch 123.txt
shashi@linuxtechi ~}$ ls -l
total 0
-rw-r--r-- 1 root root 0 Feb  6 15:51 123.txt

(5) 使用 cat 命令将内容输入到文件中,然后按 ctrl+c 保存并退出。

shashi@linuxtechi ~}$ cat > 123.txt
Welcome to this World!
^C

(6) 创建 123.txt 和 321.txt 文件之间的硬链接

shashi@linuxtechi ~}$ ln 123.txt 321.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt

(7) 检查文件的索引节点号

shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
$ cat 321.txt
Welcome to this World!

(8) 再创建一个名为 456.txt 的文件,并使用 ln 命令将其链接到 321.txt

shashi@linuxtechi ~}$  ls -li
total 12
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt
$ cat 456.txt
Welcome to this World!
shashi@linuxtechi ~}$   ls -l
total 12
-rw-r--r-- 3 root root 23 Feb  6 15:52 123.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 3 root root 23 Feb  6 15:52 456.txt

(9) 当源文件或这些文件中的任何一个被删除时,它不会影响其他文件。

shashi@linuxtechi ~}$ rm 123.txt
shashi@linuxtechi ~}$ ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ ls -li
total 8
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
794583 -rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!

(10) 不允许跨目录创建硬链接。

shashi@linuxtechi ~}$ls -l
total 8
-rw-r--r-- 2 root root 23 Feb  6 15:52 321.txt
-rw-r--r-- 2 root root 23 Feb  6 15:52 456.txt
shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln abc def
ln: abc: hard link not allowed for directory

(11) 对一个文件内容的任何更改都将影响并相应地更改其他文件的内容。

shashi@linuxtechi ~}$ vi 321.txt
Welcome to this World!
You are welcomed to this new world
:wq
shashi@linuxtechi ~}$ ls -l
total 12
-rw-r--r-- 2 root root   59 Feb  6 16:24 321.txt
-rw-r--r-- 2 root root   59 Feb  6 16:24 456.txt
drwxr-xr-x 2 root root 4096 Feb  6 16:18 abc
shashi@linuxtechi ~}$ cat 456.txt
Welcome to this World!
You are welcomed to this new world

Creating Soft Link:

(1) 使用 touch 命令创建文件 src.txt,使用 cat 命令输入内容,然后按 ctrl+c 保存并退出。

shashi@linuxtechi ~}$ touch src.txt
shashi@linuxtechi ~}$ cat > src.txt
Hello World
^C
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

(2) 将目标文件创建为 dst.txt,并使用 ln -s 命令创建符号链接 (也称为软链接)。查看 dst.txt 文件的内容,可以看到与 src.txt 相同的内容。

shashi@linuxtechi ~}$ ln -s src.txt dst.txt
shashi@linuxtechi ~}$  ls -l
total 4
lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt
shashi@linuxtechi ~}$  cat dst.txt
Hello World

(3) 在符号链接的情况下,源文件和目标文件的 inode 号不同。在权限中出现字母 l,表明这些是链接。dst.txt ->src.txt 将是现在建立的新链接。

shashi@linuxtechi ~}$  ls -li
total 4
794584 lrwxrwxrwx 1 root root  7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root 12 Feb  6 16:32 src.txt

(4) 允许对目录进行符号创建

shashi@linuxtechi ~}$ mkdir abc
shashi@linuxtechi ~}$ ln -s abc def
$ ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

(5) 源和目标的 Inode 编号不同

shashi@linuxtechi ~}$  ls -li
total 8
794585 drwxr-xr-x 2 root root 4096 Feb  6 16:34 abc
794586 lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
794584 lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
794583 -rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt

(6) 如前所述,可以为目录创建符号链接。一旦创建了这些带有符号链接的目录,就可以在这些目录中创建文件。当在源目录中创建文件时,同样的情况也会反映在目标目录中。

shashi@linuxtechi ~}$ $ cd abc
shashi@linuxtechi ~}$  touch 123.txt
shashi@linuxtechi ~}$  vi 123.txt
Hello
:wq!
shashi@linuxtechi ~}$  touch 456.txt
shashi@linuxtechi ~}$  cd ..
shashi@linuxtechi ~}$  ls -l
total 8
drwxr-xr-x 2 root root 4096 Feb  6 16:36 abc
lrwxrwxrwx 1 root root    3 Feb  6 16:34 def -> abc
lrwxrwxrwx 1 root root    7 Feb  6 16:33 dst.txt -> src.txt
-rw-r--r-- 1 root root   12 Feb  6 16:32 src.txt
shashi@linuxtechi ~}$ cd def
shashi@linuxtechi ~}$ ls -l
total 4
-rw-r--r-- 1 root root 6 Feb  6 16:37 123.txt
-rw-r--r-- 1 root root 0 Feb  6 16:36 456.txt
shashi@linuxtechi ~}$ cat 123.txt
Hello

Removing Soft Link / Symbolic Links

使用 rm 和 unlink 命令删除软链接或符号链接

# rm <soft-link-filename>
# unlink <soft-link-filename>

删除软链接目录

# rm <soft-link-directory>
# unlink <soft-link-directory>

以上就是在Linux系统上创建软连接和硬连接的方法的详细内容,更多关于Linux创建软连接和硬连接的资料请关注脚本之家其它相关文章!

相关文章

  • Linux中搭建coturn服务器的过程

    Linux中搭建coturn服务器的过程

    这篇文章主要介绍了Linux中搭建coturn服务器,首先下载coturn源码,进入到coturn路径下执行相应命令,本文给大家介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Linux bzip2 命令的使用

    Linux bzip2 命令的使用

    这篇文章主要介绍了Linux bzip2 命令的使用,帮助大家更好的理解和使用Linux系统,感兴趣的朋友可以了解下
    2020-08-08
  • Linux中rsync命令使用方式

    Linux中rsync命令使用方式

    rsync是一款高效的文件同步工具,支持增量同步、远程同步、文件压缩、权限保留等功能,它可以用于本地或远程文件夹的同步,支持断点续传和排除规则,在本地模式下,可以通过cp命令替代,实现数据的增量备份,在远程模式下
    2025-01-01
  • 分享9个实战及面试常用Linux Shell脚本编写

    分享9个实战及面试常用Linux Shell脚本编写

    这篇文章主要介绍了9个实战及面试常用Shell脚本编写,非常不错,具有一定的收藏价值,需要的朋友可以参考下
    2018-10-10
  • IO复用之select poll epoll的总结(推荐)

    IO复用之select poll epoll的总结(推荐)

    下面小编就为大家带来一篇IO复用之select poll epoll的总结(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Linux安装yum时出现apt-get install E: 无法定位软件包问题解决

    Linux安装yum时出现apt-get install E: 无法定位软件包问题解决

    这篇文章主要介绍了Linux安装yum时出现apt-get install E: 无法定位软件包问题解决的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-12-12
  • linux NFS安装配置及常见问题、/etc/exports配置文件、showmount命令

    linux NFS安装配置及常见问题、/etc/exports配置文件、showmount命令

    这篇文章主要介绍了linux NFS安装配置及常见问题,介绍的也比较详细特分享下,方便需要的朋友
    2014-07-07
  • 判断CC攻击 netstat命令详解

    判断CC攻击 netstat命令详解

    判断CC攻击 netstat命令详解,快速找出有问题的ip。
    2010-12-12
  • Linux中的进程状态和优先级

    Linux中的进程状态和优先级

    这篇文章主要介绍了Linux中的进程状态和优先级方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • linux压缩解压文件夹命令zip unzip和tar详解

    linux压缩解压文件夹命令zip unzip和tar详解

    本文介绍了如何使用zip和unzip命令处理.zip文件,以及如何使用tar命令处理.tar、.tar.gz、.tar.bz2和.tar.xz文件,包括压缩、解压、查看压缩包内容、打包与压缩、解压到指定目录、排除文件、递归压缩和压缩算法对比等内容
    2025-02-02

最新评论