您的位置:首页 > 运维架构 > Linux

linux小白-基础命令-rm

2015-05-15 13:25 471 查看


rm

【功能说明】:删除一个目录中的一个或多个文件或目录,如果没有使用-r选项,则rm不会删除目录。如果使用rm来删除文件,通常仍可以将该文件恢复原状。

【语法格式】:rm [选项] [文件/目录]

【选项参数】:
-f, --force 忽略不存在的文件,从不给出提示。-i, --interactive 进行交互式删除-r, -R, --recursive 指示rm将参数中列出的全部目录和子目录递归地删除。-v, --verbose 详细显示进行的步骤--help 显示此帮助信息并退出--version 输出版本信息并退出 【实践操作】:实例一:删除文件file,系统会先询问是否删除。 命令:rm 文件名输出:[root@linux-GC test1]# ll-rw-r--r-- 1 root root 56 10-26 14:31 log.logroot@linux-GC test1]# rm log.log rm:是否删除 一般文件 “log.log”? yroot@linux-GC test1]# ll[root@linux-GC test1]#说明:输入rm log.log命令后,系统会询问是否删除,输入y后就会删除文件,不想删除则输入n。 实例二:强行删除file,系统不再提示。 命令:rm -f log1.log输出:[root@linux-GC test1]# ll-rw-r--r-- 1 root root 23 10-26 14:40 log1.log[root@linux-GC test1]# rm -f log1.log [root@linux-GC test1]# ll[root@linux-GC test1]# 实例三:删除任何.log文件;删除前逐一询问确认 命令:rm -i *.log输出:[root@linux-GC test1]# ll-rw-r--r-- 1 root root 11 10-26 14:45 log1.log-rw-r--r-- 1 root root 24 10-26 14:45 log2.log[root@linux-GC test1]# rm -i *.logrm:是否删除 一般文件 “log1.log”? yrm:是否删除 一般文件 “log2.log”? y[root@linux-GC test1]# ll[root@linux-GC test1]# 实例四:将 test1子目录及子目录中所有档案删除命令:rm -r test1输出:[root@linux-GC test]# lldrwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 2 root root 4096 10-26 14:51 test1drwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@linux-GC test]# rm -r test1rm:是否进入目录 “test1”? yrm:是否删除 一般文件 “test1/log3.log”? yrm:是否删除 目录 “test1”? y[root@linux-GC test]# ll总计 20drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@linux-GC test]# 实例五:rm -rf test2命令会将 test2 子目录及子目录中所有档案删除,并且不用一一确认命令:rm -rf test2 输出:[root@linux-GC test]# rm -rf test2[root@linux-GC test]# lldrwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@linux-GC test]# 实例六:删除以 -f 开头的文件命令:rm -- -f输出:[root@linux-GC test]# touch -- -f[root@linux-GC test]# ls -- -f-f[root@linux-GC test]# rm -- -frm:是否删除 一般空文件 “-f”? y[root@linux-GC test]# ls -- -fls: -f: 没有那个文件或目录[root@linux-GC test]# 也可以使用下面的操作步骤:[root@linux-GC test]# touch ./-f[root@linux-GC test]# ls ./-f./-f[root@linux-GC test]# rm ./-frm:是否删除 一般空文件 “./-f”? y[root@linux-GC test]# 实例七:自定义回收站功能命令:myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; } 输出:[root@jfht ~]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
[root@jfht ~]# alias rm='myrm'
[root@jfht ~]# touch 1.txt 2.txt 3.txt
[root@jfht ~]# ls [123].txt
1.txt 2.txt 3.txt
[root@jfht ~]# rm [123].txt
moved to /tmp/20110401214056 ok
[root@jfht ~]# ls /tmp/20110401214056/
1.txt 2.txt 3.txt
[root@jfht ~]#说明:上面的操作过程模拟了回收站的效果,即删除文件的时候只是把文件放到一个临时目录中,这样在需要的时候还可以恢复过来。

本文出自 “CornerBoy” 博客,请务必保留此出处http://cornerboy.blog.51cto.com/10012663/1651578
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: