深入解析Linux常用命令–rm
1.概述
rm也是我们经常用到的一个Linux命令,常用于文件和文件夹的删除,rm取自于remove。官方解释为:“Remove files or directories”,即移除文件和目录。
2.各选项解释
1)-f
-f选项表示:不管文件是否存在均不出现提示。
[root@smart linux]# rm -f a.t
[root@smart linux]# rm -f b.t
2)-r/-R
-r/-R表示:递归删除文件或目录
[root@smart linux]# rm -r a
rm: remove regular file `a'? y
[root@smart linux]# rm -r g
rm: descend into directory `g'? y
rm: remove directory `g/abcde'? y
rm: remove directory `g'? y
[root@smart linux]# rm -R e
rm: descend into directory `e'? y
rm: remove regular file `e/b.c'? y
rm: remove directory `e'? y
[root@smart linux]# ls
A a.txt B b.c~ c f kl.txt rm.txt s.txt
abcde b b.c b.txt d h k.txt ss test
[root@smart linux]#
3)-i
-i表示:在删除文件前,提示确认信息。一般情况下,Linux都对这个命令重定义为这个选项。即默认情况下删除文件都会存在提示。
[root@smart linux]# rm -i a.t
rm: cannot remove `a.t': No such file or directory
[root@smart linux]# rm a.t
rm: cannot remove `a.t': No such file or directory
[root@smart linux]#
4)-v
-v表示:执行后显示正在进行的动作,同其他命令。
[root@smart linux]# rm -v a.txt b.txt
rm: cannot remove `a.txt': No such file or directory
rm: cannot remove `b.txt': No such file or directory
[root@smart linux]# rm -v k.txt
rm: remove regular file `k.txt'? y
removed `k.txt'
[root@smart linux]# rm -fv s.txt
removed `s.txt'
[root@smart linux]#
5)–preserve-root
–preserve-root表示:保护根目录(/)路径。此选项可以避免误操作删除根目录,导致整个系统崩溃的风险,推荐使用。
[root@smart linux]# rm --preserve-root /
rm: cannot remove `/': Is a directory
[root@smart linux]# rm --preserve-root / b
rm: cannot remove `/': Is a directory
rm: remove regular file `b'? y
[root@smart linux]# ls
A abcde B b.c b.c~ c d f h kl.txt rm.txt ss test
[root@smart linux]# ls /
bin cgroup etc lib media mnt opt root selinux sys usr
boot dev home lost+found misc net proc sbin srv tmp var
Enjoy!
2017/12/24