3

I have a file foo in the current directory. Then I run the following commands:

  1. find . -regex 'foo' finds nothing
  2. find . -regex 'foo.*' finds nothing
  3. find . -regex '.*foo' finds the file

I would expect that either of these commands should yield a positive result.

find command version is 4.4.2

2 Answers 2

5

from the manual:

   -regex pattern
          File name matches regular expression pattern.  This is  a  match
          on  the  whole path, not a search.  For example, to match a file
          named `./fubar3', you can use the regular expression `.*bar.' or
          `.*b.*3',  but  not `f.*r3'.  The regular expressions understood
          by find are by default Emacs Regular Expressions, but  this  can
          be changed with the -regextype option.

The important part is: This is a match on the whole path, not a search.

So only the third regex yields the result:

./foo
Sign up to request clarification or add additional context in comments.

2 Comments

Do I get it right that a proper regex for the second case would be '\./foo.*' then?
@SergeyPauk yes and '\./foo' for the first :-)
0

find actually try to match the string ./foo (differently from the -name switch) so the third pattern -regex '.*foo' have to works if the file foo have no extension.

Try to use:

``find . -regex '.*foo.*'``

And report the output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.