百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

如何在 Linux 系统上创建软连接和硬连接 ?

nanshan 2024-10-25 13:11 25 浏览 0 评论

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

通过执行 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>

我的开源项目

  • course-tencent-cloud(酷瓜云课堂 - gitee 仓库)
  • course-tencent-cloud(酷瓜云课堂 - github 仓库)

相关推荐

使用nginx配置域名及禁止直接通过IP访问网站

前段时间刚搭建好这个网站,一直没有关注一个问题,那就是IP地址也可以访问我的网站,今天就专门研究了一下nginx配置问题,争取把这个问题研究透彻。1.nginx配置域名及禁止直接通过IP访问先来看n...

如何在 Linux 中使用 PID 号查找进程名称?

在Linux的复杂世界中,进程是系统运行的核心,每个进程都由一个唯一的「进程ID」(PID)标识。无论是系统管理员在排查失控进程,还是开发者在调试应用程序,知道如何将PID映射到对应的进程名称都是一项...

Linux服务器硬件信息查询与日常运维命令总结

1.服务器硬件信息查询1.1CPU信息查询命令功能描述示例lscpu显示CPU架构、核心数、线程数等lscpucat/proc/cpuinfo详细CPU信息(型号、缓存、频率)cat/proc/c...

Ubuntu 操作系统常用命令详解(ubuntu常用的50个命令)

UbuntuLinux是一款流行的开源操作系统,广泛应用于服务器、开发、学习等场景。命令行是Ubuntu的灵魂,也是高效、稳定管理系统的利器。本文按照各大常用领域,详细总结Ubuntu必学...

从 0 到 1:打造基于 Linux 的私有 API 网关平台

在当今微服务架构盛行的时代,API网关作为服务入口和安全屏障,其重要性日益凸显。你是否想过,不依赖商业方案,完全基于开源组件,在Linux上构建一个属于自己的私有API网关平台?今天就带你...

Nginx搭建简单直播服务器(nginx 直播服务器搭建)

前言使用Nginx+Nginx-rtmp-module在Ubuntu中搭建简单的rtmp推流直播服务器。服务器环境Ubuntu16.04相关概念RTMP:RTMP协议是RealTi...

Linux连不上网?远程卡?这篇网络管理指南你不能错过!

大家好!今天咱们聊个所有Linux用户都躲不开的“老大难”——网络管理。我猜你肯定遇到过这些崩溃时刻:新装的Linux系统连不上Wi-Fi,急得直拍桌子;远程服务器SSH连不上,提示“Connecti...

7天从0到上线!手把手教你用Python Flask打造爆款Web服务

一、为什么全网开发者都在疯学Flask?在当今Web开发的战场,Flask就像一把“瑞士军刀”——轻量级架构让新手3天速成,灵活扩展能力又能支撑百万级用户项目!对比Django的“重型装甲”,Flas...

nginx配置文件详解(nginx反向代理配置详解)

Nginx是一个强大的免费开源的HTTP服务器和反向代理服务器。在Web开发项目中,nginx常用作为静态文件服务器处理静态文件,并负责将动态请求转发至应用服务器(如Django,Flask,et...

30 分钟搞定 Docker 安装与 Nginx 部署,轻松搭建高效 Web 服务

在云计算时代,利用容器技术快速部署应用已成为开发者必备技能。本文将手把手教你在阿里云轻量应用服务器上,通过Docker高效部署Nginx并发布静态网站,全程可视化操作,新手也能轻松上手!一、准...

Nginx 配置实战:从摸鱼到部署,手把手教你搞定生产级配置

各位摸鱼搭子们!今天咱不聊代码里的NullPointerException,改聊点「摸鱼必备生存技能」——Nginx配置!先灵魂拷问一下:写了一堆接口却不会部署?服务器被恶意请求打崩过?静态资源加载...

如何使用 Daphne + Nginx + supervisor部署 Django

前言:从Django3.0开始支持ASGI应用程序运行,使Django完全具有异步功能。Django目前已经更新到5.0,对异步支持也越来越好。但是,异步功能将仅对在ASGI下运行的应用程序可用...

Docker命令最全详解(39个最常用命令)

Docker是云原生的核心,也是大厂的必备技能,下面我就全面来详解Docker核心命令@mikechen本文作者:陈睿|mikechen文章来源:mikechen.cc一、Docker基本命令doc...

ubuntu中如何查看是否已经安装了nginx

在Ubuntu系统中,可以通过以下几种方法检查是否已安装Nginx:方法1:使用dpkg命令(适用于Debian/Ubuntu)bashdpkg-l|grepnginx输出...

OVN 概念与实践(德育概念的泛化在理论和实践中有什么弊端?)

今天我们来讲解OVN的概念和基础实践,要理解本篇博客的内容,需要前置学习:Linux网络设备-Bridge&VethPairLinux网络设备-Bridge详解OVS+Fa...

取消回复欢迎 发表评论: