Using Find Command With Regex
Enable the beast mode of the find command by using regex for your search.
The find command is a powerhouse for searching files based on a number of criteria.
You can enable the beast mode in the find command by using regular expression (regex) for searching.
But before jumping to the examples part, it is crucial to know some basic regex tokens and syntax.
Quick Introduction to Regex Tokens
Tokens are nothing but special characters to search for specified patterns.
So let's have a look at some of the most basic and widely used tokens which I'll be using with the find command:
|It gets you a match for any character once (except a new line). So a.b will match strings such as acb, aeb and abb but won't match accb or ab
|It eliminates the effect of special characters such as the (.) will indicate to period effect but when used a.b it will only search for strings as a.b
|It is known as a repeater symbol. This means the preceding character can be found 0 or more times. So the ca*t will find get you ct, cat, caat etc.
|Square brackets ()
|It will get a positive result of any character used in a string inside the square brackets. This means a[bc]d will match abd or acd, but not the abcd.
|Generally, it is sued to specify the starting point of search but can also be used to negate the content when used inside the square brackets [ ^ ]. Means a[^bc]d will get you aed, azd but not abd or acd.
Now, Let's have a look at the basic syntax of using find with regex:
find [path] -regex [regular_expression]
[path]is where you want to search files.
regular_expressionis where you will be using tokens to express the file pattern you are looking for.
Now it's time for me to share some examples of how you can use find with regex.
Practical Examples of find command with regex
I am going to start with the most common scenario where a user only knows the first few characters of a file and wants to know where it is.
Search Files based on Initial Characters in the Current Directory
Currently, my file system looks like this:
And I want to search for files that start with
Fr so my command will be:
find ./ -type f -regex '\.\/F[or].*'
-type f was used to search for files,
.\/ was used to look for files in the current directory and
F[or] will show us file names starting from Fo and Fr.
But what if you want to execute some commands/programs over the given result? This can be done using the find command with exec:
Search Files in Sub Directory
The above example only applied to the current directory and did not show some files that followed the same naming pattern.
So I'll be using the same naming pattern
F[or] to find files in the subdirectory:
find ./ -type f -regex '\.\/[^/]*\/F[or][^/]*'
Seems too complex right? Let me break it down for you.
[^/]*\/ referees to the files that do not contain any back slashes which eliminates the possibility of finding files in the current directory.
And in the end, I've replaced period ( . ) with
[^/] to not expand search than the first subdirectory by mentioning there should be no slashes after the filename.
Search Files through regex patterns in every Subdirectory
Seems quite complex after going searching in a single subdirectory right? Well, this is going to be the easiest one!
Well, two asterisks and that is it! Let me show you how:
find ./ -type f -regex '.*F[or].*'
And if you are curious about how it worked, it's because I used asterisks at the beginning and the end, so it went through every possibility.
Search Files based on Extension
First, let me share the general syntax of how you are supposed to search files based on their extensions:
find ./ -type f -regex ".*\[fileextension]"
So let's suppose I want to find all the text files (having a .txt extension) and that can be done quite easily by the given command:
find -regex -type f ".*\.txt"
Search Files based on Filename and Extension
This is my personal favorite implementation of regex with find as you can search files based on first letters and their extensions making it quite convenient.
First, let's have a look at the syntax:
find ./ -type f -regex '\.\/[Filename].*\.[extension]'
Let's make it a bit practical. So I'm in a scenario where I only know the first few letters of the file (started with Fo or Fr) and its extension (.sh):
find ./ -type f -regex '\.\/F[or].*\.sh'
From finding files modified in n minutes to executing scripts over results with exec, find is one of the most extensive commands offering over 50+ options.
This guide explained yet another way to use the find command making you one step advanced in your Linux journey.
While this guide was kept simple, if you still have any doubts, let me know in the comments.