当前位置: 首页 > 工具软件 > rm-protection > 使用案例 >

Linux命令详解之 rm

姜钊
2023-12-01

linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

rm(remove)删除文件或目录

命令用法:

rm [选项]... [文件]...

命令概述:

rm删除每个指定的文件,默认情况下不能删除目录。

命令参数:

-f , --force 
	 忽略文件是否存在和其他参数选项(提示类),不会提示(询问)用户
		
-i	 每次删除都进行提示
		
-I	
	 如果文件数量超过3个,或者当递归删除时,提示用户。

--interactive[WHEN]
	 根据WHEN进行提示:never,once(等同于-I),或者always(-i);WHEN不填时,一直
	 提示(always)
	   
--one-file-system
	 递归删除时,跳过与当前命令行参数不同的文件系统上的目录(???)

--no-preserve-root
	 不对根目录/特别对待
	 
--preserve-root
	 不删除根目录(默认情况)
	   
-r,-R,--recursive
	 递归地删除目录及其内容
	   
-d,--dir
	 删除空目录

-v,--verbose
	 表明已完成的操作

--help 
	 显示帮助信息

--version 
	 显示版本信息

示例:

1. rm [文件]…

不带任何参数,删除指定的文件。

xiaohui@ubuntu:~/work/rm_learn$ ls
b.c  c.c
xiaohui@ubuntu:~/work/rm_learn$ rm b.c c.c 
xiaohui@ubuntu:~/work/rm_learn$ ls
xiaohui@ubuntu:~/work/rm_learn$ 

注意:rm命令的删除是不可逆的,一旦删除,要恢复就不容易了。
所以,平时一定要做好备份,同时少用’*'这类通配符进行删除,比如:

xiaohui@ubuntu:~/work/rm_learn$ ls
i.c  ii.c  important.c
xiaohui@ubuntu:~/work/rm_learn$ rm i*.c
xiaohui@ubuntu:~/work/rm_learn$ ls
xiaohui@ubuntu:~/work/rm_learn$ 

这样很容易误删除,不要等删除了才后悔!
另外,我们也可以考虑改写rm命令,将其改成放入回收站,这里就不详细讲了。

2. rm -f [文件]…

强制删除,不管文件存不存在(不存在也不提示),就算加了-i选项也不会提示(询问)。

xiaohui@ubuntu:~/work/rm_learn$ touch a.c
xiaohui@ubuntu:~/work/rm_learn$ rm b.c
rm: cannot remove 'b.c': No such file or directory
xiaohui@ubuntu:~/work/rm_learn$ rm a.c b.c -f
xiaohui@ubuntu:~/work/rm_learn$ 

3. rm -i [文件]…

在每次删除前提示用户,需要用户做出选择,回车和n/N都是不删除。

xiaohui@ubuntu:~/work/rm_learn$ touch a.c
xiaohui@ubuntu:~/work/rm_learn$ rm a.c -i
rm: remove regular empty file 'a.c'? n
xiaohui@ubuntu:~/work/rm_learn$ rm a.c -i
rm: remove regular empty file 'a.c'? y
xiaohui@ubuntu:~/work/rm_learn$ ls a.c
ls: cannot access 'a.c': No such file or directory
xiaohui@ubuntu:~/work/rm_learn$ 

4. rm -r/-R [文件]…

递归删除,删除目录和目录内文件。rm默认不能删除目录,需要加上该选项。

xiaohui@ubuntu:~/work/rm_learn$ rm dir/
rm: cannot remove 'dir/': Is a directory
xiaohui@ubuntu:~/work/rm_learn$ rm dir/ -r
xiaohui@ubuntu:~/work/rm_learn$ ls dir 
ls: cannot access 'dir': No such file or directory
xiaohui@ubuntu:~/work/rm_learn$ 

5. rm -I [文件]…

文件超过三个或则递归删除时才进行提示。

