How to Search for Files Using Regular Expressions in Ubuntu (find -regex)

The Advanced Pattern Match

When searching for files in Ubuntu Linux, the standard -name flag is limited to basic wildcards (like *.txt). However, if you need to find files with highly complex naming conventions—such as files that end in exactly three numbers, or files that start with “backup” followed by a specific date format—basic wildcards are completely insufficient. To perform advanced pattern matching across thousands of files, you must inject full Regular Expressions (regex) directly into the search engine.

Using the find Command with -regex

The Linux find command utilizes the -regex flag to evaluate the entire file path against a strict mathematical pattern.

  1. Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
  2. To search the /var/www/ directory for any file ending in either .html or .php, type the following command exactly:
  3. find /var/www/ -type f -regex ".*\.\(html\|php\)$"
  4. Press Enter.

Matching the Entire Path

There is a massive difference between -name and -regex in the find command: -regex evaluates the entire absolute path, not just the file name. Therefore, your regex pattern must always begin with .* to account for the leading directory structure (e.g., matching /var/www/index.php, not just index.php). If you want the regex engine to ignore capitalization (for example, finding both .jpg and .JPG), simply use the -iregex flag instead. Mastering this command allows you to surgically extract specific data sets from massive file systems using a single line of code.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.