前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一天一个 Linux 命令(18):ln 命令

一天一个 Linux 命令(18):ln 命令

作者头像
joshua317
发布2021-09-29 15:50:06
5530
发布2021-09-29 15:50:06
举报
文章被收录于专栏:技术博文

本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/147

一天一个 Linux 命令(18):ln 命令

一、简介

Linux里面的ln(英文全拼:link files)命令是一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。

当需要在不同的目录,用到相同的文件时,不需要在每一个需要的目录下都放一个必须相同的文件,只要在某个固定的目录,放上该文件,然后在 其它的目录下用ln命令链接(link)它就可以,不必重复的占用磁盘空间。

二、格式说明

代码语言:javascript
复制
ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
  or:  ln [OPTION]... TARGET                  (2nd form)
  or:  ln [OPTION]... TARGET... DIRECTORY     (3rd form)
  or:  ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
 
ln [参数][源文件或目录][目标文件或目录]
 

Usage: ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
  or:  ln [OPTION]... TARGET                  (2nd form)
  or:  ln [OPTION]... TARGET... DIRECTORY     (3rd form)
  or:  ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]      make a backup of each existing destination file
  -b                          like --backup but does not accept an argument
  -d, -F, --directory         allow the superuser to attempt to hard link
                                directories (note: will probably fail due to
                                system restrictions, even for the superuser)
  -f, --force                 remove existing destination files
  -i, --interactive           prompt whether to remove destinations
  -L, --logical               dereference TARGETs that are symbolic links
  -n, --no-dereference        treat LINK_NAME as a normal file if
                                it is a symbolic link to a directory
  -P, --physical              make hard links directly to symbolic links
  -r, --relative              create symbolic links relative to link location
  -s, --symbolic              make symbolic links instead of hard links
  -S, --suffix=SUFFIX         override the usual backup suffix
  -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create
                                the links
  -T, --no-target-directory   treat LINK_NAME as a normal file always
  -v, --verbose               print name of each linked file
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

Using -s ignores -L and -P.  Otherwise, the last option specified controls
behavior when a TARGET is a symbolic link, defaulting to -P.

三、选项说明

通过find --help 或者man find 进行详细查看

代码语言:javascript
复制
-b 删除,覆盖以前建立的链接

-d 允许超级用户制作目录的硬链接

-f 强制执行

-i 交互模式,文件存在则提示用户是否覆盖

-n 把符号链接视为一般目录

-s 软链接(符号链接)

-v 显示详细的处理过程

-S “-S<字尾备份字符串> ”或 “--suffix=<字尾备份字符串>”

-V “-V<备份方式>”或“--version-control=<备份方式>”

--help 显示帮助信息

--version 显示版本信息

四、命令功能

Linux文件系统中的链接(link),我们可以将其视为文件或者目录的别名,而链接又可分为两种 : 硬链接(hard link)与软链接(symbolic link),硬链接的意思是一个档案可以有多个名称,而软链接的方式则是产生一个特殊的文件或者目录,该档文件或者目录指向另一个文件或者目录的位置。硬链接是存在同一个文件系统中,而软链接却可以跨越不同的文件系统。

五、软链接和硬链接区别

1.软链接

(1).软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式

(2).软链接可以跨文件系统 ,硬链接不可以

(3).软链接可以对一个不存在的文件名进行链接

(4).软链接可以对目录进行链接

2.硬链接

(1).硬链接,以文件副本的形式存在。但不占用实际空间。

(2).不允许给目录创建硬链接

(3).硬链接只有在同一个文件系统中才能创建

这里有两点要注意:

1.ln命令会保持每一处链接文件的同步性,也就是说,不论你改动了哪一处,其它的文件都会发生相同的变化;

2.ln的链接又分软链接和硬链接两种,软链接就是ln –s 源文件 目标文件,它只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间,硬链接 ln 源文件 目标文件,没有参数-s, 它会在你选定的位置上生成一个和源文件大小相同的文件,无论是软链接还是硬链接,文件都保持同步变化。

ln指令用在链接文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则会把前面指定的所有文件或目录复制到该目录中。若同时指定多个文件或目录,且最后的目的地并非是一个已存在的目录,则会出现错误信息

六、常见用法

1.给文件创建软链接,为test.txt文件创建软链接link_test.txt,如果test.txt丢失,link_test.txt将失效:

代码语言:javascript
复制
ln -s test.txt link_test.txt

2.给文件创建硬链接,为test.txt创建硬链接hard_link_test.txt,test.txt与hard_link_test.txt的各项属性相同

代码语言:javascript
复制
ln test.txt hard_link_test.txt

结果如下:

代码语言:javascript
复制
# ll -a|grep test.txt
-rwxrwxrwx   2 root root    0 Sep  8 18:17 hard_link_test.txt
lrwxrwxrwx   1 root root    8 Sep 22 17:30 link_test.txt -> test.txt
-rwxrwxrwx   2 root root    0 Sep  8 18:17 test.txt

3.接上面两个例子,链接完毕后,删除和重建链接原文件

依次执行看下效果:

代码语言:javascript
复制
rm -rf test.txt

ll -a|grep test.txt

touch test.txt

ll -a|grep test.txt

echo "hello" > test.txt

ll -a|grep test.txt

说明:

(1)源文件被删除后,并没有影响硬链接文件;软链接文件在Linux系统下不断的闪烁,提示源文件已经不存在

(2)重建源文件后,软链接不在闪烁提示,说明已经链接成功,找到了链接文件系统;重建后,硬链接文件并没有受到源文件影响,硬链接文件的内容还是保留了删除前源文件的内容,说明硬链接已经失效

4.将文件链接为另一个目录中的相同名字

在test2目录中创建了test.txt的硬链接,修改test2目录中的test.txt文件,同时也会同步到源文件

代码语言:javascript
复制
ln test.txt test2/

5.给目录创建软链接

代码语言:javascript
复制
ln -sv /root/test/test3 /root/test/test5
代码语言:javascript
复制
# ll|grep test3
drwxr-xr-x 3 root root 4096 Sep  8 18:23 test3
lrwxrwxrwx 1 root root   16 Sep 22 17:47 test5 -> /root/test/test3

说明:

(1)目录只能创建软链接

(2)目录创建链接必须用绝对路径,相对路径创建会不成功,会提示:符号连接的层数过多 这样的错误

(3)在链接目标目录中修改文件都会在源文件目录中同步变化

本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/147

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、格式说明
  • 三、选项说明
  • 四、命令功能
  • 五、软链接和硬链接区别
  • 六、常见用法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档