The Complex Search
When searching for files in an Ubuntu Linux terminal, standard wildcards (like *.txt) are often insufficient. If you are auditing a massive web server, you might need to find all configuration files that end in either .conf or .ini, but only if the filename begins with a number. To perform a search of this complexity, you cannot rely on basic globbing. You must instruct the Linux search engine to evaluate every single filename against a complex, mathematical pattern known as a Regular Expression (RegEx).
Using the find Command with -regex
The Linux find command utilizes the -regex flag to allow for highly advanced pattern matching across the entire directory tree.
- Open your Terminal application (Ctrl + Alt + T) or log into your server via SSH.
- To execute a RegEx search, you must write a pattern that matches the entire path of the file, not just the filename itself. For example, to find any file in the
/var/directory that ends in exactly.logor.txt, type the following command: sudo find /var/ -regex ".*\.\(log\|txt\)"- Press Enter and provide your administrator password.
Case Insensitive Matching
Because Linux is strictly case-sensitive, the previous command will completely ignore a file named Error.LOG. To force the regular expression engine to ignore capitalization while evaluating the pattern, simply swap the flag to -iregex (insensitive regular expression). This ensures that your search catches every possible variation of the file extension, regardless of how the user chose to capitalize the letters when they saved it.