xiaohui@ubuntu:~/work/rm_learn$ rm *.c -I
rm: remove 4 arguments? n
xiaohui@ubuntu:~/work/rm_learn$ rm dir/ -I
rm: cannot remove 'dir/': Is a directory
xiaohui@ubuntu:~/work/rm_learn$ rm dir/ -I -r
rm: remove 1 argument recursively? n
xiaohui@ubuntu:~/work/rm_learn$ 

6. rm --interactive[=WHEN] [文件]…

WHEN不填或填always和yes时,相当于-i;WHEN为nerver或no时,相当于-I,如果为none或no,则不提示。

xiaohui@ubuntu:~/work/rm_learn$ rm --interactive *.c
rm: remove regular empty file 'a.c'? n
rm: remove regular empty file 'b.c'? n
rm: remove regular empty file 'c.c'? n
rm: remove regular empty file 'd.c'?  
xiaohui@ubuntu:~/work/rm_learn$ rm --interactive=once *.c
rm: remove 4 arguments? n
xiaohui@ubuntu:~/work/rm_learn$ rm --interactive=none *.c
xiaohui@ubuntu:~/work/rm_learn$ ls *.c
ls: cannot access '*.c': No such file or directory
xiaohui@ubuntu:~/work/rm_learn$ 

7. rm -d [文件]…

删除空目录,作用和rmdir一样。在删除无用目录时,比较实用,比-r安全。

xiaohui@ubuntu:~/work/rm_learn$ touch dir/a.c
xiaohui@ubuntu:~/work/rm_learn$ rm dir/ -d
rm: cannot remove 'dir/': Directory not empty
xiaohui@ubuntu:~/work/rm_learn$ rm dir/a.c 
xiaohui@ubuntu:~/work/rm_learn$ rm dir/ -d
xiaohui@ubuntu:~/work/rm_learn$ ls
xiaohui@ubuntu:~/work/rm_learn$ 

8. rm -v [文件]…

打印删除的过程,每删除一个就提示一个。

xiaohui@ubuntu:~/work/rm_learn$ touch a.c b.c c.c d.c
xiaohui@ubuntu:~/work/rm_learn$ rm *.c -v
removed 'a.c'
removed 'b.c'
removed 'c.c'
removed 'd.c'
xiaohui@ubuntu:~/work/rm_learn$ 

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 the -I or --interactive=once option is given,  and  there  are  more
       than  three  files  or  the  -r,  -R, or --recursive are given, then rm
       prompts the user for whether to proceed with the entire operation.   If
       the response is not affirmative, the entire command is aborted.

       Otherwise,  if  a file is unwritable, standard input is a terminal, and
       the -f or --force  option  is  not  given,  or  the  -i  or  --interac‐
       tive=always  option is 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 and arguments, never prompt

       -i     prompt before every removal

       -I     prompt once before removing  more  than  three  files,  or  when
              removing recursively; less intrusive than -i, while still giving
              protection against most mistakes

       --interactive[=WHEN]
              prompt according to WHEN: never,  once  (-I),  or  always  (-i);
              without WHEN, prompt always

	   --one-file-system
              when  removing  a hierarchy recursively, skip any directory that
              is on a file system different from  that  of  the  corresponding
              command line argument

       --no-preserve-root
              do not treat '/' specially

       --preserve-root
              do not remove '/' (default)

       -r, -R, --recursive
              remove directories and their contents recursively

       -d, --dir
              remove empty directories

       -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  might  be  possible  to
       recover  some  of its contents, given sufficient expertise and/or time.
       For greater assurance that the contents are truly  unrecoverable,  con‐
       sider using shred.

AUTHOR
       Written  by  Paul  Rubin, David MacKenzie, Richard M. Stallman, and Jim
       Meyering.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report rm translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2016 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       unlink(1), unlink(2), chattr(1), shred(1)

       Full documentation at: <http://www.gnu.org/software/coreutils/rm>
       or available locally via: info '(coreutils) rm invocation'

GNU coreutils 8.25               February 2017                           RM(1)

 类似资料: