linux 命令中方括号后跟感叹号是什么意思?

What do square brackets followed by exclamation mark in a linux command?

我找到了以下 linux 命令。

 cp -f [!r][!e][!d][!m][!i][!n][!e]* /SomePath

我知道 cp 的作用,-f 也没有问题。但我不知道的是方括号和感叹号的作用([!r][!e][!d][!m][!i][!n]).谁能帮帮我?

我在这里找到这个命令:https://redmine.org/projects/redmine/wiki/HowTo_Migrate_Redmine_to_a_new_server_to_a_new_Redmine_version

这在pattern matching下的手册中有描述:

[…]
Matches any of the enclosed characters. [...] If the first character following the [ is a ! or a ^ then any character not enclosed is matched.

所以,[!r]是除r以外的任意字符,[!e]是除e以外的任意字符,依此类推。 [!r][!e][!d][!m][!i][!n][!e]* 扩展为所有不以字符串 redmine 开头的文件的名称(以 . 开头的文件除外,除非 dotglob shell 选项已设置)。

还有一个 shell 选项可以让你把同样的东西写得更优雅一点:

shopt -s extglob
cp -f !(redmine)* /SomePath

其中 !(<i>pattern</i>) 匹配除 pattern 之外的所有内容。 =26=]