如何在 Linux 上的 Bash 中一次删除多个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10516384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:15:00  来源:igfitidea点击:

How to delete multiple files at once in Bash on Linux?

linuxbashrm

提问by user1253847

I have this list of files on a Linux server:

我在 Linux 服务器上有这个文件列表:

abc.log.2012-03-14
abc.log.2012-03-27
abc.log.2012-03-28
abc.log.2012-03-29
abc.log.2012-03-30
abc.log.2012-04-02
abc.log.2012-04-04
abc.log.2012-04-05
abc.log.2012-04-09
abc.log.2012-04-10

I've been deleting selected log files one by one, using the command rm -rfsee below:

我一直在逐个删除选定的日志文件,使用如下命令rm -rf

rm -rf abc.log.2012-03-14
rm -rf abc.log.2012-03-27
rm -rf abc.log.2012-03-28

Is there another way, so that I can delete the selected files at once?

有没有另一种方法,以便我可以立即删除所选文件?

采纳答案by unwind

Bash supports all sorts of wildcards and expansions.

Bash 支持各种通配符和扩展。

Your exact case would be handled by brace expansion, like so:

您的确切情况将由大括号扩展处理,如下所示:

$ rm -rf abc.log.2012-03-{14,27,28}

The above would expand toa single command with all three arguments, and be equivalent to typing:

以上将扩展为具有所有三个参数的单个命令,并等效于键入:

$ rm -rf abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28

It's important to note that this expansion is done by the shell, before rmis even loaded.

重要的是要注意,这种扩展是由外壳完成的,rm甚至在加载之前。

回答by dogbane

Use a wildcard (*) to match multiple files.

使用通配符 ( *) 匹配多个文件。

For example, the command below will delete all files with names beginning with abc.log.2012-03-.

例如,下面的命令将删除名称以abc.log.2012-03-.

rm -f abc.log.2012-03-*

I'd recommend running ls abc.log.2012-03-*to list the files so that you can see what you are going to delete before running the rmcommand.

我建议运行ls abc.log.2012-03-*以列出文件,以便您可以在运行rm命令之前查看要删除的内容。

For more details see the Bash man page on filename expansion.

有关更多详细信息,请参阅有关文件名扩展的 Bash 手册页。

回答by Dave

I am not a linux guru, but I believe you want to pipe your list of output files to xargs rm -rf. I have used something like this in the past with good results. Test on a sample directory first!

我不是 linux 专家,但我相信您想将输出文件列表通过管道传输到xargs rm -rf. 我过去使用过类似的东西,效果很好。首先在示例目录上进行测试!

EDIT - I might have misunderstood, based on the other answers that are appearing. If you can use wildcards, great. I assumed that your original list that you displayed was generated by a program to give you your "selection", so I thought piping to xargswould be the way to go.

编辑 - 根据出现的其他答案,我可能误解了。如果您可以使用通配符,那就太好了。我假设您显示的原始列表是由程序生成的,以便为您提供“选择”,所以我认为管道xargs将是要走的路。

回答by lacrosse1991

A wild card would work nicely for this, although to be safe it would be best to make the use of the wild card as minimal as possible, so something along the lines of this:

通配符可以很好地解决这个问题,但为了安全起见,最好尽可能少地使用通配符,所以大致如下:

rm -rf abc.log.2012-*

Although from the looks of it, are those just single files? The recursive option should not be necessary if none of those items are directories, so best to not use that, just for safety.

虽然从它的外观来看,那些只是单个文件吗?如果这些项目都不是目录,则不需要递归选项,所以最好不要使用它,只是为了安全。

回答by Keith Thompson

If you want to delete all files whose names match a particular form, a wildcard (glob pattern) is the most straightforward solution. Some examples:

如果要删除名称与特定格式匹配的所有文件,通配符(glob 模式)是最直接的解决方案。一些例子:

