无忧老猪 2019-09-24
你可能有时需要在 Linux 上创建或者删除符号链接。如果有,你知道该怎样做吗?之前你做过吗?你踩坑没有?如果你踩过坑,那没什么问题。如果还没有,别担心,我们将在这里帮助你。
使用 rm
和 unlink
命令就能完成移除(删除)符号链接的操作。
符号链接(symlink)又称软链接,它是一种特殊的文件类型,在 Linux 中该文件指向另一个文件或者目录。它类似于 Windows 中的快捷方式。它能在相同或者不同的文件系统或分区中指向一个文件或着目录。
符号链接通常用来链接库文件。它也可用于链接日志文件和挂载的 NFS(网络文件系统)上的文件夹。
rm 命令 被用来移除文件和目录。它非常危险,你每次使用 rm
命令的时候要非常小心。
unlink
命令被用来移除特殊的文件。它被作为 GNU Gorutils 的一部分安装了。
rm
命令是在 Linux 中使用最频繁的命令,它允许我们像下列描述那样去移除符号链接。
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> symlinkfile</span>
始终将 rm
命令与 -i
一起使用以了解正在执行的操作。
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i symlinkfile1</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkfile1</span><span class="pun">’?</span><span class="pln"> y</span>
它允许我们一次移除多个符号链接:
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i symlinkfile2 symlinkfile3</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkfile2</span><span class="pun">’?</span><span class="pln"> y</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkfile3</span><span class="pun">’?</span><span class="pln"> y</span>
这像移除符号链接文件那样。使用下列命令移除符号链接目录。
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i symlinkdir</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">’?</span><span class="pln"> y</span>
使用下列命令移除多个符号链接目录。
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i symlinkdir1 symlinkdir2</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkdir1</span><span class="pun">’?</span><span class="pln"> y</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove symbolic </span><span class="kwd">link</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkdir2</span><span class="pun">’?</span><span class="pln"> y</span>
如果你在结尾增加 /
,这个符号链接目录将不会被删除。如果你加了,你将得到一个错误。
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">i symlinkdir</span><span class="pun">/</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> cannot remove </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">/’:</span><span class="pln"> </span><span class="typ">Is</span><span class="pln"> a directory</span>
你可以增加 -r
去处理上述问题。但如果你增加这个参数,它将会删除目标目录下的内容,并且它不会删除这个符号链接文件。(LCTT 译注:这可能不是你的原意。)
<span class="com">#</span><span class="pln"> </span><span class="kwd">rm</span><span class="pln"> </span><span class="pun">-</span><span class="pln">ri symlinkdir</span><span class="pun">/</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> descend into directory </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">/’?</span><span class="pln"> y</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove regular </span><span class="kwd">file</span><span class="pln"> </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">/</span><span class="pln">file4</span><span class="pun">.</span><span class="pln">txt</span><span class="pun">’?</span><span class="pln"> y</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> remove directory </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">/’?</span><span class="pln"> y</span>
<span class="kwd">rm</span><span class="pun">:</span><span class="pln"> cannot remove </span><span class="pun">‘</span><span class="pln">symlinkdir</span><span class="pun">/’:</span><span class="pln"> </span><span class="typ">Not</span><span class="pln"> a directory</span>
unlink
命令删除指定文件。它一次仅接受一个文件。
删除符号链接文件:
<span class="com">#</span><span class="pln"> </span><span class="kwd">unlink</span><span class="pln"> symlinkfile</span>
删除符号链接目录:
<span class="com">#</span><span class="pln"> </span><span class="kwd">unlink</span><span class="pln"> symlinkdir2</span>