12.2 ln: 在文件之間創建连接

 

 

下一篇: mkdir invocation, 上一篇: link invocation, 上一级: 特殊文件類型   [目錄][索引]


12.2 ln: 在文件之間創建连接

ln 命令會在文件之间建立一个鏈接。默認情况,它會建立一個硬鏈接; 當指定了參數 -s 时, 它會建立一個符號鏈接 (或稱為 软鏈接)。語法:

ln [option]… [-T] target linkname
ln [option]… target
ln [option]… target… directory
ln [option]… -t directory target…
  • 如果指定了两個文件名,ln 命令將第一個文件名創建为第二個文件名的连接。
  • 如果仅给出了一個 targetln 將在當前目錄里創建这個文件的连接。
  • 如果指定了参数 --target-directory (-t) 。或者最后一個参数是已经存在的目錄,且没有指定 --no-target-directory (-T) 参数。 ln 命令將会在那個目錄里給每一個源文件用与源文件相同的文件名产生连接。

通常ln不会替换现有的文件。使用-force(-f)选项来无条件地替换它们,-interactive(-i)选项有条件地替换它们,以及-备份(-b)选项来重命名它们。除非使用-备份(-b)选项,否则就不会有短暂的时间,因为目的地不存在;这是对POSIX的扩展。 缺省时 ln 命令不會替换已存在的文件连接。使用 --force (-f) 参数可以无条件地替换连接,使用 --interactive (-i) 参数可以有条件地替换连接。使用 --backup (-b) 参数可以重命名它们。Unless the --backup (-b) option is used there is no brief moment when the destination does not exist; this is an extension to POSIX.

硬连接 是文件的另一個名称;這种连接方式无法和原文件区分开来。从技术上讲,它们有相同的索引節点 (inode)。而索引節点包含了直实文件(file-indeed)的所有信息。 所以说索引 就是 文件这种说法是不对的。在大多数系统里只有超级用户才可以对目录建立超链接(要谨慎建立目录的硬链接,因為如果形成了一個循环會给其它许多实用程序使用带来麻烦)。硬链接只能在同一文件系统分区中建立(然而,这一限制并不是 POSIX 標準强制规定的)。

符号链接 Symbolic links (简写为symlinks) 是一种特殊的文件类型(并不是所有的内核都支持符号链接, System V release 3 以及更老版本的系统不支持符号链接) 符号链接内部实际是指向了不同的文件名。对符号文件的大多数操作 (如读文件,写文件,复制文件等)内核会自动解析符号链接, 并对符号链接所指向的文件进行对应的操作。但有些操作却只会针对符号 链接文件本身进行操作,如移动、删除。 The owner and group of a symlink are not significant to file access performed through the link, but do have implications on deleting a symbolic link from a directory with the restricted deletion bit set. On the GNU system, the mode of a symlink has no significance and cannot be changed, but on some BSD systems, the mode can be changed and will affect whether the symlink will be traversed in file name resolution. See Symbolic Links in The GNU C Library Reference Manual.

符号链接可以包含任意的字符串;当符号链接指向的文件丢失时, 符号链接就会变成dangling symlink occurs when the string in the symlink does not resolve to a file. There are no restrictions against creating dangling symbolic links. There are trade-offs to using absolute or relative symlinks. An absolute symlink always points to the same file, even if the directory containing the link is moved. However, if the symlink is visible from more than one machine (such as on a networked file system), the file pointed to might not always be the same. A relative symbolic link is resolved in relation to the directory that contains the link, and is often useful in referring to files on the same device without regards to what name that device is mounted on when accessed via networked machines.

When creating a relative symlink in a different location than the current directory, the resolution of the symlink will be different than the resolution of the same string from the current directory. Therefore, many users prefer to first change directories to the location where the relative symlink will be created, so that tab-completion or other file resolution will find the same target as what will be placed in the symlink.

這個命令可以接收以下給定的參數。也可參考 通用選項

