命令简介:
该命令用来删除Linux系统中的文件或目录(文件夹)。
命令语法:
rm [-dfirv][--help][--version][文档或目录...]
参数:
短选项
长选项
含义
-f
--force
忽略不存在的文件,强制删除,无任何提示。
-i
--interactive
进行交互式删除
-r
--recursive
递归式删除(本目录下)全部文件和目录
-v
--verbose
详细显示进行的步骤
格式:rm file 删除文件file,系统会先询问是否删除。
格式:rm -f file 强行删除file,系统不再提示。
格式:rm -rf dir 强行删除目录dir下的所有文件、子目录下的所有文件和目录、删除dir本身。
格式:rm -f *.LOG* 删除日志文件。
rm 文件名
删除文件,系统会先询问是否删除。
[root@localhost test1]# ls -l
总计 4
-rw-r--r-- 1 root root 56 10-26 14:31 log.log
root@localhost test1]# rm log.log
rm:是否删除 一般文件 “log.log”? y
root@localhost test1]# ls -l
总计 0[root@localhost test1]#
说明:输入rm log.log命令后,系统会询问是否删除,输入y后就会删除文件,不想删除则数据n。
rm -f 文件名
强行删除文件,系统不再提示。
[root@localhost test1]# ls -l
总计 4
-rw-r--r-- 1 root root 23 10-26 14:40 log1.log
[root@localhost test1]# rm -f log1.log
[root@localhost test1]# ls -l
总计 0[root@localhost test1]#
rm -i 文件名
删除任何文件,删除前逐一询问确认
[root@localhost test1]# ls -l
总计 8
-rw-r--r-- 1 root root 11 10-26 14:45 log1.log
-rw-r--r-- 1 root root 24 10-26 14:45 log2.log
[root@localhost test1]# rm -i *.log
rm:是否删除 一般文件 “log1.log”? y
rm:是否删除 一般文件 “log2.log”? y
[root@localhost test1]# ls -l
总计 0[root@localhost test1]#
rm -r 目录名
将子目录及子目录中所有档案删除
[root@localhost test]# ls -l
总计 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 2 root root 4096 10-26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm -r test1
rm:是否进入目录 “test1”? y
rm:是否删除 一般文件 “test1/log3.log”? y
rm:是否删除 目录 “test1”? y
[root@localhost test]# ls -l
总计 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#
rm -rf 目录名
把子目录及子目录中所有档案删除,并且不用一一确认
[root@localhost test]# rm -rf test2
[root@localhost test]# ls -l
总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#
rm -- -f
删除以 -f 开头的文件
[root@localhost test]# touch -- -f
[root@localhost test]# ls -- -f
-f[root@localhost test]# rm -- -f
rm:是否删除 一般空文件 “-f”? y
[root@localhost test]# ls -- -f
ls: -f: 没有那个文件或目录
[root@localhost test]#
也可以使用下面的操作步骤:
[root@localhost test]# touch ./-f
[root@localhost test]# ls ./-f
./-f[root@localhost test]# rm ./-f
rm:是否删除 一般空文件 “./-f”? y
[root@localhost test]#