在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