Wildcards are used to match multiple files in a shell. The two main wildcards are * and ? . * matches any number of characters, so *.jpg is expanded by the shell to a list of all files with a .jpg extension. It is important to understand that this expansion is done by the shell before passing the list of files to whatever command you’ve called. If you have two files called in.txt and out.txt, and run $ ls *.txt ,the shell replaces the wildcard expression with a list of matching files, then calls the command, so it runs: $ ls in.txt out.txt
The command has no idea that a wildcard has been used. The other commonly used wildcard is ? , which matches any single character: a?c.txt matches abc.txt or aBc.txt, but not ac.txt or abbc.txt. One character not matched by these is the directory separator / . *.txt matches abc.txt but not abc/def.txt; you need */*.txt to match the latter. Some shells, such as ZShell, use ** to match any character, including directory separators, so you can list files in the current directory and those below it with: $ ls **/*.jpg
Shell wildcards (or globbing) are not the same as regular expressions, although some aspects are similar. The shell also accepts ranges of characters using brackets: $ ls [abcd]* $ ls [a-d]*
Both of these list all files starting with a, b, c or d. What happens if you have a file with a *, [ or ? in the name? It’s not usually a clever idea to do this but it may be beyond your control. The answer is to ‘escape’ the wildcard with a backslash. If you run the following, the shell will ignore the wildcard and ls will try to show a file called *.txt, (although creating such a file in the first place would not be the best idea you ever had): $ ls \*.txt