All About Linux Command Line Wildcards

Bash TerminalIn a previous post I mentioned wild cards very briefly, in conjunction with the Linux command line.

I did not stop to explain at that time, but wild cards have a life that extends far beyond the Linux command line.

It is time now to delve deeper in this subject, as it is a great one to be aware of. But take note that I will be focusing back on the use of wildcards in the Linux command line.

The Concept of Wildcards

Wildcards are common in card games, search-platforms, programming, and even our everyday lives. You may have heard someone say, “He/She/It is a wild card.” The term has more or less the same meaning in each one of these cases.

Wildcards are unknown variables, or placeholders, usually among known variables. They can stand for something very specific, or for something very broad.

Electronically, a wildcard is a keyboard character or characters that represent the unknown variable(s). The character(s) indicate a gap, which can be filled/matched by unknown data that fits the criteria of the wildcard(s) used.

Regular expressions, used in programming (and by the grep and find commands, etc.), are an advanced form of wildcards that are used to form patterns, usually for string matching and searching purposes.

Wildcards in the Command Line

Wildcards can be used in the command line as part of file names and/or file paths.

The possibilities that wildcards unlock in such an instance include not only the ability to do less typing — because you already have tabs for that! — but primarily the abilities to refer to files/paths that are unknown, and to make changes that apply to multiple files/paths at once.

As an example, let’s say that you have a bunch of vacation photos named vacation_2016-02-02_1.png, vacation_2016-02-02_2.png, vacation_2016-02-02_3.png, etc. and that you want to copy all of those photos to a pen drive to give to relatives. You might be thinking, “Easy, just copy the folder!”, but what if the folder has other photos in it, too? By using the appropriate wildcard in place of the photo number, you can copy every single photo with a single command.

Wildcards can be dangerous; used incorrectly, they can help/allow you to perform commands of a devastating nature. You could delete all those vacation photos in one fell swoop, and they wouldn’t just end up in the recoverable Trash bin, either! (Cue the joke about erasing the entire contents of your hard drive… but who would do that, even accidentally?!)

The ? Wildcard

The question mark ? wildcard represents any single character.

Multiple question marks can be used at one time, but each one represents a single character; if three question marks are used together, they represent three characters, no more, and no less.

The command mv ?apples.txt apples.txt, for example, would rename a file named 7apples.txt or mapples.txt, but would not rename 77apples.txt or chapples.txt.

Thus, the command cp grapes.??? /home/gwen/ would copy a file named grapes.txt or grapes.vlc, but would not copy grapes.xlsx or grapes.jpeg.

Wildcards can be used on path names, as well as file names, although in many cases I’m hard-pressed to see how this might be useful: cd /var/???/html

The ? wildcard is very specific in its usage.

The * Wildcard

The asterisk * wildcard represents an unlimited number of multiple characters.

In fact, using * on it’s own, without sufficient qualifications, is one of the easiest ways around to totally destroy the contents of your hard drive. Be warned, but don’t be afraid. (Much.)

Used correctly, with sufficient qualifying data (and especially with “harmless” commands like ls and cp, that are not prone to destroying data), the * wildcard is a great asset.

The command ls *.ttxt, for example, will list all of the text files in your current directory, while ls Gwen.* will list all files named only with my name (regardless of the file extension) and ls *Gwen*.* will list all files that contain my name anywhere in the file name (regardless of extension).

Take note that using the * wildcard loosely can very easily cause actions to be performed on multiple files at once — which is great, if you want to affect multiple files — so take care to provide sufficient qualifications when multiple files should not be affected.

Wildcards can be used on path names, as well as file names, although in many cases I’m hard-pressed to see how this might be useful: cd /*/gwen

The * wildcard is very broad in its usage.

The [] Wildcard

The square bracket [] wildcards are used to specify a range. A range can refer to any of the characters enclosed in the brackets. Ranges can employ hyphens to indicate, well, a range of characters.

To begin, here are several examples of how ranges can be put together:

[rs] is a range that will match any one of two characters (r or s).

[ghi] is a range that will match any one of three characters (g, h, or i).

[A-Z] is a range that will match any uppercase letter of the alphabet.

[f-z] is a range that will match any lowercase letter of the alphabet between f and z.

[0-9] is a range that will match any single-digit number.

[a-suv] is a range that will match any character a through s, as well as u and v.

[^G] is a range that will not match the character(s) specified after the caret.

So, in what instance would a range make more sense than using a generic ? or * wildcard? I’m glad you asked. The short answer is that ranges allow you to be more specific about the results, but let’s take a look at the following examples to further answer the question.

ls gwen-pic[0-9].jjpg will list all pictures with this name, followed by a single digit number, followed by a .jpg file extension. It will not list the files if they have other names, multiple-digit numbers, or a different file extension.

ls *.[aa-zA-Z][a-zA-Z][a-zA-Z] will list all of the files in your current directory with a 3-letter file extension, regardless of case.

ls *.[^x]* will list all files in your current directory that do not begin with an x, such as .xls and .xlsx.

The {} Wildcard

I don’t really know if the curly braces {} are considered wildcards, as much as they are considered “batch operators” or “shortcuts”, but I’m sticking them in the wildcard section because I feel that they fit the bill sufficiently.

The curly braces allow many actions to be performed, either to multiple files at once, or to a single file with less typing involved. I’ll explain some common uses very briefly, and include examples of each one.

Actions can be performed on various files at the same time by putting the files and/or the appropriate wildcards in curly braces, separated by commas and no spaces: cp {*.pdf,*.xls} Desktop/

Actions that refer to the same file in the same path can be performed by typing in that path and file only once, and specifying the change within the curly braces:

touch filename{1,2,3}.txt will create multiple (in this case, three) filename.txt with the specified numbers (one in each name).

mv /file/path/filename.{txt,html} will change the text file to an HTML file.

cp /file/path/filename.html{,-old} will make a copy of the HTML file and append -old to the name of the copied file.

Curly braces may not be ideal for every task, but there are many instances where they come in handy.

The ! Wildcard

The exclamation point ! is also not so much a wildcard in itself, as it is a “logical NOT” construct.

This means that it is used to indicate what should not match, rather than what should.

The exclamation mark is used inside of the braces [], in the same way the braces would be used, except, you know, not.

Example: mv filename[!7].txt ../ will move all “filename.txt” files that do not include the number 7 in its name.

The ! and ^ symbols, used in a range, do pretty much the same thing.

Escaping Wildcards

What happens when you have a file or directory name that includes a wildcard character?

I can’t personally imagine needing to name my file is*this*a*bad*file*name?[1].vlc, but who knows what a person could come up with.

There is a way to escape wildcard characters in the command line, so that they do no act as wildcards, and are understood to be part of the file/path name.

The backslash \ character is used for this purpose: rm is\*this\*a\*bad\*file\*name\?\[1\].vlc

The backslash can also be used to escape spaces, cd My\ Music/, quotation marks, mv \"funny-file-name\".txt newname.txt and itself mv file\\name.txt filename.txt.


I expected this to be a very brief review, but looking back over it now, I see that I did not manage.

Hopefully the details provided will be helpful to you, but if you still have questions, let me know in the comments.

Leave a Reply

You have to agree to the comment policy.

Scroll to top