How to Use Bash Shell metacharacters in Linux

Using the Shell Metacharacters

Bash shell metacharacters are specific characters, generally, symbols, that have special meaning to the shell. Three types of metacharacters are pathname metacharacters, file name substitution metacharacters, and redirection metacharacters.

Using the Path Name Metacharacters

Some of the shell metacharacters have specific path name functions. These metacharacters simplify location changes within the directory hierarchy. Some examples of path name metacharacters are:

~ ~ username

Tilde (~) Character

The tilde (~) character represents the home directory of the current user. It is a substitution that equates to the absolute path name of the user. To change directories to dir1 by using the ~ character, perform the following command:

$ cd ~/dir1
$ pwd
/export/home/user1/dir1/
$

Tilde (~) Character With a User Name

The tilde (~) character followed by a user name represents the home directory of the specified user. To change directories to the user2 home directory, perform the following command:

$ cd ~user2
$ pwd
/export/home/user2
$

Dash (-) Character

The dash (-) character in the shell represents the previous working directory. You can use the dash character to switch between two specific directories. The shell automatically displays the current directory path.

To switch between the user1 and tmp directories, perform the following command:

$ cd
$ pwd
/export/home/user1
$ cd /tmp
$ pwd
/tmp
$ cd
/export/home/user1
$ cd
/tmp
$

Using the File Name Substitution Metacharacters

You can substitute some shell metacharacters for other characters. These metacharacters simplify commands. Some examples of file name substitution metacharacters are:

*
?
[]

Asterisk (*) Character

The asterisk (*) character is also called the wild card character and represents zero or more characters, except the leading period (.) of a hidden file. To list all files and directories that start with the letter f followed by zero or more other characters, perform the following commands:

$ cd
$ ls f*
feathers    file.1      file.2      file.3      file4       fruit2
feathers_6  file1       file2       file3       fruit
$

To list all files and directories that start with the letter d followed by zero or more other characters, perform the following command:

$ ls d*
dante    dante_1

dir1:
coffees  fruit    trees
dir2:
beans  notes recipes
dir3:
cosmos moon planets space sun vegetables
dir4:
constellation memo roses
dir5:
dir10:
planets
$

To list all files and directories that end with the number 3, preceded by zero or more characters, perform the following command:

$ ls *3
file.3  file3

dir3:
cosmos moon planets space sun vegetables
$

Question Mark (?) Character

The question mark (?) character represents any single character except the leading period (.) of a hidden file. The question mark character is also called a wild card character.

To list all files and directories that start with the string dir and followed by one other character, perform the following command:

$ ls dir?
dir1:
coffees  fruit    trees

dir2:
beans  notes recipes

dir3:
cosmos moon planets space sun vegetables

dir4:
constellation memo roses

dir5:
$

If no files match an entry with the question mark, an error message appears.

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

Square Bracket ([]) Characters

The square bracket ([]) characters represent a set or range of characters for a single character position. A set of characters is any number of specific characters; for example, [acb]. The characters in a set do not generally need to be in any order. For example, [abc] is the same as [cab].

A range of characters is a series of ordered characters. A range lists the first character, a hyphen (-), and the last character, for example, [a–z] or [0–9]. When you specify a range, arrange the characters in the order that you want them to appear in the output. Use [A–Z] or [a–z] to search for any uppercase or lowercase alphabetical character, respectively.

To list all files and directories that start with the letters a through f, perform the following command:

$ ls [a-f]*
brands      dante_1     file.1      file2       file4
celery      feathers    file1       file.3      fruit
dante       feathers_6  file.2      file3       fruit2

dir1:
coffees  fruit    trees

dir10:
planets

dir2:
beans    notes    recipes

dir3:
cosmos      moon        planets     space       sun         vegetables

dir4:
constellation  memo           roses

dir5:
$

To list all files and directories that start with the letters f or p, perform the following command:

 $ ls [fp]*
feathers    file.1      file.2      file.3      file4       fruit2
feathers_6  file1       file2       file3       fruit

perm:
group   motd    skel    vfstab

practice1:
appointments  file.1        file.2        play
$