linux rm命令

本文详细介绍了在Linux终端中使用Python3的rm命令时,如何通过-i选项实现交互式删除、-r选项进行递归删除,以及避免因文件名特殊字符导致的问题,如-开头的文件和空格。
摘要由CSDN通过智能技术生成

-f 就是force 的意思,忽略不存在的文件,不会出现警告信息
-i:互动模式,就是在删除操作前会询问
-r:递归删除,很危险

[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# pwd
/new_my
[root@python3 new_my]# 
[root@python3 new_my]# ll
总用量 244
drwxr-xr-x. 118 root root 12288 1月  14 22:29 etc
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   2 root root   190 1月  16 15:04 new_bash_cp
-rw-r--r--.   2 root root   190 1月  16 15:04 new_bash_cp_lLink
lrwxrwxrwx.   1 root root    11 1月  14 22:45 new_bash_cp_sLink -> new_bash_cp
-rw-r--r--.   1 root root   190 1月  16 15:18 new_bash_cp_sLink_1
lrwxrwxrwx.   1 root root    11 1月  16 15:21 new_bash_cp_sLink_2 -> new_bash_cp
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# # 带参数i,在删除相关文件时会有相关的询问
[root@python3 new_my]# rm -i new_bash_cp
rm:是否删除普通文件 "new_bash_cp"?y
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# ll
总用量 240
drwxr-xr-x. 118 root root 12288 1月  14 22:29 etc
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 new_bash_cp_lLink
lrwxrwxrwx.   1 root root    11 1月  14 22:45 new_bash_cp_sLink -> new_bash_cp
-rw-r--r--.   1 root root   190 1月  16 15:18 new_bash_cp_sLink_1
lrwxrwxrwx.   1 root root    11 1月  16 15:21 new_bash_cp_sLink_2 -> new_bash_cp
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# # 通配符*  ,其中*代表0到无穷的字符,将相关开头的文件名全部删除
[root@python3 new_my]# rm -i new_bash_cp_*
rm:是否删除普通文件 "new_bash_cp_lLink"?y
rm:是否删除符号链接 "new_bash_cp_sLink"?y
rm:是否删除普通文件 "new_bash_cp_sLink_1"?y
rm:是否删除符号链接 "new_bash_cp_sLink_2"?y
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# ll
总用量 232
drwxr-xr-x. 118 root root 12288 1月  14 22:29 etc
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# # rm删除不了非空的目录
[root@python3 new_my]# rm ./etc
rm: 无法删除"./etc": 是一个目录
[root@python3 new_my]# 
[root@python3 new_my]# # 使用递归删除
[root@python3 new_my]# rm -r ./etc
rm:是否进入目录"./etc"? y
rm:是否进入目录"./etc/pkcs11"? y
rm:是否删除目录 "./etc/pkcs11/modules"?yy^H
rm:是否删除目录 "./etc/pkcs11"?y
rm:是否进入目录"./etc/latrace.d"? y
rm:是否删除普通文件 "./etc/latrace.d/syslog.conf"?y
rm:是否删除普通文件 "./etc/latrace.d/libio.conf"?y
rm:是否删除普通文件 "./etc/latrace.d/wait.conf"?^C
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# # 使用-r操作时,默认自带-i的与客户交互功能,所以我们需要一直按y才能删除
[root@python3 new_my]# # 如果我们确定要删除这个目录,而不用询问
[root@python3 new_my]# # 在命令行前加\rm  可以忽略alias指定的参数
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# \rm -r ./etc
[root@python3 new_my]# ll
总用量 220
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 

文件名最好不要使用-开头,因为在-后面接的是参数

root@python3 new_my]# ll
总用量 220
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# #创建一个名为-aa-的文件
[root@python3 new_my]# touch -aa-
touch:无效选项 -- -
请尝试执行"touch --help"来获取更多信息。
[root@python3 new_my]# touch ./-aa-.txt
[root@python3 new_my]# ll
总用量 220
-rw-r--r--.   1 root root     0 1月  16 16:15 -aa-.txt
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 
[root@python3 new_my]# 现在删除这个文件
-bash: 现在删除这个文件: command not found
[root@python3 new_my]# 
[root@python3 new_my]# rm -aa-.txt 
rm:无效选项 -- a
尝试使用"rm ./-aa-.txt"删除文件"-aa-.txt"。
请尝试执行"rm --help"来获取更多信息。
[root@python3 new_my]# 
[root@python3 new_my]# 
[root@python3 new_my]# rm ./-aa-.txt 
rm:是否删除普通空文件 "./-aa-.txt"?y
[root@python3 new_my]# ll
总用量 220
drwxr-xr-x. 118 root root 12288 1月  13 08:53 etc2
drwxr-xr-x.   2 root root  4096 1月  16 15:28 haha
-rw-r--r--.   1 root root   190 1月  16 15:04 u_new_bash_cp
-rw-r--r--.   1 root root   176 1月  16 15:01 u_new_bash_cp_2
-rw-r--r--.   1 root root   176 1月  16 15:03 u_new_bash_cp_3
-rw-r--r--.   1 root root 96768 1月  16 15:43 wtmp
-rw-rw-r--.   1 root utmp 95232 1月  13 14:21 wtmp2
[root@python3 new_my]# 

如上我们可以知道,rm -aa- 被系统误判
其实也可以这样执行命令 rm – -aa-

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值