OS X Find in bash with regex digits \ d without creating expected results

I use the following regex search command in an OS X terminal to find a whole download of files with 8-digit file names, followed by the extension .jpg, .gif, .png or .eps. The following results do not give any results, although I told OS X / BSD to find the use of modern regex

find -E ./ -iregex '\d{8}' 

Using http://rubular.com/ (http://rubular.com/r/YMz3J8Qlgh) shows that the regex pattern gives the expected results, and OS X gives the results when input

 find . -iname '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*' 

But it seems a little long.

+8
bash regex terminal find macos
source share
5 answers

These commands work on OSX

 find -E . -iregex '.*/[0-9]{8}\.(jpg|png|eps|gif)' 

this command corresponds to 12345678.jpg, not 123456789.jpg


 find -E . -iregex '.*/[0-9]{8,}\.(jpg|png|eps|gif)' 

this command corresponds to 12345678.jpg and 123456789.jpg


 .*/ 

equal to the path to the folder or path to the subfield

+10
source share

With all your answers, I was finally able to use OSX find (10.8.1) with a regex. In order to backtrack, here are my conclusions: We use custom strings to identify clips, the template is as follows: "YYMMDDab ## 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 starting element ^ ensures that the pattern is at the beginning of the search, [0-9] {6} searches for a six-digit string, \ d does'nt work. \ D does not work for letters, A-Za-z does. The $ at the end ensures that the last search is the end of the line.

After reading the Apple man page, find and re_format, I was completely disabled regarding escape characters.

+2
source share

man re_format explains the specifics of the modern regex that find will accept.

This works for me: -iregex '[0-9]{8}'

+1
source share

It was a very open thread. I bring a solution to my own problem on the table and hopefully clarify something for you and other users looking for reliability (like me).

In my case, my mac had a bunch of duplicate photos. When macs make duplicates, they add a space and a number to the end before expansion.

IMG_0001.JPG can have complex complexity with IMG_0001 2.JPG , IMG_0001 3.JPG and so on. In my case, this went on and amounted to about 2,600 useless files.

To push things, I went to the folder in question.

 cd ~/Pictures/ 

Next, we show ourselves that we can list all the files in a directory. You will notice that in regex you must include . which says "look in this directory". In addition, you must match the entire file name, therefore .+ Must catch all other characters.

 find -E . -regex '\..+' 

Accordingly, the results will lead to strings that you will need to match, including the one mentioned earlier . i, slash / and everything else.

 ./IMG_1788.JPG ./IMG_1789.JPG ./IMG_1790.JPG ./IMG_1791.JPG 

Therefore, I cannot write this to find duplicates, because it does not include "./"

 find -E . -regex 'IMG_[0-9]{4} .+' 

but I can write this to find duplicates, because it includes "./"

 find -E . -regex '\./IMG_[0-9]{4} .+` 

or a more fancy version of .*/ , as mentioned by @ jackjr300, does the same.

 find -E . -regex '.*/IMG_[0-9]{4} .+` 

Finally, this is the confusing part. \d not recognized in BSD. [0-9] works just as well. Other users' answers provide a guide to re_format , which lists ways to write common patterns that replace things like \d funny square-colon syntax that looks like this: [:digit:] . I tried and tried, but it never works. Just use [0-9] . In my case, I spent a lot of time thinking that I should use [:space:] instead of space, but I found (as usual!) That I just needed to breathe and really read the regular expression. This turned out to be my mistake. :)

Hope this helps someone!

0
source share

I use this regex to find and remove iPhone dups:

find -E. -regex '. * / IMG_ [0-9] {4} [] 1.JPG '-print -exec rm' {} '\;

0
source share

All Articles