Linux-软连接的使用

软件帮助文本

[rhce@Servera var]$ ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAME
  or:  ln [OPTION]... TARGET
  or:  ln [OPTION]... TARGET... DIRECTORY
  or:  ln [OPTION]... -t DIRECTORY TARGET...
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.

必选参数对长短选项同时适用。
      --backup[=CONTROL]        为每个已存在的目标文件创建备份文件
  -b                            类似--backup,但不接受任何参数
  -d, -F, --directory           创建指向目录的硬链接(只适用于超级用户)
  -f, --force                   强行删除任何已存在的目标文件
  -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            显示此帮助信息并退出
      --version         显示版本信息并退出

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       不进行备份(即使使用了--backup 选项)
  numbered, t     备份文件加上数字进行排序
  existing, nil   若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
  simple, never   永远使用普通方式备份

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

GNU coreutils 在线帮助:<https://www.gnu.org/software/coreutils/>
请向 <http://translationproject.org/team/zh_CN.html> 报告 ln 的翻译错误
完整文档请见:<https://www.gnu.org/software/coreutils/ln>
或者在本地使用:info '(coreutils) ln invocation'

常用的连接命令

创建软硬链接

  • 【硬连接】
    硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。
    • 具体使用方法
    ln [源文件] [链接文件]
    
  • 【软连接】
    软链接文件有类似于Windows的快捷方式。包含的有另一文件的位置信息
    • 具体使用方法
    ln -s [源文件] [链接文件]
    

示例:

前提

[root@Servera test]# pwd
/var/test
[root@Servera test]# tree
.
├── soft_link
└── testFiles
    └── 1.txt
  • 硬链接
[root@Servera test]# ln testFiles/1.txt 2.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
└── testFiles
    └── 1.txt
  • 软连接
[root@Servera test]# ln -s testFiles/1.txt soft_link/3.txt
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
│   └── 3.txt -> testFiles/1.txt
└── testFiles
    └── 1.txt

修改软连接

  • 使用新的源文件或目录
[root@Servera test]# ln -snf testFiles/10.txt soft_link/3.txt 
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
│   └── 3.txt -> testFiles/10.txt
└── testFiles
    ├── 10.txt
    └── 1.txt

删除软链接

  • 依旧使用通用的rm命令进行删除
    • rm [options] [soft_link_file]
[root@Servera test]# rm soft_link/3.txt 
rm:是否删除符号链接 'soft_link/3.txt'?yes
[root@Servera test]# tree
.
├── 2.txt
├── soft_link
└── testFiles
    ├── 10.txt
    └── 1.txt
posted @ 2022-07-06 15:09  Demon丶安梓  阅读(615)  评论(0编辑  收藏  举报