linux下rm命令使用详解—linux删除文件或目录命令
命令简要介绍:
该命令用来删除Linux系统中的文件或目录。日常景况下rm不会去除目录,你无法不经过点名参数-r或-纳瓦拉来删除目录。别的rm日常能够将该公文或目录复苏(注意,rm删除文件其实只是将对准数据块的索引点(information
nodes)释放,只要不被遮蔽,数据实际上还在硬盘上。借使想要有限帮助文件的剧情不可能复苏,能够接纳命令shred
。 别的一般照旧要慎用rm -rf *这么的吩咐。
进行权限 :All User
一声令下所在路线:/bin/rm
指令语法:
rm [OPTION]… FILE…
一声令下参数:
参数 |
长参数 |
描叙 |
-f |
–force |
强制删除,忽略不存在的文件,从不给出提示。 |
-i |
–interactive |
交互模式删除文件,删除文件前给出提示。 |
-r |
–recursive |
递归的删除目录下面文件以及子目录下文件。 |
-R |
–recursive |
递归的删除目录下面文件以及子目录下文件。 |
-v |
–verbose |
显示运行时详细信息 |
–help |
显示命令在线帮助 |
|
–version |
显示命令版本信息 |
利用示例:
1:查看获取rm命令的声援音信。
[[email protected] kerry]# man rm
RM(1) User Commands RM(1)
NAME
rm - remove files or directories
SYNOPSIS
rm [OPTION]... FILE...
DESCRIPTION
This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories.
If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If
the response is not affirmative, the file is skipped.
OPTIONS
Remove (unlink) the FILE(s).
-f, --force
ignore nonexistent files, never prompt
-i, --interactive
prompt before any removal
--no-preserve-root do not treat 鈥?鈥?specially (the default)
--preserve-root
fail to operate recursively on 鈥?鈥?
-r, -R, --recursive
remove directories and their contents recursively
-v, --verbose
explain what is being done
--help display this help and exit
--version
output version information and exit
By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a 鈥?鈥? for example 鈥?foo鈥? use one of these commands:
rm -- -foo
rm ./-foo
Note that 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.
AUTHOR
Written by Paul Rubin, David MacKenzie, Richard Stallman, and Jim Meyering.
REPORTING BUGS
Report bugs to <[email protected]>.
COPYRIGHT
Copyright 漏 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There
is NO WARRANTY, to the extent permitted by law.
SEE ALSO
chattr(1), shred(1)
The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command
info rm
should give you access to the complete manual.
rm 5.97 June 2011 RM(1)
[[email protected] kerry]# rm --help
Usage: rm [OPTION]... FILE...
Remove (unlink) the FILE(s).
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
--no-preserve-root do not treat `/' specially (the default)
--preserve-root fail to operate recursively on `/'
-r, -R, --recursive remove directories and their contents recursively
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
By default, rm does not remove directories. Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.
To remove a file whose name starts with a `-', for example `-foo',
use one of these commands:
rm -- -foo
rm ./-foo
Note that 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.
Report bugs to [email protected]
金沙4066官网,2:删除钦命文件
正如所示,该目录下有贰拾二个文件,删除钦点文件后,只剩下21个文本了。
[[email protected] bdump]$ ls -lrt | wc -l
20
[[email protected] bdump]$ rm gsp_lgwr_7826.trc
[[email protected] bdump]$ ls -lrt | wc -l
19
3:删除文件呈现运转时详细音信
[[email protected] bdump]$ rm -v gsp_lgwr_4617.trc
removed `gsp_lgwr_4617.trc'
4:交互情势下删除文件
正如所示,输入y后就能够去除文件,不想删除则数据n
[[email protected] bdump]$ rm -iv gsp_lgwr_5005.trc
rm: remove regular file `gsp_lgwr_5005.trc'? y
removed `gsp_lgwr_5005.trc'
[[email protected] bdump]$ rm -iv gsp_lgwr_7826.trc
rm: remove regular file `gsp_lgwr_7826.trc'? n
5: 递归删除某个目录下文件以及子目录
如下所示,kerry目录下有test1.txt、 test2.txt 文书,以及子目录tmp,
而子目录下又有文件file1、file2.
[[email protected] ~]# tree kerry
kerry
|-- test1.txt
|-- test2.txt
`-- tmp
|-- file1
`-- file2
1 directory, 4 files
[[email protected] ~]# rm -rf kerry/
6: 删除空目录
[[email protected] ~]# mkdir kerry
[[email protected] ~]# rm -r kerry/
rm: remove directory `kerry/'? y
7:删除目录bdump下的享有trc文件类型的文件。
[[email protected] bdump]$ ls
alert_GSP.log gsp_lgwr_20025.trc gsp_lgwr_5023.trc gsp_p000_4637.trc gsp_p000_4718.trc gsp_p000_5025.trc gsp_p001_4639.trc gsp_p001_4720.trc gsp_p001_5027.trc
gsp_lgwr_19915.trc gsp_lgwr_4719.trc gsp_lgwr_5089.trc gsp_p000_4682.trc gsp_p000_4739.trc gsp_p000_5043.trc gsp_p001_4684.trc gsp_p001_4741.trc gsp_p001_5045.trc
[[email protected] bdump]$ rm -v *.trc
removed `gsp_lgwr_19915.trc'
removed `gsp_lgwr_20025.trc'
removed `gsp_lgwr_4719.trc'
removed `gsp_lgwr_5023.trc'
removed `gsp_lgwr_5089.trc'
removed `gsp_p000_4637.trc'
removed `gsp_p000_4682.trc'
removed `gsp_p000_4718.trc'
removed `gsp_p000_4739.trc'
removed `gsp_p000_5025.trc'
removed `gsp_p000_5043.trc'
removed `gsp_p001_4639.trc'
removed `gsp_p001_4684.trc'
removed `gsp_p001_4720.trc'
removed `gsp_p001_4741.trc'
removed `gsp_p001_5027.trc'
removed `gsp_p001_5045.trc'
仿效资料:
每一日四个 Linux 命令(5):rm
命令。rm是常用的通令,该命令的效果为除去三个索引中的二个或多少个公文或目录。它也足以去除有个别目录及其下有所的公文和子目录。对于链接文件,它只是剔除了链接,原有文件保持不改变。
用户能够用rm命令删除无需的公文。该命令的效能为除去一个目录中的一个或五个文本或目录,它也得以将有些目录及其下的全数文件及子目录均删除。对于链接文件,只是断开了链接,原来的书文件保持不改变。
rm -rf filenem
[email protected]:~$
rm –help
用法:rm [选项]… 文件…
删除 (unlink) 文件。
-f, –force强制删除。忽略不设有的文书,不提示确认
-i在剔除前须要认可
-I在剔除超过八个文件恐怕递归删除前须要确认。此选项比-i 提
示内容越来越少,但同样能够阻止大大多不当发生
–interactive[=WHEN]基于内定的WHEN 进行确认提醒:never,once (-I),
抑或always (-i)。如若此参数不加WHEN 则连年提醒
–one-file-system递归删除一个层级时,跳过具备不相符命令行参
数的文件系统上的文件
–no-preserve-roo不新鲜对待”/”
–preserve-root不允许删除”/”(暗中同意)
-r, -Wrangler, –recursive递归删除目录及其内容
-v, –verbose详细突显实行的步子
–help显示此匡助新闻并脱离
–version展现版本音信并脱离
默许时,rm 不会去除目录。使用–recursive(-r 或-揽胜极光)选项可去除每一个给定
的目录,以及其下全数的源委。
要刨除第二个字符为”-“的文件 (比如”-foo”),请使用以下办法之一:
rm — -foo
rm ./-foo
Note that if you use rm to remove a file, it might be possible to
recover
some of its contents, given sufficient expertise and/or time. For
greater
assurance that the contents are truly unrecoverable, consider using
shred.
请向[email protected]
报告rm 的错误
GNU coreutils 项目主页:<www.gnu.org/software/coreutils/>
GNU 软件一般性救助:<www.gnu.org/gethelp/>
请向<translationproject.org/team/zh_CN.html> 报告rm 的翻译错误
要赢得完整文书档案,请运转:info coreutils ‘rm
invocation’…余下全文>>
rm是三个高危的指令,使用的时候要特别警惕,越发对于菜鸟,不然全体系统就能毁在这一个命令(譬如在/(根目录)下实行rm
-rf
*)。所以,我们在实行rm在此以前最棒先承认一下在哪些目录,到底要刨除什么事物,操作时保持高度清醒的脑子。
rm命令的貌似方式为:rm [选项]… 目录…
删除钦赐的<文件>(即解除链接)。
除去文件的话,你就可以直接选取rm;
不过删除目录的话,你能够用rmdir,可能rm -rf
命令简单介绍:
该命令用来删除Linux系统中的文件或目录。平时景况下rm不会删除目录,你无法不透过…
rm [选项] 文件
-d –directory 删除大概仍有多少的目录 (只限一流用户)
除去三个索引中的一个或四个文本或目录,如果未有行使-r选项,则rm不会去除目录。如果使用rm来删除文件,日常还是可以够将该公文苏醒原状。
-f –force 略过不设有的公文,不出示任何新闻
-f,–force,忽略海市蜃楼的文件,从不给出提示。
-i,–interactive,进行交互式删除。
-r,-CRUISER, –recursive,提示rm递归地删除参数中列出的一切索引和子目录。
-v,–verbose,详细展现进行的步子。
–help,呈现此帮忙音讯并脱离。
–version,输出版本音信并脱离。
-i –interactive 举办其余删除操作前必须先承认
例一:删除文件file,系统会先领会是否删除。
命令:rm 文件名
# ll
总计 4
-rw-r--r-- 1 root root 56 10-26 14:31 log.log
# rm log.log
rm:是否删除 一般文件 “log.log”? y //输入y后就会删除文件,不想删除则输入n
# ll
总计 0
例二:强行删除file,系统不再提拔。
命令:rm -f log1.log
# ll
总计 4
-rw-r--r-- 1 root root 23 10-26 14:40 log1.log
# rm -f log1.log
# ll
总计 0
例三:删除任何.log文件,删除前逐条询问确认
命令:rm -i *.log
# ll
总计 8
-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
# rm -i *.log
rm:是否删除一般文件“log1.log”? y
rm:是否删除一般文件“log2.log”? y
# ll
总计 0
例四:将 test1子目录及子目录中有着档案删除
命令:rm -r test1
# ll
总计 24
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 2 root root 4096 10-26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
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
# rm -r test1
rm:是否进入目录 “test1”? y
rm:是否删除 一般文件 “test1/log3.log”? y
rm:是否删除 目录 “test1”? y
# ll
总计 20
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
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
例五:rm -rf 命令会将 test2
子目录及子目录中存有文件删除,并且毫不一一确认
命令:rm -rf test2
# ll
总计 20
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
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
# rm -rf test2
# ll
总计 16
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
例六:删除以 -f 先河的文本
命令:rm — -f
# touch -- -f // --等价于./
# ls -- -f
# rm -- -f
rm:是否删除 一般空文件 “-f”? y
# ls -- -f
ls: -f: 没有那个文件或目录
也足以使用上边包车型大巴操作步骤:
# touch ./-f
# ls ./-f
./-f
# rm ./-f
rm:是否删除 一般空文件 “./-f”? y
例七:自定义回收站功用
命令:
myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
# alias rm='myrm'
# touch 1.log 2.log 3.log
# 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
# rm [123].log
moved to /tmp/20121026150901 ok
# ll
总计 16
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
# ls /tmp/20121026150901/
1.log 2.log 3.log
证实:上边的操作进程模拟了回收站的法力,即除去文件的时候只是把公文放到一个这两天目录中,这样在需求的时候还是能苏醒过来。
-r/CRUISER –recursive 同一时间删除该目录下的有所目录层
-v –verbose 详细呈现举行的手续
–help 展现此扶助音信并离开
–version 突显版本消息并离开
要刨除第贰个字符为‘-’的文件
(例如‘-foo’),请使用以下其中一种办法:
rm — -foo
rm ./-foo
请留神,假设应用 rm 来删除文件,平常还是可以够将该文件恢复生机原状。假诺想保障
该公文的原委不能复苏,请思索动用 shred。
范例:
去除全体rb语言程序档;删除前逐条询问确认 :
rm -i *.rb
将 Finished 子目录及子目录中全部档案删除 :
rm -r Finished
将 Finished 子目录及子目录中负有档案删除,何况毫不–确认 :
rm -rf Finished