CentOS硬链接与软链接 Linux教程



ln 命令格式

ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... TARGET (2nd form)
ln [OPTION]... TARGET... DIRECTORY (3rd form)
ln [OPTION]... -t DIRECTORY TARGET... (4th form)

 

ln 命令参数

--backup[=CONTROL]
  如果指定的链接名已经存在,为该同名文件创建备份文件

-b
  类似 --backup 选项,但不接受参数。

-d, -F, --directory
  允许超级用户创建目录的硬链接(注:由于系统限制很有可能会失败,即使是超级用户)

-f, --force
  如果指定的链接名已经存在,则覆盖该同名文件

-n, --no-dereference
  如果指定的链接名是一个引用目录的符号链接,则将其当作是普通文件

-i, --interactive
  如果指定的链接名已经存在,提示是否覆盖该同名文件

-s, --symbolic
  创建符号链接而非硬链接

-S, --suffix=SUFFIX
  使用 SUFFIX 作为备份文件的后缀

-v, --verbose
  显示链接过程的信息

--help display this help and exit
  打印帮助信息

--version
  打印版本信息

 

ln 实例

a) 创建硬链接

ln testfile testfile-hard

b) 创建符号链接

ln -s testfile testfile-sym

c) 强制创建链接,如果指定的链接名已经存在,则覆盖该同名文件

ln -f testfile testfile-hard

d) 为被覆盖的文件创建备份

ln -bf testfile testfile-hard

e) 为被覆盖的文件创建备份,并以 ".~[num]~" 作为后缀

ln -f --backup=numbered testfile testfile-hard

f) 为被覆盖的文件创建备份,并指定备份文件的后缀为 ".bak"

ln -bf -S '.bak' testfile testfile-hard

 

ln 的 -n 选项

使用 man ln 命令,-n 选项的释义是 "treat destination that is a symlink to a directory as if it were a normal file",如何理解?

假设有 a、b 两个目录,还有一个指向 a 目录的软链接 c:

huey@huey-K42JE:~/huey/linux/cmdline$ mkdir a bhuey@huey-K42JE:~/huey/linux/cmdline$ ln -s a c

现在如果要使软链接 c 改为指向 b 目录而执行下面这条命令:

huey@huey-K42JE:~/huey/linux/cmdline$ ln -sf b c

这并不能达到预期的效果,而是会在 c 的引用目录(即 a 目录)里创建 b 目录的软链接。这种情况下, -n 选项会将软链接 c 当作一个普通文件而被覆盖:

huey@huey-K42JE:~/huey/linux/cmdline$ ln -sfn b c


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回