rm (删除文件)

瑞兹 发表于 2020-11-23 09:23
浏览次数:
在手机上阅读

在类似Unix的操作系统上,rm命令删除文件。

查看英文版

目录

1 rm 运行系统环境

2 rm 描述

3 rm 语法

4 rm 例子

rm 运行系统环境

Linux

rm 描述

rm删除命令行上指定的每个文件。默认情况下,它不会删除目录。

RM与执行-r-R选项,它递归地删除任何匹配的目录,它们的子目录,它们包含的所有文件。有关详细信息,请参见下面的删除目录。

删除过程将文件系统中的文件名与其关联的数据断开链接,并将存储设备上的该空间标记为将来的写入可用。换句话说,删除文件时,文件中的数据不会更改,但不再与文件名关联。

数据本身不会被破坏,但是在与rm取消链接之后,将变得不可访问。明智地删除文件!这不像在Windows回收站中放东西。一旦你的RM文件或目录,也没有办法将其复原。

rm removes each file specified on the command line. By default, it does not remove directories.

When rm is executed with the -r or -R options, it recursively deletes any matching directories, their subdirectories, and all files they contain. See removing directories below for details.

The removal process unlinks a file name in a filesystem from its associated data, and marks that space on the storage device as usable by future writes. In other words, when you remove a file, the data in the file isn't changed, but it's no longer associated with a filename.

The data itself is not destroyed, but after being unlinked with rm, it becomes inaccessible. Remove your files wisely! It's not like putting something in the Windows Recycle Bin; once you rm a file or directory, there is no way to undo it.

如果要完全擦除磁盘上的数据,请使用shred命令。shred将覆盖文件的内容,以便以后无法重建它们。

If you want is to completely wipe the data on the disk, use the shred command instead. shred will overwrite the file's contents so that they cannot be reconstructed later.

查看英文版

查看中文版

rm 语法

rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
   [--interactive=never]} [--one-file-system] [--no-preserve-root | --preserve-root] 
[-r | -R | --recursive] [-d | --dir] [-v | --verbose] FILE...
rm --help
rm --version

选件

-f
--force
忽略不存在的文件,并且在删除之前永远不会提示。
-i 每次移除前均提示。
-I 在删除三个以上的文件之前或在递归删除时提示一次。该选项比-i更具侵入性,但仍然可以防止大多数错误。
--interactive [ =WHEN] 根据WHEN提示:neveronce-I)或alway-i)。如果未指定WHEN,则始终提示。
--on-file-system 递归删除层次结构时,请跳过文件系统上与相应命令行参数不同的任何目录
--no-preserve-root 请勿以任何特殊方式处理“ / ”(根目录)。
--preserve-root 不要删除“ / ”(根目录),这是默认行为。
-r

-R

--recursive

递归删除目录及其内容。
-d,--dir 删除空目录。如果目录为空,此选项允许您在不指定-r / -R / --recursive的情况下删除目录。换句话说,rm -d等效于使用rmdir
-v,--verbose 详细模式;随时说明正在做什么。
- help 显示帮助消息,然后退出。
- version 显示版本信息,然后退出。

使用说明

如果给出了-I / --interactive = once选项,并且文件数超过三个,或者指定了-r / -R / --recursive选项,则rm将在删除任何内容之前提示。如果用户对提示不回答“y / y ”,则整个命令将中止。

如果文件不可写,stdin是terminal,并且没有给出-f / --force选项,或者没有给出-i--interactive = always选项,则rm提示用户是否删除文件。如果响应不是yes / y / Y,则跳过文件。

删除目录

缺省情况下,rm不删除目录。但是,如果指定了-r / -R / --recursive选项,则rm将删除所有匹配的目录及其内容。

如果指定目录为空,则可以使用-d / --dir选项将其删除。

文件名以破折号开头

要删除名称以破折号(“ - ”)开头的文件,可以在文件名之前分别指定双破折号(“ - ”)。此多余的破折号是必需的,以便rm不会将文件名误解为选项。

例如,如果当前目录中有一个名为“ -file.txt ”的文件,则可以使用以下命令将其删除

rm--file.txt

或者,您可以通过使用pathname引用来删除它。例如,如果文件“ -file.txt ”位于目录“ / home / hope ”中,则可以使用以下命令将其删除:

rm /home/hope/-file.txt

...或者,如果/ home / hope当前是您的工作目录,

