I'm using the following regex find command in OS X terminal to find a whole load of files that have 8 digit file names followed by either a .jpg, .gif, .png or .eps extension. The following produces no results even though I've told OS X/BSD find to use modern regex
find -E ./ -iregex '\d{8}'
Using http://rubular.com/ (http://rubular.com/r/YMz3J8Qlgh) shows that the regex pattern produces the expected results and OS X produces the results when typing
find . -iname '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*'
But this seems a little long winded.
In regex, the uppercase metacharacter denotes the inverse of the lowercase counterpart, for example, \w for word character and \W for non-word character; \d for digit and \D or non-digit.
You can use the test construct, [[ ]] , along with the regular expression match operator, =~ , to check if a string matches a regex pattern (documentation). where commands after && are executed if the test is successful, and commands after || are executed if the test is unsuccessful.
Since version 3 (circa 2004), bash has a built-in regular expression comparison operator, represented by =~. A lot of scripting tricks that use grep or sed can now be handled by bash expressions and the bash expressions might just give you scripts that are easier to read and maintain.
Regex is a very powerful tool that is available at our disposal & the best thing about using regex is that they can be used in almost every computer language. So if you are Bash Scripting or creating a Python program, we can use regex or we can also write a single line search query.
These commands works on OSX
find -E . -iregex '.*/[0-9]{8}\.(jpg|png|eps|gif)'
this command matches 12345678.jpg , not 123456789.jpg
find -E . -iregex '.*/[0-9]{8,}\.(jpg|png|eps|gif)'
this command matches 12345678.jpg and 123456789.jpg
.*/
equal the folder path or the subFolder path
With all your answers, i was finally able to use OSX find (10.8.1) with regex. For giving back, here are my findings: We use custom strings to identify clips, the pattern goes like this: "YYMMDDabc##abc*.ext": Year/Month/Day/3chars/2digits/3chars/whatever/ext
find -E /path/to/folder -type f -regex '^/.*/[0-9]{6}[A-Za-z]{3}[0-9]{2}[A-Za-z0-9]{3}\.*.*\.(ext)$'
The initial ^ makes sure the pattern is at the beginning of the search, [0-9]{6} searches for a 6 digit string, \d does'nt work. \D doesn't work for letters, A-Za-z does. The $ in the end makes sure the last search is the end of the string.
After reading Apples manpage about find and re_format i was completely off track regarding escaping characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With