linux软连接原文件删除,linux下删除文件夹的软链接时注意千万不能在后面加反斜杠,千万不要用强制删除,否则下面2种场景,你会把源文件删除,要闯祸的...

今天遇到一个坑,自己在子目录下创建了父目录的软链接,导致可以无限循环进入父目录

[[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值