• 要找福利?记不住网址?搜不到?如何与我们保持联系?试试 CTRL+D 把零点博客添加进您的浏览器收藏夹,一键直达永不失联!

Linux ln命令

linux系统 涛涛 87次浏览 0个评论

Linux ln命令是一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接。

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

语法

<span class="pln"> ln </span><span class="pun">[参数][源文件或目录][目标文件或目录]</span>

其中参数的格式为

[-bdfinsvF] [-S backup-suffix] [-V {numbered,existing,simple}]

[–help] [–version] [–]

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

不论是硬链接或软链接都不会将原本的档案复制一份,只会占用非常少量的磁碟空间。

软链接

  • 1.软链接,以路径的形式存在。类似于Windows操作系统中的快捷方式
  • 2.软链接可以 跨文件系统 ,硬链接不可以
  • 3.软链接可以对一个不存在的文件名进行链接
  • 4.软链接可以对目录进行链接

硬链接

  • 1.硬链接,以文件副本的形式存在。但不占用实际空间。
  • 2.不允许给目录创建硬链接
  • 3.硬链接只有在同一个文件系统中才能创建

命令参数

必要参数

  • -b 删除,覆盖以前建立的链接
  • -d 允许超级用户制作目录的硬链接
  • -f 强制执行
  • -i 交互模式,文件存在则提示用户是否覆盖
  • -n 把符号链接视为一般目录
  • -s 软链接(符号链接)
  • -v 显示详细的处理过程

选择参数

  • -S “-S<字尾备份字符串> “或 “–suffix=<字尾备份字符串>”
  • -V “-V<备份方式>”或”–version-control=<备份方式>”
  • –help 显示帮助信息
  • –version 显示版本信息

实例

给文件创建软链接,为log2013.log文件创建软链接link2013,如果log2013.log丢失,link2013将失效:

<span class="pln">ln </span><span class="pun">-</span><span class="pln">s log2013</span><span class="pun">.</span><span class="pln">log link2013</span>

输出:

<span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ll
</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span> <span class="lit">1</span><span class="pln"> root bin      </span><span class="lit">61</span> <span class="lit">11</span><span class="pun">-</span><span class="lit">13</span> <span class="lit">06</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log
</span><span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ln </span><span class="pun">-</span><span class="pln">s log2013</span><span class="pun">.</span><span class="pln">log link2013
</span><span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ll
lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> root root     </span><span class="lit">11</span> <span class="lit">12</span><span class="pun">-</span><span class="lit">07</span> <span class="lit">16</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> link2013 </span><span class="pun">-></span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log
</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span> <span class="lit">1</span><span class="pln"> root bin      </span><span class="lit">61</span> <span class="lit">11</span><span class="pun">-</span><span class="lit">13</span> <span class="lit">06</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log</span>

给文件创建硬链接,为log2013.log创建硬链接ln2013,log2013.log与ln2013的各项属性相同

<span class="pln">ln log2013</span><span class="pun">.</span><span class="pln">log ln2013</span>

输出:

<span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ll
lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> root root     </span><span class="lit">11</span> <span class="lit">12</span><span class="pun">-</span><span class="lit">07</span> <span class="lit">16</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> link2013 </span><span class="pun">-></span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log
</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span> <span class="lit">1</span><span class="pln"> root bin      </span><span class="lit">61</span> <span class="lit">11</span><span class="pun">-</span><span class="lit">13</span> <span class="lit">06</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log
</span><span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ln log2013</span><span class="pun">.</span><span class="pln">log ln2013
</span><span class="pun">[</span><span class="pln">root@localhost test</span><span class="pun">]#</span><span class="pln"> ll
lrwxrwxrwx </span><span class="lit">1</span><span class="pln"> root root     </span><span class="lit">11</span> <span class="lit">12</span><span class="pun">-</span><span class="lit">07</span> <span class="lit">16</span><span class="pun">:</span><span class="lit">01</span><span class="pln"> link2013 </span><span class="pun">-></span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log
</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span> <span class="lit">2</span><span class="pln"> root bin      </span><span class="lit">61</span> <span class="lit">11</span><span class="pun">-</span><span class="lit">13</span> <span class="lit">06</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> ln2013
</span><span class="pun">-</span><span class="pln">rw</span><span class="pun">-</span><span class="pln">r</span><span class="pun">--</span><span class="pln">r</span><span class="pun">--</span> <span class="lit">2</span><span class="pln"> root bin      </span><span class="lit">61</span> <span class="lit">11</span><span class="pun">-</span><span class="lit">13</span> <span class="lit">06</span><span class="pun">:</span><span class="lit">03</span><span class="pln"> log2013</span><span class="pun">.</span><span class="pln">log</span>

欢迎转载本文:Linux ln命令
喜欢 (0)
[ld]
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址