每天一个Linux命令:rm(5)

rm

rm命令可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均删除掉。对于链接文件,只是删除整个链接文件,而原有文件保持不变

注意:使用rm命令要格外小心。因为一旦删除了一个文件,就无法再恢复它。所以,在删除文件之前,最好再看一下文件的内容,确定是否真要删除。rm命令可以用-i选项,这个选项在使用文件扩展名字符删除多个文件时特别有用。使用这个选项,系统会要求你逐一确定是否要删除。这时,必须输入y并按Enter键,才能删除文件。如果仅按Enter键或其他字符,文件不会被删除。

格式

rm [选项] [文件...]

参数选项

参数 备注
-d 直接把欲删除的目录的硬连接数据删除成0,删除该目录
-f 强制删除文件或目录;
-i 删除已有文件或目录之前先询问用户;
-r或-R 递归处理,将指定目录下的所有文件与子目录一并处理;
--preserve-root 不对根目录进行递归操作;
-v 显示指令的详细执行过程。

实例

  • 删除文件file,系统会先询问是否删除

    命令:**rm 文件名 **

    [root@VM_0_9_centos ~]# rm testFile1 
    rm: remove regular empty file testFile1?. y
    
  • 强行删除file,系统不再提示

    命令:**rm -f log1.log **

    [root@VM_0_9_centos ~]# touch log1.log
    [root@VM_0_9_centos ~]# rm -f log1.log
    
  • 删除任何.log文件;删除前逐一询问确认

    命令:**rm -i *.log **

    [root@VM_0_9_centos ~]# touch log1.log
    [root@VM_0_9_centos ~]# touch log2.log
    [root@VM_0_9_centos ~]# rm -i *.log
    rm: remove regular empty file log1.log?. y
    rm: remove regular empty file log2.log?. y
    
  • 将 test目录及子目录中所有档案删除

    命令:rm -r test

    [root@VM_0_9_centos ~]# mkdir test
    [root@VM_0_9_centos ~]# cd test
    [root@VM_0_9_centos test]# touch file1
    [root@VM_0_9_centos test]# touch file2
    [root@VM_0_9_centos test]# touch file3
    [root@VM_0_9_centos test]# rm -r ../test
    rm: descend into directory ./test?. y
    rm: remove regular empty file ./test/file3?. y
    rm: remove regular empty file ./test/file2?. y
    rm: remove regular empty file ./test/file1?. y
    rm: remove directory ./test?. y
    
  • rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认

    命令:**rm -rf test2 **

    同上,只是没有确认删除提示
    
  • 通配符匹配删除,删除文件名以wuzhazha开头的文件

    命令:**rm wuzhazha **

    [root@VM_0_9_centos ~]# touch wuzhazha.txt
    [root@VM_0_9_centos ~]# touch wuzhazha1.txt
    [root@VM_0_9_centos ~]# touch wuzhazha2.txt
    [root@VM_0_9_centos ~]# rm wuzhazha*
    rm: remove regular empty file wuzhazha1.txt?. y
    rm: remove regular empty file wuzhazha2.txt?. y
    rm: remove regular empty file wuzhazha.txt?. y
    

参考

原文地址:https://www.cnblogs.com/DiDi516/p/11741649.html