Rynardald
Rynardald

Reputation: 359

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

I found the following linux command.

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

I know what cp does and the -f is also no problem. But what I don;t know is what the square brackets and the exclamation marks do ([!r], [!e], [!d], [!m], [!i], [!n]). Can anyone help me?

I found this command here: https://redmine.org/projects/redmine/wiki/HowTo_Migrate_Redmine_to_a_new_server_to_a_new_Redmine_version

Upvotes: 1

Views: 424

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52132

This is described in the manual under 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.

So, [!r] is any character except r, [!e] is any character except e, and so on. [!r][!e][!d][!m][!i][!n][!e]* expands to the names of all files that don't begin with the string redmine (except those that begin with ., unless the dotglob shell option is set).

There is another shell option that lets you write the same thing a little more elegant:

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

where !(pattern) matches everything except pattern.

Upvotes: 1

Related Questions