5.5 Identifying and Using Metacharacters
5.5.2 Using metacharacters
Asterisk
The asterisk (*- also called "splat" or "star") is a substitution symbol that represents zero or more characters, except the leading dot on a hidden file. The asterisk is often referred to as a wildcard character. The asterisk can be very helpful with many commands such as the
ls (list), cp (copy), rm (remove) and mv (move). If there were a large number of files in a directory and you only wanted to get a listing of project files that started with p1, you could use an example like the one shown in Figure . If you enter the ls command by itself, you will see all files in the current directory. 

If you enter ls d*, you will see all files that start with the letter d plus all directories that start with d, followed by a colon, then the directory contents.

Question Mark
The question mark (?) is a substitution character that matches any single character, except for the leading dot on a hidden file. The question mark is also referred to as a wildcard character. Figure shows an example of the
ls command using the question mark in the fourth position. This indicates that the file or directory name must start with dir but any character can be in the fourth position and the file name cannot be more than four characters long.

If no file name matches the wildcard character (whether * or ?), you will receive the following error message:

$ ls z?
z?: No such file or directory
$

Square Brackets
Square brackets ([ ]) can be used to match a set or range of characters for a single character position in the file or directory name. The characters inside the brackets do not generally need to be in any order; for example, [abc] is the same as [cab]. However, if you are looking for a range of characters, they must be in proper order (for example, [a-z] or [3-9]). If you want to search for all alphabetic characters, whether lowercase or uppercase, use [A-z] for the pattern to match. You can use alphabetic or numeric characters for the search pattern.

Figure shows two examples using square brackets along with the asterisk wildcard character. The first example defines a range and will list all files and directories that start with the lower case letters b through f with anything after that. The second example specifies that the first character must be either the letter a or f and anything can be after that. Notice that only files starting with f are displayed in example 2.

Semicolon
The semicolon (;) enables you to enter multiple commands on a single command line before pressing enter. Entering multiple commands on one command line is an efficient way to accomplish a task. The semicolon is also referred to as the command separator. Figure shows two examples using the semicolon to separate commands. In the first example, the
clear command will clear the screen, the cd command will return you to your home directory and the ls command will list files in that directory. The second example displays the current date and time, and then the calendar for the current month.

Interactive Media Activity (Flash, 95 kB)
  Using Metacharacters

In this media activity, you are logged in as user2 and your current working directory is: /home/user2. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.

 

Lab Activity (Adobe Acrobat Reader, 44 kB)
  Directory Listings with Metacharacters

In this lab you will work with various metacharacters and use them with the ls command to refine your directory listings.