请注意,本文编写于 705 天前,最后修改于 654 天前,其中某些信息可能已经过时。
rm 是 remove 的缩写,rm命令用于删除文件/文件夹。rm命令删除文件/文件夹后,删除的文件/文件夹无法恢复,请谨慎使用rm命令。
语法:
rm [选项] [目标文件/目标目录]
选项:
-r, -R, --recursive 递归删除,删除指定目录中的全部文件和子目录,此选项慎用
-f, --force 强制删除,忽略不存在的文件,从不给出提示。
...
# 更多的选项可以用 rm --help 来查看。
实例:
# 实例一:删除一个文件
[email protected]:~/test# rm test2.txt
[email protected]:~/test# ll
total 20
drwxr-xr-x 5 root root 4096 7月 9 11:30 ./
drwx------ 23 root root 4096 7月 9 08:02 ../
drwxr-xr-x 2 root root 4096 7月 9 10:59 test/
drwxr-xr-x 5 root root 4096 7月 9 11:02 test2/
drwxr-xr-x 2 root root 4096 7月 9 07:52 .test2/
-rw-r--r-- 1 root root 0 7月 9 07:52 .test2.txt
-rw-r--r-- 1 root root 0 7月 9 10:54 test3.txt
-rw-r--r-- 1 root root 0 7月 9 07:47 test4.txt
# 实例二:删除多个文件
[email protected]:~/test# rm test3.txt test4.txt
[email protected]:~/test# ll
total 20
drwxr-xr-x 5 root root 4096 7月 9 11:31 ./
drwx------ 23 root root 4096 7月 9 08:02 ../
drwxr-xr-x 2 root root 4096 7月 9 10:59 test/
drwxr-xr-x 5 root root 4096 7月 9 11:02 test2/
drwxr-xr-x 2 root root 4096 7月 9 07:52 .test2/
-rw-r--r-- 1 root root 0 7月 9 07:52 .test2.txt
# 实例三:删除一个目录
[email protected]:~/test# rm -rf test4
[email protected]:~/test# ll
total 28
drwxr-xr-x 7 root root 4096 7月 9 11:33 ./
drwx------ 23 root root 4096 7月 9 08:02 ../
drwxr-xr-x 2 root root 4096 7月 9 10:59 test/
drwxr-xr-x 2 root root 4096 7月 9 11:32 test2/
drwxr-xr-x 2 root root 4096 7月 9 07:52 .test2/
-rw-r--r-- 1 root root 0 7月 9 07:52 .test2.txt
drwxr-xr-x 2 root root 4096 7月 9 10:58 test5/
drwxr-xr-x 2 root root 4096 7月 9 09:30 test6/
# 实例四:删除多个目录
[email protected]:~/test# rm -rf test5 test6
[email protected]:~/test# ll
total 20
drwxr-xr-x 5 root root 4096 7月 9 11:34 ./
drwx------ 23 root root 4096 7月 9 08:02 ../
drwxr-xr-x 2 root root 4096 7月 9 10:59 test/
drwxr-xr-x 2 root root 4096 7月 9 11:32 test2/
drwxr-xr-x 2 root root 4096 7月 9 07:52 .test2/
-rw-r--r-- 1 root root 0 7月 9 07:52 .test2.txt