跳转至

ln 命令


2015-01-23 by dongnan

举个栗子

创建文件软连接

ln -s /dev/urandom /dev/random

谨慎删除目录软连接

创建目录软连接

ln -s /root/test/ .

查看软连文件

ll

lrwxrwxrwx 1 root root  11 2015-06-01 17:09:16 test -> /root/test/

ls test/

cron.log  txt

删除目录软连接,谨慎使用 tab键自动补齐路径,否则将删除源目录中的文件

rm -rf test/  # 注意"/"

验证破坏性

软连接文件仍然存在

ll
....
lrwxrwxrwx 1 root root  11 2015-06-01 17:20:14 test -> /root/test/

源目录中的文件却被删除

ll /root/test/

total 0

小结

所以删除目录软连接时要特别注意,谨慎使用 tab键自动补齐路径,或者保证软连接后没有斜杠。

rm -rf test

帮助

man ln

NAME
       ln - make links between files

SYNOPSIS
       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)

DESCRIPTION
       In  the 1st form, create a link to TARGET with the name LINK_NAME.  In the 2nd form, create a link to TARGET in the current directory.  In the 3rd and 4th forms, create links to
       each TARGET in DIRECTORY.  Create hard links by default, symbolic links with --symbolic.  When creating hard links, each TARGET must exist.  Symbolic links  can  hold  arbitrary
       text; if later resolved, a relative link is interpreted in relation to its parent directory.


     -s, --symbolic
              make symbolic links instead of hard links

欢迎关注微信公众号: 运维录

Back to top