-1

I am trying to search some files in a specific directory called Dico with find command e.g.

  • I want to find all files ending with g : find ./Dico -name "*g works but find ./Dico -name ".*g or find ./Dico -name ".*g$ do not. Can someone explain me why ?
  • Another example would be to find : all files that start with a number followed by exactly 5 characters (lowercase) : find ./Dico -name "\[0-9\]+[a-z]\{5\}" or find ./Dico -name "\d+[a-z]{5}". In that case +, \d+ and {n} do seem to do nothing.. I've tried both {5} and \{5\} (emacs syntax) but still the special characters seem to not work correctly.

I am on Ubuntu 20.04. Thank you in advance.

Drew
  • 29,895
  • 7
  • 74
  • 104
Artemis
  • 145
  • 7
  • I have also tried ```find ./Dico -type f -regex "g$"``` and still does not return what I expected. – Artemis Oct 06 '22 at 18:13
  • 1
    You should really use `'` as a string delimiter when you intend to escape all of its content instead of `"`. Try that and it should work. – jthulhu Oct 06 '22 at 18:56
  • @BlackBeans just tried ```find ./Dico -name '\[0-9\]+[a-z]\{5\}'``` and still does not work.. – Artemis Oct 06 '22 at 19:07
  • 2
    for this one, just do `man find` (hint: search for `regex`). Note: a shell pattern is not a regular expression. – jthulhu Oct 06 '22 at 19:21
  • @BlackBeans thak you. Apparently I was trying to merge shell patterns with regexes. – Artemis Oct 07 '22 at 06:53

1 Answers1

1

First of all, -name does not use regex.

-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern.

A matching shell pattern could look like this:

find /Dico -name '[0-9][a-z][a-z][a-z][a-z][a-z]'

To use regex, you need to instead use the -regex option (and -regextype to select the regex dialect you want).

-regex pattern
File name matches regular expression pattern. This is a match on the whole path, not a search.

I selected the regex dialect egrep:

find /Dico -regextype egrep -regex '.*/[0-9]+[a-z]{5}'

You can do find -regextype help (or just about any invalid dialect) to get a list of the supported regex dialects.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Thank you for your reply. This works as expected, but I was trying to recreate the same result with ```find``` command. – Artemis Oct 07 '22 at 06:54
  • @Artemis You're welcome. What does _"but I was trying to recreate the same result with `find` command"_ mean? The above is all about the `find` command. – Ted Lyngmo Oct 07 '22 at 06:57
  • Yes you are right, I meant with shell patterns. – Artemis Oct 07 '22 at 06:59
  • @Artemis Now I'm confused. You tagged the question [tag:regex] and also used regex to try to find the file. If you are actually trying to use a shell pattern to match the file, then why tag it regex? Anyway, I updated the answer with a matching shell pattern. – Ted Lyngmo Oct 07 '22 at 07:13
  • @Artemis Did the shell pattern work? – Ted Lyngmo Oct 07 '22 at 14:07