学习了创建文件和目录的命令mkdir ,今天学习一下linux中删除文件和目录的命令:rm命令。rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除。对于链接文件,只是删除了链接,原有文件均保持不变。
rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf)。所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西,操作时保持高度清醒的头脑。
1.命令格式:
rm [选项] 文件…
2.命令功能:
删除一个目录中的一个或多个文件或目录,如果没有使用-r选项,则rm不会删除目录。如果使用rm来删除文件,通常仍可以将该文件恢复原状。
3.命令参数:
-i, --interactive 进行交互式删除
-f, --force 忽略不存在的文件,从不给出提示
-r, -R, --recursive 指示rm将参数中列出的全部目录和子目录均递归地删除
4.命令实例:
实例一:创建text1文件后删除text1,系统会先询问是否删除
命令:
rm 文件名
输出:
[BEGIN] 2015/12/24 23:44:26 [[email protected] ~]# [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog log.txt text [[email protected] ~]# touch text1 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog log.txt text text1 [[email protected] ~]# rm text1 rm: remove regular empty file `text1‘? y [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog log.txt text [END] 2015/12/24 23:44:52
输入rm text1命令后,系统会询问是否删除,输入y(yes)后就会删除文件,不想删除则数据n(no)。
实例二:强行删除新建的text1,系统不再提示。
命令:
rm -f text1
输出:
[BEGIN] 2015/12/24 23:48:02 [[email protected] ~]# [[email protected] ~]# touch text1 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog log.txt text text1 [[email protected] ~]# rm -f text1 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog log.txt text [END] 2015/12/24 23:48:32
实例三:删除任何新建的.txt文件;删除前逐一询问确认
命令:
rm -i *.txt
输出:
[BEGIN] 2015/12/24 23:52:04 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog text [[email protected] ~]# [[email protected] ~]# touch text1.txt text2.txt text3.txt [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog text text1.txt text2.txt text3.txt [[email protected] ~]# rm -i *.txt rm: remove regular empty file `text1.txt‘? y rm: remove regular empty file `text2.txt‘? y rm: remove regular empty file `text3.txt‘? y [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog text [END] 2015/12/24 23:52:36
实例四:将 text子目录及子目录中所有档案删除
命令:
rm -r text
输出:
[BEGIN] 2015/12/24 23:58:34 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog text [[email protected] ~]# tree /root/text/ /root/text/ └── init.d ├── dnsmasq ├── firstboot ├── functions ├── haldaemon ├── halt ├── htcacheclean ├── httpd ├── ip6tables ├── iptables ├── irqbalance ├── mdmonitor ├── messagebus ├── NetworkManager ├── portreserve ├── postfix ├── psacct ├── quota_nld ├── udev-post └── xinetd 1 directory, 19 files [[email protected] ~]# rm -r text rm: descend into directory `text‘? y rm: descend into directory `text/init.d‘? y rm: remove regular file `text/init.d/dnsmasq‘? y rm: remove regular file `text/init.d/postfix‘? y rm: remove regular file `text/init.d/htcacheclean‘? y rm: remove regular file `text/init.d/portreserve‘? y rm: remove regular file `text/init.d/httpd‘? y rm: remove regular file `text/init.d/messagebus‘? y rm: remove regular file `text/init.d/quota_nld‘? y rm: remove regular file `text/init.d/haldaemon‘? y rm: remove regular file `text/init.d/halt‘? y rm: remove regular file `text/init.d/mdmonitor‘? y rm: remove regular file `text/init.d/xinetd‘? y rm: remove regular file `text/init.d/iptables‘? y rm: remove regular file `text/init.d/ip6tables‘? y rm: remove regular file `text/init.d/NetworkManager‘? y rm: remove regular file `text/init.d/psacct‘? y rm: remove regular file `text/init.d/irqbalance‘? y rm: remove regular file `text/init.d/firstboot‘? y rm: remove regular file `text/init.d/functions‘? y rm: remove regular file `text/init.d/udev-post‘? y rm: remove directory `text/init.d‘? y rm: remove directory `text‘? y [[email protected] ~]# [END] 2015/12/24 23:59:35
实例五:rm -rf 命令会将 text 子目录及子目录中所有档案删除,并且不用一一确认
命令:
rm -rf text
输出:
[BEGIN] 2015/12/25 0:05:00 [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog text [[email protected] ~]# tree /root/text /root/text └── init.d ├── haldaemon ├── halt ├── htcacheclean ├── httpd ├── NetworkManager ├── portreserve ├── postfix ├── psacct ├── quota_nld ├── rdisc ├── restorecond ├── rngd ├── rsyslog ├── wdaemon ├── winbind ├── wpa_supplicant └── xinetd 1 directory, 17 files [[email protected] ~]# rm -rf text [[email protected] ~]# ls anaconda-ks.cfg install.log install.log.syslog [END] 2015/12/25 0:05:37
时间: 2024-12-26 09:13:16