Regex match filename

The Linux command line comes with many options that we can use in order to search for files. One of the most powerful features is regex (regular expression) pattern matching. This convention allows us to search our file system based on very granular name patterns found inside the file names – for example, the ability to search for all files that start with an A and end with a K. In regex, this would be written ^A.*K$.

In this tutorial, you will see how to use regular expressions to match file names on a Linux system. This will allow you to find and isolate files with very granular precision. Even if you only have very little information about a file name, there is usually some sort of regex pattern that can find it for you. Let’s go over some command examples below.

In this tutorial you will learn:

  • How to match patterns and use pipes
  • What are meta characters and what do they do?
  • How to search for file names using regular expressions
Regex match filename
Regex match filename
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ls, grep
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Matching patterns and using pipes




Two of the tools we will be using in this tutorial are ls and grep. These commands can be used to find a file by its name with ease. Here is an example:

$ ls | grep file
somefile1.txt

The pipe | character allows us to pass the output of ls over to the grep command, which will then display to us only the output which meets are search requirement, which in this case is “file.” Thus, it was able to find somefile1.txt in the current directory.

Regular expression meta characters

Regular expressions come in handy because they are able to isolate file names based on more complex patterns. Here is an example where we search for all files that start with s and end with .txt file extension.

$ ls | grep ^s.*.txt$
somefile1.txt

In this case, we used four meta characters, . which matches any character, * matches the character immediately before it any number of times, ^ which indicates the start of the string, and $ which indicates the end of the string.

There are a myriad of other meta characters in addition to these. Check the table below for a full list.

Meta Character Description
. (period) Matches any one character no matter what the character is
? Matches the character immediately before it either zero times or one time
* Matches the character immediately before it any number of times including zero (the character may not be in the string at all)
+ Matches the character immediately before it one or more times (the character must be in the string at least once)
^ Indicates that the characters which follow are at the start of the string only
$ Indicates that the characters which precede it are at the end of the string
\d Matches any decimal digit
\D Matches any character that is not a decimal digit
\s Matches a tab or space character
\S Matches any character that is not a tab or a space
\w Matches any letter, any digit, or the underscore character
\W Matches any character which is not a letter, a digit, or the underscore
\ Escape character allowing the use of any of the metacharacters with their regular keyboard meaning. For instance, \. matches a period (.) in a regular expression. A period (.) matches any one character no matter what the character is.

Regex match filename examples

Based on the knowledge above we can combine all what we have learned into some more complicated Linux command line examples that use regular expression. Simply put, a regular expression allows us to search for a pattern within a string by use of meta characters.

  1. Match file names that start with a g character.


    $ ls | grep ^g
    
  2. Match file names that end with a S character.
    $ ls | grep S$
    
  3. Match a set of characters by using square brackets. For example, match file names that begin with either o or i characters:
    $ ls | grep ^[o,i]
    
  4. Match files names that have a set number of wildcard characters with the . meta character. For example:
    $ ls | grep file..txt
    file1.txt
    file2.txt
    file3.txt
    

    The previous command would not match a file such as file10.txt since there are two characters between “file” and “.txt”.

Closing Thoughts

In this tutorial, we saw how to match file names using regular expressions on a Linux system. We learned how using meta characters can be used to search for file names using very high precision. Finally, we took a tour of some of the more used regular expressions operators by seeing examples with the grep command.