1.12- 删除文件(和目录) rm命令详解

本文详细介绍了Linux系统下rm命令的使用方法,包括删除文件、硬链接及软连接的不同行为,以及如何利用通配符批量删除文件和目录。通过具体实例展示了rm命令在不同情况下的应用。

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

在Linux中,删除过程称为移除,bash shell中移除文件的命令是 rm
rm取remove之意
rm命令的用法如下:
1、用法一:rm 文件名,删除文件
1> 删除普通文件
rm命令删除文件默认会提示是否需要删除文件,bash shell中没有回收站,一旦删除文件则无法还原。

[root@hadoop tmp]# rm efile1
rm:是否删除普通空文件 "efile1"?y
[root@hadoop tmp]# ls -li efile*
16811789 -rw-r--r--. 1 root root 0 8月   8 21:40 efile2
16811790 -rw-r--r--. 1 root root 0 8月   8 21:40 efile3
16811791 -rw-r--r--. 1 root root 0 8月   8 21:40 efile4
16811792 -rw-r--r--. 1 root root 0 8月   8 21:40 efile5
16811793 -rw-r--r--. 1 root root 0 8月   8 21:41 efile6

2> 删除硬链接文件的源文件
删除硬链接文件的源文件,硬链接文件仍然可用

[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file1
16811483 -rw-r--r--. 2 root root  7 8月  10 18:48 test_file11
16811794 -rw-r--r--. 1 root root  7 8月  11 12:56 test_file2
16811788 lrwxrwxrwx. 1 root root 10 8月  11 13:29 test_file22 -> test_file2
[root@hadoop tmp]# rm test_file1 -f
[root@hadoop tmp]# cat test_file11
12
123

3> 删除软连接文件的源文件
删除软连接文件的源文件,软连接文件报出 没有那个文件或目录错误

[root@hadoop tmp]# ls -li test_file*
16811483 -rw-r--r--. 1 root root  7 8月  10 18:48 test_file11
16811794 -rw-r--r--. 1 root root  7 8月  11 12:56 test_file2
16811788 lrwxrwxrwx. 1 root root 10 8月  11 13:29 test_file22 -> test_file2
[root@hadoop tmp]# rm test_file2 -f
[root@hadoop tmp]# cat test_file22
cat: test_file22: 没有那个文件或目录

2、用法二:rm 文件名,删除文件与通配符一起使用,批量删除文件

[root@hadoop tmp]# rm test_file* -f
[root@hadoop tmp]# ls -li test_file*
ls: 无法访问test_file*: 没有那个文件或目录

3、用法三:rm 目录名,删除目录
rm 除了可以移除文件,还可以移除目录
使用 -r 参数地柜移除目录中的文件,然后再移除目录本身
-f参数强制删除,不做删除提示

dafu6是一个非空目录,下面有文件 file1 和 file2

[root@hadoop tmp]# rm dafu6 -rf
[root@hadoop tmp]# ll |grep dafu
drwxr-xr-x. 2 root root  6 8月   7 23:47 dafu2
drwxr-xr-x. 3 root root 19 8月  10 19:08 dafu3
drwxr-xr-x. 2 root root 45 8月  10 19:10 dafu4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值