Wildcard Selection in Bash

Select zero or more characters in a file name or a path using *

In the Bash command line interface (CLI), the wildcard character is the asterisk (*). It is used to match zero or more characters in a file name or a path.

For example, the command ls *.txt would list all files in the current directory that have the ".txt" extension. The asterisk is a placeholder for any characters that might appear before or after the ".txt" extension.

Here are a few more examples of how the wildcard character can be used in the Bash CLI:

  • cp * /target/directory: This command would copy all files in the current directory to the /target/directory directory
  • rm a*.txt: This command would delete all files in the current directory that have names that begin with the letter “a” and end with the “.txt” extension
  • grep 'ERROR' *.log: This command would search all files in the current directory that have the “.log” extension for the string “ERROR”

You can also use multiple wildcards in a single command. For example:

  • ls a*b*.txt: This command would list all files in the current directory that have names that begin with the letter “a”, followed by zero or more characters, followed by the letter “b”, followed by zero or more characters, and end with the “.txt” extension
  • mv [a-z]*.jpg /target/directory: This command would move all files in the current directory that have names that begin with a lowercase letter and end with the “.jpg” extension to the /target/directory directory

there are several special characters that are used in combination with the wildcard character (*) to match specific patterns in file names or paths.

Here are a few examples of wildcard special characters and how they can be used:

  • ?: This character matches any single character. For example, ls a?b.txt would match any file with a name that has a “b” surrounded by an “a” and any single character, such as “a1b.txt” or “aXb.txt”.
  • [...]: This character class matches any single character that is contained within the brackets. For example, ls [abc]*.txt would match any file with a name that begins with an “a”, “b”, or “c”, followed by zero or more characters, and ends with the “.txt” extension.
  • [!...]: This negated character class matches any single character that is not contained within the brackets. For example, ls [!abc]*.txt would match any file with a name that begins with a character that is not an “a”, “b”, or “c”, followed by zero or more characters, and ends with the “.txt” extension.
  • [a-z]: This range of characters matches any single character that is within the specified range of ASCII values. For example, ls [a-z]*.txt would match any file with a name that begins with a lowercase letter, followed by zero or more characters, and ends with the “.txt” extension.

You can also use multiple wildcard special characters in a single command. For example:

  • ls a[a-z]*b?c.txt: This command would match any file with a name that has an “a” followed by zero or more lowercase letters, followed by a “b”, followed by any single character, followed by a “c”, and ends with the “.txt” extension (e.g. “aabbc.txt” or “aaxb1c.txt”)
  • ls a*[!abc].txt: This command would match any file with a name that has an “a” followed by zero or more characters, followed by a character that is not an “a”, “b”, or “c”, and ends with the “.txt” extension (e.g. “a1.txt” or “ax.txt”)

Match any single character as wildcard using ?

the ? character is a wildcard that is used to match any single character in a file name or path.

For example, the command ls a?b.txt would match any file with a name that has a "b" surrounded by an "a" and any single character, such as "a1b.txt" or "aXb.txt".

Here are a few more examples of how the ? character can be used in the Bash CLI:

  • ls ???.txt: This command would match any file with a name that has three characters followed by the “.txt” extension (e.g. “abc.txt” or “123.txt”)
  • rm file??.txt: This command would delete any file with a name that starts with “file” followed by two characters and ends with the “.txt” extension (e.g. “file01.txt” or “fileXX.txt”)
  • grep 'ERROR' log-file?.log: This command would search for the string “ERROR” in any file with a name that starts with “log-file” followed by a single character and ends with the “.log” extension (e.g. “log-file1.log” or “log-fileA.log”)

You can also use the ? character in combination with other wildcard special characters. For example:

  • ls a[a-z]?b?c.txt: This command would match any file with a name that has an “a” followed by a lowercase letter, followed by a single character, followed by a “b”, followed by a single character, followed by a “c”, and ends with the “.txt” extension (e.g. “aaxbbc.txt” or “aazb1c.txt”)
  • ls a*[!abc]?.txt: This command would match any file with a name that has an “a” followed by zero or more characters, followed by a character that is not an “a”, “b”, or “c”, followed by a single character, and ends with the “.txt” extension (e.g. “a1x.txt” or “ax1.txt”)

Character set matching [...] and NOT matching [!...]using wildcard

the [...] wildcard special character to match a set of characters in a file name or path. This is known as a character class.

For example, the command ls [abc]*.txt would select all files in the current directory that have names that begin with an "a", "b", or "c", followed by zero or more characters, and end with the ".txt" extension.

You can also specify a range of characters using the - character. For example, ls [a-z]*.txt would select all files in the current directory that have names that begin with a lowercase letter, followed by zero or more characters, and end with the ".txt" extension.

You can use the [!...] wildcard special character to match any character that is not contained within the brackets. For example, ls [!abc]*.txt would select all files in the current directory that have names that begin with a character that is not an "a", "b", or "c", followed by zero or more characters, and end with the ".txt" extension.

You can also use multiple character classes in a single command. For example:

  • ls [a-zA-Z]*.txt: This command would select all files in the current directory that have names that begin with a letter (either lowercase or uppercase), followed by zero or more characters, and end with the “.txt” extension
  • ls [0-9a-zA-Z]*.txt: This command would select all files in the current directory that have names that begin with a digit or a letter (either lowercase or uppercase), followed by zero or more characters, and end with the “.txt” extension

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book