How to use regular expressions in 'find' searches in Linux?
Published on Aug. 22, 2023, 12:17 p.m.
To use regular expressions in ‘find’ searches in Linux, you can use the ‘-regex’ option followed by the regular expression you want to use to match file names.
For example, the following command will find all files in the current directory and its subdirectories that have file names starting with “log” and ending with a “.txt” extension:
find . -type f -regex "./log.*\.txt"
In this example, the regular expression “./log.*.txt” matches file names starting with “log” and ending with a “.txt” extension.
You can also use different types of regular expressions, such as extended regular expressions and Perl-style regular expressions. The ‘-regextype’ option is used to specify the type of regular expression you want to use. For example:
find . -type f -regextype posix-extended -regex "./log.*\.(txt|pdf)"
This will find all files in the current directory and its subdirectories that have file names starting with “log” and ending with either a “.txt” or “.pdf” extension using the POSIX extended regular expression type.