2.9-rm命令

本文详细介绍了Linux系统中rm命令的使用方法,包括如何删除文件、强制删除、删除目录及可视强制删除等操作,同时提供了rm命令结合通配符、强制选项(-f)和递归选项(-r)的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

rm (remove) 删除,可以删除文件和目录

删除文件
先用 tree 命令查看结构,然后使用 rm 命令删除,删除时候会有提示,输入 y/n 回车后即删除

[root@evan-01 evanlinux]# ll
总用量 4
-rw-r--r-- 1 root root 15 8月   8 10:05 test.txt
[root@evan-01 evanlinux]# rm test.txt 
rm:是否删除普通文件 "test.txt"?y
[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]#

添加多个文件,删除文件夹下所有文件
rm *.txt 中 * 是通配符,意思要删除 /tmp/evanlinux/ 目录下的所有 .txt 文件
经常询问是否删除普通空文件,如果我们有一两个文件询问还好,如果要几十上百那就让人崩溃了,那么能不能强制删除,不提示呢?当然可以

[root@evan-01 evanlinux]# touch test1.txt
[root@evan-01 evanlinux]# touch test2.txt
[root@evan-01 evanlinux]# touch test3.txt
[root@evan-01 evanlinux]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月   8 10:06 test1.txt
-rw-r--r-- 1 root root 0 8月   8 10:06 test2.txt
-rw-r--r-- 1 root root 0 8月   8 10:06 test3.txt
[root@evan-01 evanlinux]# rm *.txt
rm:是否删除普通空文件 "test1.txt"?y
rm:是否删除普通空文件 "test2.txt"?y
rm:是否删除普通空文件 "test3.txt"?y
[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]# 

rm -f 强制删除

[root@evan-01 evanlinux]# touch test1.txt
[root@evan-01 evanlinux]# touch test2.txt
[root@evan-01 evanlinux]# touch test3.txt
[root@evan-01 evanlinux]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月   8 10:08 test1.txt
-rw-r--r-- 1 root root 0 8月   8 10:08 test2.txt
-rw-r--r-- 1 root root 0 8月   8 10:08 test3.txt
[root@evan-01 evanlinux]# rm -f *.txt 
[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]#

!命令 查看历史里面最近一次使用的命令
!touch 查看历史里面最近一次使用touch的命令

[root@evan-01 evanlinux]# touch test1.txt
[root@evan-01 evanlinux]# touch test2.txt
[root@evan-01 evanlinux]# touch test3.txt
[root@evan-01 evanlinux]# ll
总用量 0
-rw-r--r-- 1 root root 0 8月   8 10:08 test1.txt
-rw-r--r-- 1 root root 0 8月   8 10:08 test2.txt
-rw-r--r-- 1 root root 0 8月   8 10:08 test3.txt
[root@evan-01 evanlinux]# rm -f *.txt 
[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]# !touch
touch test3.txt
[root@evan-01 evanlinux]#

history 查看历史命令

[root@evan-01 evanlinux]# history
....(省略若干行)
 3904  2019/08/08 10:08:26 touch test1.txt
 3905  2019/08/08 10:08:27 touch test2.txt
 3906  2019/08/08 10:08:30 touch test3.txt
 3907  2019/08/08 10:08:31 ll
 3908  2019/08/08 10:08:45 rm -f *.txt
 3909  2019/08/08 10:08:47 ll
 3910  2019/08/08 10:10:26 touch test3.txt
 3911  2019/08/08 10:12:11 history

删除目录
反复输入y回车,过于麻烦

[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]# mkdir -p one/two
[root@evan-01 evanlinux]# touch one/two/test.txt
[root@evan-01 evanlinux]# tree ./
./
└── one
    └── two
        └── test.txt

2 directories, 1 file
[root@evan-01 evanlinux]# rm -r one/two/
rm:是否进入目录"one/two/"? y
rm:是否删除普通空文件 "one/two/test.txt"?y
rm:是否删除目录 "one/two/"?y
[root@evan-01 evanlinux]# ll
总用量 0
drwxr-xr-x 2 root root 6 8月   8 10:15 one
[root@evan-01 evanlinux]# 

强制删除目录

[root@evan-01 evanlinux]# ll
总用量 0
drwxr-xr-x 2 root root 6 8月   8 10:15 one
[root@evan-01 evanlinux]# rm -rf one/
[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]#

可视强制删除目录
文件刚才都删除掉了,在这再重新创建一些,然后使用 -rvf 强制可视删除

[root@evan-01 evanlinux]# ll
总用量 0
[root@evan-01 evanlinux]# mkdir -p one/two
[root@evan-01 evanlinux]# touch one/two/test.txt
[root@evan-01 evanlinux]# tree ./
./
└── one
    └── two
        └── test.txt

2 directories, 1 file
[root@evan-01 evanlinux]# rm -rvf one/two/
已删除"one/two/test.txt"
已删除目录:"one/two/"
[root@evan-01 evanlinux]# ll
总用量 0
drwxr-xr-x 2 root root 6 8月   8 10:17 one
[root@evan-01 evanlinux]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值