rm ./-file.txt

...也可以。

rm [-f | --force] {[-i | --interactive[=always]] | [-I | --interactive=once] |
   [--interactive=never]} [--one-file-system] [--no-preserve-root | --preserve-root] 
[-r | -R | --recursive] [-d | --dir] [-v | --verbose] FILE...
rm --help
rm --version

Options

-f,
--force
Ignore nonexistant files, and never prompt before removing.
-i Prompt before every removal.
-I Prompt once before removing more than three files, or when removing recursively. This option is less intrusive than -i, but still gives protection against most mistakes.
--interactive[=WHEN] Prompt according to WHENneveronce (-I), or always (-i). If WHEN is not specified, then 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 "/" (the root directory) in any special way.
--preserve-root Do not remove "/" (the root directory), which is the default behavior.
-r,
-R,
--recursive
Remove directories and their contents recursively.
-d,
--dir
Remove empty directories. This option permits you to remove a directory without specifying -r/-R/--recursive, provided that the directory is empty. In other words, rm -d is equivalent to using rmdir.
-v,
--verbose
Verbose mode; explain at all times what is being done.
--help Display a help message, and exit.
--version Display version information, and exit.

Usage notes

If the -I/--interactive=once option is given, and there are more than three files or the -r/-R/--recursive options are specified, rm will prompt before deleting anything. If the user does not respond yes/y/Y to the prompt, the entire command is aborted.

If a file is unwritable, stdin is a terminal, and the -f/--force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not yes/y/Y, the file is skipped.

Removing directories

By default, rm does not remove directories. If the -r/-R/--recursive option is specified, however, rm will remove any matching directories and their contents.

If the specified directory is empty, it may be removed with the -d/--dir option, instead.

File names starting with a dash

To remove a file whose name begins with a dash ("-"), you can specify a double dash ("--") separately before the file name. This extra dash is necessary so that rm does not misinterpret the file name as an option.

For instance, if there is a file in your current directory named "-file.txt", you can delete it with the command

rm -- -file.txt

Or, you can delete it by referring to it with a pathname. For instance, if the file "-file.txt" was located in the directory "/home/hope", you could delete it using:

rm /home/hope/-file.txt

...or, if /home/hope is currently your working directory,

rm ./-file.txt

...will work as well.

查看英文版

查看中文版

rm 例子

rm myfile.txt

删除文件myfile.txt。如果文件是写保护的,将提示您确认您确实要删除它。

rm -f myfile.txt

删除文件myfile.txt。即使文件是写保护的,也不会提示您。如果rm可以删除文件,它将删除。

R M *

删除工作目录中的所有文件。如果它是写保护的,在rm删除它之前会提示您。

rm -f *

删除工作目录中的所有文件。rm不会以任何理由提示您删除它们。

rm -i *

尝试删除工作目录中的每个文件,但在每个文件之前提示确认。

rm -I *

删除工作目录中的每个文件;提示确认是否删除三个以上的文件。

rm -r mydirectory

删除目录mydirectory,以及其中包含的所有文件和目录。如果rm尝试删除的文件或目录具有写保护,则系统将提示您确保确实要删除它。

rm -rf mydirectory

与上面的命令相同,但是不会提示您;如果rm可以删除文件,它将删除。

rm myfile.txt

Remove the file myfile.txt. If the file is write-protected, you will be prompted to confirm that you really want to delete it.

rm -f myfile.txt

Remove the file myfile.txt. You will not be prompted, even if the file is write-protected; if rm can delete the file, it will.

rm *

Remove all files in the working directory. If it is write-protected, you will be prompted before rm removes it.

rm -f *

Remove all files in the working directory. rm will not prompt you for any reason before deleting them.

rm -i *

Attempt to remove every file in the working directory, but prompt before each file to confirm.

rm -I *

Remove every file in the working directory; prompt for confirmation if more than three files are being deleted.

rm -r mydirectory

Remove the directory mydirectory, and any files and directories it contains. If a file or directory that rm tries to delete is write-protected, you will be prompted to make sure that you really want to delete it.

rm -rf mydirectory

Same as the above command, but you will never be prompted; if rm can delete the files, it will.

查看英文版

查看中文版

其他命令行

renice | replace | rmdir | rn | route | rpcinfo | rcp | readlink | rehash | rsh | rlogin | rmmod |

如此好文,分享给朋友