Which pattern will match only filenames that begin with an uppercase letter?

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively.

Read Also: 10 Useful Practical Examples on Chaining Operators in Linux

These wildcards are interpreted by the shell and the results are returned to the command you run. There are three main wildcards in Linux:

  • An asterisk
    $ ls -l l*	
    
    4 – matches one or more occurrences of any character, including no character.
  • Question mark
    $ ls -l l*	
    
    5 – represents or matches a single occurrence of any character.
  • Bracketed characters
    $ ls -l l*	
    
    6 – matches any occurrence of character enclosed in the square brackets. It is possible to use different types of characters (alphanumeric characters): numbers, letters, other special characters etc.

You need to carefully choose which wildcard to use to match correct filenames: it is also possible to combine all of them in one operation as explained in the examples below.

How to Match Filenames Using Wildcards in Linux

For the purpose of this article, we will use following files to demonstrate each example.

createbackup.sh  list.sh  lspace.sh        speaker.sh
listopen.sh      lost.sh  rename-files.sh  topprocs.sh

1. This command matches all files with names starting with

$ ls -l l*	
7 (which is the prefix) and ending with one or more occurrences of any character.

$ ls -l l*	
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
List Files with Character

2. This example shows another use of

$ ls -l l*	
8 to copy all filenames prefixed with
$ ls -l l*	
9 and ending with one or more occurrences of any character.

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
List and Copy All Files

3. The following command matches all files with names beginning with

$ ls -l l*	
7 followed by any single character and ending with
$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
1 (which is the suffix).

$ ls l?st.sh	
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Match File with Character Name

4. The command below matches all files with names starting with

$ ls -l l*	
7 followed by any of the characters in the square bracket but ending with
$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
1.

$ ls l[abdcio]st.sh 
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Matching Files with Names

How to Combine Wildcards to Match Filenames in Linux

You can combine wildcards to build a complex filename matching criteria as described in the following examples.

5. This command will match all filenames prefixed with any two characters followed by

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
4 but ending with one or more occurrence of any character.

$ ls
$ ls ??st*
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Match File Names with Prefix

6. This example matches filenames starting with any of these characters

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
5 and ending with one or more occurrence of any character.

$ ls
$ ls [clst]*
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Match Files with Characters

7. In this examples, only filenames starting with any of these characters

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
5 followed by one of these
$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
7 and then any single character, followed by a
$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
8 and lastly, one or more occurrence of any character will be listed.

$ ls
$ ls [clst][io]?t*
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
List Files with Multiple Characters

8. Here, filenames prefixed with one or more occurrence of any character, followed by the letters

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
9 and ending with one or more occurrence of any character will be removed.

$ ls
$ rm *tar*
$ ls
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Remove Files with Character Letters

How to Match Characters Set in Linux

9. Now lets look at how to specify a set of characters. Consider the filenames below containing system users information.

$ ls

users-111.list  users-1AA.list  users-22A.list  users-2aB.txt   users-2ba.txt
users-111.txt   users-1AA.txt   users-22A.txt   users-2AB.txt   users-2bA.txt
users-11A.txt   users-1AB.list  users-2aA.txt   users-2ba.list
users-12A.txt   users-1AB.txt   users-2AB.list  users-2bA.list

This command will match all files whose name starts with

$ ls l?st.sh	
0, followed by a number, a lower case letter or number, then a number and ends with one or more occurrences of any character.

$ ls -l l*	
0

The next command matches filenames beginning with

$ ls l?st.sh	
0, followed by a number, a lower or upper case letter or number, then a number and ends with one or more occurrences of any character.

$ ls -l l*	
1

This command that follows will match all filenames beginning with

$ ls l?st.sh	
0, followed by a number, a lower or upper case letter or number, then a lower or upper case letter and ends with one or more occurrences of any character.

$ ls -l l*	
2
Which pattern will match only filenames that begin with an uppercase letter?
Which pattern will match only filenames that begin with an uppercase letter?
Match Characters in Filenames

How to Negate a Set of Characters in Linux

10. You can as well negate a set of characters using the

$ ls l?st.sh	
3 symbol. The following command lists all filenames starting with
$ ls l?st.sh	
0, followed by a number, any valid file naming character apart from a number, then a lower or upper case letter and ends with one or more occurrences of any character.

$ ls -l l*	
3

That’s all for now! If you have tried out the above examples, you should now have a good understanding of how wildcards work to match filenames in Linux.

You might also like to read these following articles that shows examples of using wildcards in Linux:

Which command displays the absolute path name of the current location?

The pwd command displays the full, absolute path of the current, or working, directory.

Which directory is the administrative superuser's home directory?

/root/ is the home directory of the superuser "root" /sbin/ and /usr/sbin/ store system commands. /tmp/ is the system temporary directory.

Which command is used to return to the current user's home directory?

The cd command can be used to change into a subdirectory, move back into the parent directory, move all the way back to the root directory or move to any given directory.

What is the name of the device file of an entire SATA hard drive in the dev directory?

Device files are created automatically by the operating system. In Red Hat Enterprise Linux, the first SATA/PATA, SAS, SCSI, or USB hard drive detected is called /dev/sda, the second is /dev/sdb, and so on. These names represent the entire hard drive.