跟着itlife365每天学习一个linux命令(5)之rm命令
--begin by itlife365 说明:不同平台细节可能有差别,具体需要看帮助文件,使用命令man rm
跟着itlife365每天学习一个linux命令总目录
http://itlife365.com/blog/post/linux-everyday-learn.php
**********表示代码区域
linux rm linux命令 rm命令 每日一则linux命令 rm命令详解 rm命令怎么使用 rm参数 rm命令使用注意
上一篇文章学习创建文件和目录的命令mkdir ,有创建就会有删除来对应,linux中删除文件和目录的命令 rm 命令。
rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件或目录,它也可以将某个目录及其下的所有文件及子目录均删除。对于链接文件,只是删除了链接,原有文件均保持不变。
rm是一个危险的命令,尤其对于新手使用的时候要特别当心,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf)。所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西,操作时保持高度清醒的头脑。在生产环境比较执行rm 命令必须保持清醒,三思而后行
remove files or directories
1.rm命令格式:
rm [选项] 文件…
2.rm命令功能:
删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录。如果使用 rm 来删除文件,通常仍可以将该文件恢复原状。
3.rm命令参数:
-f, --force 忽略不存在的文件,从不给出提示。
-i, --interactive 进行交互式删除
-r, -R, --recursive 指示rm将参数中列出的全部目录和子目录均递归地删除。
-v, --verbose 详细显示进行的步骤
--help 显示此帮助信息并退出
--version 输出版本信息并退出
4.命令实例:
下面删除我们之前创建的文件和文件夹
实例一:删除文件file,系统会先询问是否删除。
命令:
rm 文件名
输出:
[root@serverp60231305290002 test]# cd testdir1
[root@serverp60231305290002 testdir1]# echo "abcd">a.txt
[root@serverp60231305290002 testdir1]# ll
total 4
-rw-r--r-- 1 root root 5 Nov 30 16:58 a.txt
[root@serverp60231305290002 testdir1]# rm a.txt
rm: remove regular file `a.txt'? y
[root@serverp60231305290002 testdir1]#
说明:
输入rm a.txt命令后,系统会询问是否删除,输入y后就会删除文件,不想删除则数据n。
实例二:强行删除,系统不再提示。
命令:
rm -f a.txt
输出:
[root@serverp60231305290002 testdir1]# echo "abcd" >a.txt
You have new mail in /var/spool/mail/root
[root@serverp60231305290002 testdir1]# cat a.txt 查看创建的文件
abcd
[root@serverp60231305290002 testdir1]# ll
total 4
-rw-r--r-- 1 root root 5 Nov 30 17:00 a.txt
[root@serverp60231305290002 testdir1]# rm -r a.txt
rm: remove regular file `a.txt'? n
[root@serverp60231305290002 testdir1]# rm -f a.txt
[root@serverp60231305290002 testdir1]# ll
total 0
[root@serverp60231305290002 testdir1]#
实例三:删除任何.txt为后缀的文件,删除前逐一询问确认
命令:
rm -i *.txt
输出:
[root@serverp60231305290002 testdir1]# echo "aaa" >a.txt
[root@serverp60231305290002 testdir1]# echo "bbb" >b.txt
[root@serverp60231305290002 testdir1]# echo "itlife35" >c.txt
[root@serverp60231305290002 testdir1]# ll
total 12
-rw-r--r-- 1 root root 4 Nov 30 17:03 a.txt
-rw-r--r-- 1 root root 4 Nov 30 17:03 b.txt
-rw-r--r-- 1 root root 9 Nov 30 17:03 c.txt
[root@serverp60231305290002 testdir1]# rm -i *.txt
rm: remove regular file `a.txt'? y
rm: remove regular file `b.txt'? y
rm: remove regular file `c.txt'? n
[root@serverp60231305290002 testdir1]# ll
total 4
-rw-r--r-- 1 root root 9 Nov 30 17:03 c.txt
[root@serverp60231305290002 testdir1]# cat c.txt
itlife35
[root@serverp60231305290002 testdir1]#
实例四:将testdir1子目录及子目录中所有档案删除
命令:
rm -r testdir1
输出:
[root@serverp60231305290002 testdir1]# mkdir a
[root@serverp60231305290002 testdir1]# mkdir -p b/b1/b.txt
[root@serverp60231305290002 testdir1]# cd b/b1/b.txt/
[root@serverp60231305290002 b.txt]# echo "ssd" > "b.txt"
[root@serverp60231305290002 b.txt]# ls
b.txt
[root@serverp60231305290002 b.txt]# cd ../..
[root@serverp60231305290002 b]# cd ..
[root@serverp60231305290002 testdir1]# ls
a b c.txt
[root@serverp60231305290002 testdir1]# cd ..
[root@serverp60231305290002 test]# ls
scf testdir1 testdir2 testdir3 testdir4 testdir5
[root@serverp60231305290002 test]# rm testdir1 -r
rm: descend into directory `testdir1'? y
rm: remove regular file `testdir1/c.txt'? y
rm: remove directory `testdir1/a'? y
rm: descend into directory `testdir1/b'? y
rm: descend into directory `testdir1/b/b1'? y
rm: descend into directory `testdir1/b/b1/b.txt'? n
[root@serverp60231305290002 test]# ls
scf testdir1 testdir2 testdir3 testdir4 testdir5
[root@serverp60231305290002 test]# ll testdir1
total 4
drwxr-xr-x 3 root root 4096 Nov 30 17:07 b
[root@serverp60231305290002 test]# ll testdir1/b/b1/b.txt/
total 4
-rw-r--r-- 1 root root 4 Nov 30 17:07 b.txt
[root@serverp60231305290002 test]# ls
scf testdir1 testdir2 testdir3 testdir4 testdir5
[root@serverp60231305290002 test]# rm -r testdir1
rm: descend into directory `testdir1'? y
rm: descend into directory `testdir1/b'? y
rm: descend into directory `testdir1/b/b1'? y
rm: descend into directory `testdir1/b/b1/b.txt'? y
rm: remove regular file `testdir1/b/b1/b.txt/b.txt'? y
rm: remove directory `testdir1/b/b1/b.txt'? y
rm: remove directory `testdir1/b/b1'? y
rm: remove directory `testdir1/b'? y
rm: remove directory `testdir1'? y
[root@serverp60231305290002 test]#
[root@serverp60231305290002 test]# ls
scf testdir2 testdir3 testdir4 testdir5
[root@serverp60231305290002 test]#
实例五:rm -rf testdir2命令会将 testdir2 子目录及子目录中所有档案删除,并且不用一一确认
命令:
rm -rf testdir2
输出:
[root@serverp60231305290002 test]# cd testdir2
[root@serverp60231305290002 testdir2]# ls
testdir22
[root@serverp60231305290002 testdir2]# echo 2.txt
2.txt
[root@serverp60231305290002 testdir2]# cd ..
[root@serverp60231305290002 test]# rm -rf testdir2
[root@serverp60231305290002 test]# ls
scf testdir3 testdir4 testdir5
[root@serverp60231305290002 test]#
实例六:删除以 -f 开头的文件,特殊字符需要加--
命令:
rm -- -f
输出:
[root@serverp60231305290002 test]# touch -- -f
You have new mail in /var/spool/mail/root
[root@serverp60231305290002 test]# ls
-f scf testdir3 testdir4 testdir5
[root@serverp60231305290002 test]# touch -f
touch: missing file operand
Try `touch --help' for more information.
[root@serverp60231305290002 test]# ll
total 16
-rw-r--r-- 1 root root 0 Nov 30 17:15 -f
drwxr-xr-x 7 root root 4096 Nov 30 16:22 scf
drwxrwxrwx 2 root root 4096 Nov 30 16:11 testdir3
drwxr-xr-x 2 root root 4096 Nov 30 16:20 testdir4
drwxr-xr-x 3 root root 4096 Nov 30 16:20 testdir5
[root@serverp60231305290002 test]# rm -f
[root@serverp60231305290002 test]# ls
-f scf testdir3 testdir4 testdir5
[root@serverp60231305290002 test]# rm -- -f
rm: remove regular empty file `-f'? y
[root@serverp60231305290002 test]# ll
total 16
drwxr-xr-x 7 root root 4096 Nov 30 16:22 scf
drwxrwxrwx 2 root root 4096 Nov 30 16:11 testdir3
drwxr-xr-x 2 root root 4096 Nov 30 16:20 testdir4
drwxr-xr-x 3 root root 4096 Nov 30 16:20 testdir5
[root@serverp60231305290002 test]#
也可以使用下面的操作步骤:
[root@serverp60231305290002 test]# touch ./-f
[root@serverp60231305290002 test]# ll
total 16
-rw-r--r-- 1 root root 0 Nov 30 17:19 -f
drwxr-xr-x 7 root root 4096 Nov 30 16:22 scf
drwxrwxrwx 2 root root 4096 Nov 30 16:11 testdir3
drwxr-xr-x 2 root root 4096 Nov 30 16:20 testdir4
drwxr-xr-x 3 root root 4096 Nov 30 16:20 testdir5
[root@serverp60231305290002 test]# rm ./-f
rm: remove regular empty file `./-f'? y
[root@serverp60231305290002 test]#
实例七:自定义回收站功能
命令:
myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
输出:
[root@localhost test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
[root@localhost test]# alias rm='myrm'
[root@localhost test]# touch 1.log 2.log 3.log
[root@localhost test]# ll
总计 16
-rw-r--r-- 1 root root 0 10-26 15:08 1.log
-rw-r--r-- 1 root root 0 10-26 15:08 2.log
-rw-r--r-- 1 root root 0 10-26 15:08 3.log
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm [123].log
moved to /tmp/20121026150901 ok
[root@localhost test]# ll
总计 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# ls /tmp/20121026150901/
1.log 2.log 3.log
[root@localhost test]#
说明:
上面的操作过程模拟了回收站的效果,即删除文件的时候只是把文件放到一个临时目录中,这样在需要的时候还可以恢复过来。
参考资料:codingstandards.iteye.com/blog/983531
深入:
在rm的手册中写道:用rm删除文件还是有可能恢复文件内容的,如果要不可恢复的删除文件,考虑使用shred命令。(_Warning_: If you use `rm' to remove a file, it is usually possible to recover the contents of that file. If you want more assurance that the contents are truly unrecoverable, consider using `shred'.)至于具体怎么恢复rm删除的文件,有文章专门讨论了。但是不幸的是文件系统是ext2可恢复、ext3不可恢复,ext3的删除机制是直接把 inode data 删除了,所以造成 ext3 无法反删除(ext3设计为无法恢复被删除的文件)。不过,有些文章提到可以使用debugfs工具来尝试恢复。无论如何,rm文件之后要想恢复还是很麻烦的。
为了避免一个命令毁掉整个系统、或者误删重要文件( 比如辛苦编写的源程序文件),最好的方式不去删除,而是采用改名或者移动文件位置来去掉不需要的文件(mv命令)。还有就是,在进行一些关键操作时,先对数据进行备份,尤其是异机备份、异地备份。
查看当前的rm结构
[root@serverp60231305290002 test]# type -a rm
rm is aliased to `rm -i'
rm is /bin/rm
You have new mail in /var/spool/mail/root
[root@serverp60231305290002 test]#
linux rm linux命令 rm命令 每日一则linux命令 rm命令详解 rm命令怎么使用 rm参数 rm命令使用注意
--end by itlife365