今天遇到一个坑,自己在子目录下创建了父目录的软链接,导致可以无限循环进入父目录
[[email protected] dir1]$ ll
total 8
-rw-rw-r-- 1 clouder clouder 4 Oct 9 17:49 a.txt
-rw-rw-r-- 1 clouder clouder 4 Oct 9 17:49 b.txt
lrwxrwxrwx 1 clouder clouder 10 Oct 9 17:49 dir1 -> /tmp/dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ cd dir1/
[[email protected] dir1]$ ls
a.txt b.txt dir1
[[email protected] dir1]$ pwd
/tmp/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1/dir1
然后想把这个软链接删除,于是在终端输入
linux终端下,常利用自动补全功能,输入部分文件名,如di,然后按tab键,自动补全,结果自己带上了后面的反斜杠;
然后 被告知这是个文件夹
[[email protected] dir1]$ rm dir1/
rm: cannot remove ‘dir1/’: Is a directory
于是我加上-rf (f表示强制r表示删除递归删除,用于删除文件夹)
结果悲剧了,当文件夹dir1的软链接dir1在原文件的子目录,则rm -rf 软链接目录也会把源文件删除!!!
[[email protected] dir1]$ rm -rf dir1/
[[email protected] dir1]$ ll
total 0
下面做一个实验,在另外一个目录dir2下创建dir1的软链接,强制删除软链接:rm -f dir1 也会把源目录删除!!!
准备源目录dir1,里面有a.txt , b.txt文件
[[email protected] tmp]$ cd dir1/
[[email protected] dir1]$ echo 'aaa'>a.txt
[[email protected] dir1]$ echo 'aaa'>b.txt
[[email protected] dir1]$ ll
total 8
-rw-rw-r-- 1 clouder clouder 4 Oct 9 17:26 a.txt
-rw-rw-r-- 1 clouder clouder 4 Oct 9 17:26 b.txt
进入目标目录/tmp/dir2,创建/tmp/dir1目录的软链接
[[email protected] dir1]$ cd ../dir2
[[email protected] dir2]$ ll
total 0
[[email protected] dir2]$ ln -s /tmp/dir1 .
[[email protected] dir2]$ ll
total 0
lrwxrwxrwx 1 clouder clouder 9 Oct 9 17:26 dir1 -> /tmp/dir1
用file命令查看文件类型,注意软链接目录没带反斜杠是链接
[[email protected] dir2]$ file dir1
dir1: symbolic link to `/tmp/dir1'
删除目录的软链接文件成功(此处因为我用命令别名alias rm=‘rm -i‘ ,所以在我本机用rm命令时,都会提示是否确认删除)
[[email protected] dir2]$ rm dir1
rm: remove symbolic link ‘dir1’? y
[[email protected] dir2]$ ls
[[email protected] dir2]$ ls /tmp/dir1/
a.txt b.txt
在创建一个软链接
[[email protected] dir2]$ ln -s /tmp/dir1/ .
[[email protected] dir2]$ ll
total 0
lrwxrwxrwx 1 clouder clouder 10 Oct 9 17:27 dir1 -> /tmp/dir1/
file命令查看dir1/ 是什么类型,软链接目录带了反斜杠就当成目录了
[[email protected] dir2]$ file dir1/
dir1/: directory
删除软链接目录提示这是一个文件夹
[[email protected] dir2]$ rm dir1/
rm: cannot remove ‘dir1/’: Is a directory
强制删除软链接目录(文件夹)没有成功
[[email protected] dir2]$ rm dir1/ -rf
[[email protected] dir2]$ ll
total 0
lrwxrwxrwx 1 clouder clouder 10 Oct 9 17:27 dir1 -> /tmp/dir1/
强制删除软链接目录提示这是一个目录
[[email protected] dir2]$ rm -f dir1/
rm: cannot remove ‘dir1/’: Is a directory
强制删除软链接文件,结果把原始文件给删了!!!
[[email protected] dir2]$ rm -f dir1
[[email protected] dir2]$ ll
total 0
[[email protected] dir2]$ ll /tmp/dir1/
total 0
原文地址:https://www.cnblogs.com/xiaozhuangAna/p/11643527.html
时间: 2024-11-05 13:34:52