-b
--backup[=method]

参閱 備份選項。对在链接时会被删除、覆盖的文件进行备份。

-d
-F
--directory

允許具有相應權限的用戶建立一个目錄的硬鏈接。请注意,由於系統限制,即便是超级用戶也可能会建立失敗。

-f
--force

链接时先将与 dist 同名称的文件删除

-i
--interactive

创建的 dist 有冲突时进行提示

-L
--logical

如果 -s 没有生效,並且源文件是一個符號鏈接,則創建指向符號鏈接引用的文件的硬鏈接, 而不是符號鏈接的本身。

-n
--no-dereference

创建符号链接时,将 dist 视为一般的文件处理

When the destination is an actual directory (not a symlink to one), there is no ambiguity. The link is created in that directory. But when the specified destination is a symlink to a directory, there are two ways to treat the user’s request. ln can treat the destination just as it would a normal directory and create the link in it. On the other hand, the destination can be viewed as a non-directory—as the symlink itself. In that case, ln must delete or backup that symlink before creating the new link. The default is to treat a destination that is a symlink to a directory just like a directory.

This option is weaker than the --no-target-directory (-T) option, so it has no effect if both options are given.

-P
--physical

If -s is not in effect, and the source file is a symbolic link, create the hard link to the symbolic link itself. On platforms where this is not supported by the kernel, this option creates a symbolic link with identical contents; since symbolic link contents cannot be edited, any file name resolution performed through either link will be the same as if a hard link had been created.

-r
--relative

Make symbolic links relative to the link location.

Example:

ln -srv /a/file /tmp
'/tmp/file' -> '../a/file'

Relative symbolic links are generated based on their canonicalized containing directory, and canonicalized targets. I.e., all symbolic links in these file names will be resolved. See realpath invocation, which gives greater control over relative file name generation, as demonstrated in the following example:

ln--relative() {
  test "$1" = --no-symlinks && { nosym=$1; shift; }
  target="$1";
  test -d "$2" && link="$2/." || link="$2"
  rtarget="$(realpath $nosym -m "$target" \
              --relative-to "$(dirname "$link")")"
  ln -s -v "$rtarget" "$link"
}
-s
--symbolic

創建符號鏈接而非硬鏈接。這個選項仅會在不支持符號鏈接的系統上出現一条错誤信息。

-S suffix
--suffix=suffix

使用 suffix后缀对使用 -b 參數的文件进行备份。参閱 備份選項

-t directory
--target-directory=directory

指定目標的 目錄。参閱 Target directory

-T
--no-target-directory

最后一个参数是目录或者是指向目录的链接时不对其进行特殊处理。参目 Target directory.

-v
--verbose

创建连接成功后显示每一個文件的名稱。

如果 -L-P 同时存在,则以最后的一个参数优先。 如果 -s 也存在,-L-P 则会被忽略。如 果两个参数都没有。那么如果系统 链接 支持硬链接到符号链接(如 GNU 系统),那么默认为 -P。如果 链接遵循符号链接(如 BSD) ,则默认为 -L

退出狀态为零表示成功,而非零表示失敗。

范例:

错误示范:

# 创建一个指向目录 ../a 中 a 的链接
# 并不会真的有用,因为它指向了自己。
ln -s a ..

正确示范:

# 在创建符号链接前,先改变目录,以免造成混淆。
cd ..
ln -s adir/a .

错误示范:

# 硬链接的文件名不容易移动
ln -s $(pwd)/a /some/dir/

正确示范:

# 相对的文件名可以在目录移动中存活,也可以在网络文件系统中工作。
ln -s afile anotherfile
ln -s ../adir/afile yetanotherfile

下一篇: mkdir invocation, 上一篇: link invocation, 上一级: Special file types   [目錄][索引]

posted @ 2022-11-22 16:36  偊偊而步  阅读(165)  评论(0)    收藏  举报