Linux 第一周学习笔记 (16)
2.9.RM 命令
rm 命令 (remove): 可以删除一个目录中的一个或多个文件或目录, 也可以将某个目录及其下属的所有文件及其子目录均删除掉;
rm -f
命令
:
表示强制删除, 不在询问是否删除, 而是直接删除;
rm -r
命令: 删除目录 (因为
rm
命令不能直接删除目录, 要加参数
-r
);
rm
-
rf
命令: 强制删除, 不在询问是否删除目录;
rm
-
rfv
命令: 可视化强制删除目录, 不在询问是否删除目录;
"!": 加入 "!" 执行最的一次命令, 如:!tree 执行最近的一次 tree 命令;
history 命令: h 用于显示指定数目的指令命令, 读取历史命令文件中的目录到历史命令缓冲区和将历史命令缓冲区中的目录写入命令文件, 该命令单独使用时, 仅显示历史命令.
--------------------------------------------------------------------------------
rm 删除文件:
[root@daizhihong01 ~]# tree /tmp/daizhihong(查看有哪些文件)
/tmp/daizhihong
└── 01
└── 02
├── 03
│
└──
11.txt
└── 11.txt
3 directories, 2 files
[root@daizhihong01 ~]# rm /tmp/daizhihong/01/02/03/11.txt(删除 / 03 / 文件夹下的 11.txt 的文件)
rm
: 是否删除普通空文件
"/tmp/daizhihong/01/02/03/11.txt"
?
y
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
└── 02
├── 03
└── 11.txt
3 directories,
1 file-----------------------------------------------------------------------------------------------
rm-f 命令删除文件:
因为 rm 命令删除文件的时候每一次都会询问 "rm: 是否删除普通空文件"/tmp/daizhihong/01/02/03/11.txt"?y", 如果在以后的工作当中删除的文件次数比较多的话是十分的麻烦, 所以在这里我们可以加入 - f 参数表示强制删除不在询问
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
└── 02
├── 03
└── 11.txt
3 directories, 1 file
[root@daizhihong01 ~]# rm -f /tmp/daizhihong/01/02/11.txt(执行时就不在询问直接执行)
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
└── 02
└── 03
3 directories,
0 files-----------------------------------------------------------------------------------------------
rm-r 命令删除目录:
因为
rm
命令不能直接删除目录, 要加参数
-r
才能删除
[root@daizhihong01 ~]# rm /tmp/daizhihong/01/02/03/
rm:
无法删除
"/tmp/daizhihong/01/02/03/":
是一个目录
以上实验是未加 - r 参数, 所以提示错误 "rm: 无法删除"/tmp/daizhihong/01/02/03/": 是一个目录"
rm-r 命令:
[root@daizhihong01 ~]# rm -r /tmp/daizhihong/01/02/03/
rm
: 是否删除目录
"/tmp/daizhihong/01/02/03/"
?
y
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
└── 02
2 directories, 0 files
以上实验 / 03 / 目录就直接被删除
rm-rf 命令:
[root@daizhihong01 ~]# rm -rf /tmp/daizhihong/01/02/
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
1 directory, 0 files
以上实验加入 f 参数在执行时就不在询问直接执行删除
--------------------------------------------------------------------------------------------
rm-rfv 命令:
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
└── 02
└── 11.txt
2 directories,
1 file[root@daizhihong01~]#rm - rfv / tmp / daizhihong / 01 / 02 /
已删除 "/tmp/daizhihong/01/02/11.txt"
已删除目录:"/tmp/daizhihong/01/02/"
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
1 directory, 0 files
以上实验加入 V 参数在执行时是可视化的, 还以看出在删除一个目录的时候他需要先删除目录下面的文件或者是子目录然后才删除这个目录
------------------------------------------------------------------------------------------------
使用 f 参数删除一个不存在的目录:
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
1 directory, 0 files
[root@daizhihong01 ~]# rm -r /tmp/daizhihong/01/02/
rm:
无法删除
"/tmp/daizhihong/01/02/":
没有那个文件或目录
[root@daizhihong01 ~]# rm -rf /tmp/daizhihong/01/02/
[root@daizhihong01 ~]#
以上实验中这个目录 "/tmp/daizhihong/01/02/" 是不存在, 使用 rm -r 命令删除时提示 "rm: 无法删除"/tmp/daizhihong/01/02/": 没有那个文件或目录", 但是加入 - f 参数以后, 就不会报错, 所以在实际工作当中使用一定要注意这个问题, 以免造成一些不良后果
--------------------------------------------------------------------------------------------
--------------------------------------- 使用技巧 --------------------------------------------
"!" 使用: 加入 "!" 执行最的一次命令:
[root@daizhihong01 ~]# tree /tmp/daizhihong
/tmp/daizhihong
└── 01
1 directory, 0 files
[root@daizhihong01 ~]# !tree
tree /tmp/daizhihong
/tmp/daizhihong
└── 01
1 directory,
0 files------------------------------------------------------------------------------------------------
history 命令: 显示历史命令 (方便查询使用过的命令):
[root@daizhihong01 ~]# history
250 tree /tmp/daizhihong
251 rm -rfv /tmp/daizhihong/01/02/03/04/
252 rm -rfv /tmp/daizhihong/01/02/03/
253 tree /tmp/daizhihong
254 rm -rfv /tmp/daizhihong/01/02/
255 tree /tmp/daizhihong
256 rm -rf /tmp/daizhihong/01/02/
257 rm -r /tmp/daizhihong/01/02/
258 tree /tmp/daizhihong
259 history
[root@daizhihong01 ~]#
以上实验由于命令过多节选一部分
来源: http://www.bubuko.com/infodetail-2477909.html