For faster navigation, this Iframe is preloading the Wikiwand page for ln (Unix).

ln (Unix)

ln 是用来为文件创建链接的标准 Unix 命令

链接文件

[编辑]

通过链接文件,不同的文件名可以指向同一个文件。

ln 可以创建两种类型的链接文件:

  1. 符号链接,也称软链接,这是指向另一个不同路径文件的一个符号路径。
  2. 硬链接,这是一个存储了链接建立时它所指向文件的实际数据的文件副本。

参数

[编辑]
  • 源文件:指定链接的源文件。如果使用-s选项创建符号链接,则“源文件”可以是文件或者目录。创建硬链接时,则“源文件”参数只能是文件;
  • 目标文件:指定源文件的目标链接文件。

从以下命令示例可看出两种链接文件的区别:

$ echo '文件内容' > oringinal.file
$ ln oringinal.file hardlink.file
$ ln -s oringinal.file softlink.file
$ cat softlink.file
文件内容
$ rm oringinal.file
$ cat softlink.file
cat: softlink.file: 没有那个文件或目录
$ cat hardlink.file
文件内容

原始文件被删除后,符号链接将失效,访问软链接时,会提示找不到文件,但硬链接文件还在,而且还保存有原始文件的内容。按照inode的设计理论,硬链接的文件和原始文件对应相同的inode号,共享同一个数据储存区,而符号链接是一个独立的特殊文件,有自己的文件inode号,而内容为指向原始文件的路径。

规范

[编辑]

单一Unix规范(SUS)规定了创建一个原始文件(或目录)的链接(不管是符号链接还是硬链接)文件时的行为。

ln 可以用两种方式使用。第一种,是第一个参数指定原始文件,第二个参数指定链接文件;第二种,指定多于两个选项,应该先是多个原始文件(或目录),最后指定一个目录,所有原始文件(或目录)的链接将会被创建于最后指定的目录里。在后一种方式中,ln 命令的行为和具体的程序实现有关。

ln 和标准的 unlink()link() 函数执行完全一致的操作,详细的 ln 命令用法,可以执行以下两个命令查询:

 $ man ln
 $ ln --help

参见

[编辑]

外部链接

[编辑]

手册页

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
ln (Unix)
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?