$ rm -f abc.log.*             # Remove them all
$ rm -f abc.log.2012*         # Remove all logs from 2012
$ rm -f abc.log.2012-0[123]*  # Remove all files from the first quarter of 2012

Regular expressions are more powerful than wildcards; you can feed the output of grepto rm -f. For example, if some of the file names start with "abc.log"and some with "ABC.log", greplets you do a case-insensitive match:

正则表达式比通配符更强大;您可以提供grepto的输出rm -f。例如,如果某些文件名以 开头,"abc.log"有些以开头"ABC.log",则grep可以进行不区分大小写的匹配:

$ rm -f $(ls | grep -i '^abc\.log\.')

When I do this, I run the ls | grep ...command first and check that it produces the output I want -- especiallyif I'm using rm -f:

当我这样做时,我ls | grep ...首先运行命令并检查它是否产生了我想要的输出 -特别是如果我正在使用rm -f

$ ls | grep -i '^abc\.log\.'
(check that the list is correct)
$ rm -f $(!!)

where !!expands to the previous command. Or I can type up-arrow or Ctrl-P and edit the previous line to add the rm -fcommand.

where!!扩展到上一个命令。或者我可以输入向上箭头或 Ctrl-P 并编辑上一行以添加rm -f命令。

This assumes you're using the bash shell. Some other shells, particularly csh and tcsh and some older sh-derived shells, may not support the $(...)syntax. You can use the equivalent backtick syntax:

这假设您使用的是 bash shell。其他一些 shell,尤其是 csh 和 tcsh 以及一些较旧的 sh 派生 shell,可能不支持该$(...)语法。您可以使用等效的反引号语法:

$ rm -f `ls | grep -i '^abc\.log\.'`

The $(...)syntax is easier to read, and if you're really ambitious it can be nested.

$(...)语法更容易阅读,如果你真的雄心勃勃它可以被嵌套。

Finally, if the subset of files you want to delete can't be easily expressed with a regular expression, a trick I often use is to list the files to a temporary text file, then edit it:

最后,如果您要删除的文件子集无法用正则表达式轻松表达,我经常使用的一个技巧是将文件列出到临时文本文件中,然后对其进行编辑:

$ ls > list
$ vi list   # Use your favorite text editor

I can then edit the listfile manually, leaving only the files I want to remove, and then:

然后我可以list手动编辑文件,只留下我想删除的文件,然后:

$ rm -f $(<list)

or

或者

$ rm -f `cat list`

(This assumes none of the file names contain funny characters, particularly spaces.)

(这假设所有文件名都不包含有趣的字符,尤其是空格。)

Or, when editing the listfile, I can add rm -fto the beginning of each line and then:

或者,在编辑list文件时,我可以添加rm -f到每一行的开头,然后:

$ . ./list

or

或者

$ source ./list

回答by Alican

if you want to delete all files that belong to a directory at once. For example: your Directory name is "log" and "log" directory include abc.log.2012-03-14, abc.log.2012-03-15,... etc files. You have to be above the log directory and:

如果您想一次删除属于一个目录的所有文件。例如:您的目录名称是“log”,“log”目录包括 abc.log.2012-03-14、abc.log.2012-03-15、...等文件。您必须位于日志目录之上,并且:

rm -rf /log/*

回答by Andy

Just use multiline selection in sublime to combine all of the files into a single line and add a space between each file name and then add rmat the beginning of the list. This is mostly useful when there isn't a pattern in the filenames you want to delete.

只需在 sublime 中使用多行选择将所有文件合并为一行,并在每个文件名之间添加一个空格,然后rm在列表的开头添加。当您要删除的文件名中没有模式时,这非常有用。

[$]> rm abc.log.2012-03-14 abc.log.2012-03-27 abc.log.2012-03-28 abc.log.2012-03-29 abc.log.2012-03-30 abc.log.2012-04-02 abc.log.2012-04-04 abc.log.2012-04-05 abc.log.2012-04-09 abc.log.2012-04-10