rm

命令功能:

删除文件或目录


语法:

rm [-fir]文件或目录


参数:

-f :忽略不存在的文件,不会出现警告信息

-i :互动模式,在删除前会询问用户是否操作

-r :递归删除,最常用在目录的删除


命令实践:

[root@yubinghost~]# alias

alias cp='cp -i'

alias l.='ls -d.* --color=tty'

alias ll='ls -l--color=tty'

alias ls='ls--color=tty'

alias mv='mv -i'

alias rm='rm -i'

aliaswhich='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@yubinghost~]# unalias rm     取消系统命令别名

[root@yubinghost~]# alias

alias cp='cp -i'

alias l.='ls -d.* --color=tty'

alias ll='ls -l--color=tty'

alias ls='ls--color=tty'

alias mv='mv -i'

aliaswhich='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@yubinghost~]# touch yubing.txt

[root@yubinghost~]# ll

total 8476

-rw------- 1root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1root root 3422452 Apr  8 10:57etc.tar.bz2

-rw-r--r-- 1root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1root root      18 Apr  5 14:43 o1

-rw-r--r-- 1root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1root root      18 Apr  5 14:47 test.txt

-rw-r--r-- 1root root       0 Apr 11 14:27 yubing.txt

[root@yubinghost~]# rm yubing.txt        删除文件

[root@yubinghost~]# ll

total 8476

-rw------- 1root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1 rootroot 3422452 Apr  8 10:57 etc.tar.bz2

-rw-r--r-- 1root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1root root      18 Apr  5 14:43 o1

-rw-r--r-- 1root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1root root      18 Apr  5 14:47 test.txt

[root@yubinghost~]# touch yubing.txt

[root@yubinghost~]# rm -i yubing.txt     -i互动

rm: removeregular empty file `yubing.txt'? y

[root@yubinghost ~]# alias rm='rm -i'  增加命令别名

[root@yubinghost~]# alias

alias cp='cp -i'

alias l.='ls -d.* --color=tty'

alias ll='ls -l--color=tty'

alias ls='ls--color=tty'

alias mv='mv -i'

alias rm='rm -i'

aliaswhich='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@yubinghost~]# touch yubing.txt

[root@yubinghost~]# rm yubing.txt

rm: removeregular empty file `yubing.txt'? n

[root@yubinghost~]# rm -f yubing.txt    -f强制,没有出现提示

[root@yubinghost~]#

[root@yubinghost~]# mkdir yubing      创建目录

[root@yubinghost~]# rm yubing        

rm: cannotremove directory `yubing': Is a directory    提示是目录,无法删除

[root@yubinghost~]# rm -r yubing        加上-r参数,可以删除目录了

rm: remove directory`yubing'? y

[root@yubinghost~]# ll

total 8476

-rw------- 1root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1root root 3422452 Apr  8 10:57etc.tar.bz2

-rw-r--r-- 1root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1root root      18 Apr  5 14:43 o1

-rw-r--r-- 1root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1root root      18 Apr  5 14:47 test.txt

[root@yubinghost~]# mkdir -p /yu/bing/txt  创建目录及子目录

[root@yubinghost~]# rm -rv /yu/bing/txt  

rm: removedirectory `/yu/bing/txt'? y

removeddirectory: `/yu/bing/txt'   过程显示,直接将目录及其字母全部删除了

[root@yubinghost~]# ll

total 8476

-rw------- 1root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1root root 3422452 Apr  8 10:57etc.tar.bz2

-rw-r--r-- 1root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1root root      18 Apr  5 14:43 o1

-rw-r--r-- 1root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1root root      18 Apr  5 14:47 test.txt

rm: cannot lstat`yuing': No such file or directory  因为此文件不存在

[root@yubinghost~]# rm -f yubing    

[root@yubinghost~]#          没有出提示

[root@yubinghost~]# mkdir -p yu/bing

[root@yubinghost~]# mkdir -p yu/bing

[root@yubinghost~]# ls

anaconda-ks.cfg  install.log install.log.syslog  o1  oldboy.txt test.txt  yu

[root@yubinghost~]# rm yu

rm: cannotremove directory `yu': Is a directory         提示是目录无法删除

[root@yubinghost~]# rm -rf yu     -r还是删除了

[root@yubinghost~]# ls

anaconda-ks.cfg  install.log install.log.syslog  o1  oldboy.txt test.txt

[root@yubinghost ~]# mkdir yubing       创建目录

[root@yubinghost ~]# touch yubing/yubing.txt   在目录中创建个文件

[root@yubinghost ~]# cd yubing; ll

total 0

-rw-r--r-- 1 root root 0 Apr 11 12:05yubing.txt

[root@yubinghost yubing]# cd

[root@yubinghost ~]# rmdir yubing

rmdir: yubing: Directory not empty  目录不为空,无法使用rmdir删除

[root@yubinghost ~]#

[root@yubinghost ~]# rm -r yubing

rm: descend into directory `yubing'? y

rm: remove regular empty file`yubing/yubing.txt'? y

rm: remove directory `yubing'? y     一步步逐一删除了