J.Pei
J.Pei

Reputation: 141

What's the wildcard for dot in bash?

I have files like 0001.file1.email.data.spam.txt and 0001.file1.email.data.spam_1.txt

Now I want to delete all files end with "_1.txt", I tried to use "rm -rf *spam_1.txt", but it cannot find the files. Maybe because * cannot viewed as dot.

Upvotes: 2

Views: 1657

Answers (1)

tripleee
tripleee

Reputation: 189908

Dot is a literal character which simply matches itself.

The * wildcard matches any sequence of characters, and the ? wildcard matches any single character, but wildcard matching will ignore files which start with a dot unless you have dotglob enabled (it is off by default).

You can easily examine what files are being matched by a wildcard expression with something like

printf '>>%s<<\n' *spam_1.txt

(The >>...<< decoration is just to make it easy to see any leading or trailing whitespace, and isn't strictly necessary.)

In the absence of nullglob, the above will print the wildcard itself if it doesn't find any matches. Also check out failglob which causes an error to be printed and the command to be aborted if the wildcard doesn't match anything.

Upvotes: 3

Related Questions