-   Programming (
-   -   find with a regex path (

bdoremus 05-05-2010 09:08 PM

find with a regex path
I'm new to bash scripting and regexp, so sorry if this is a simple question.

I'm using bash scripting to find any file that matches a path governed by the following regular expression:
to match files like:

I've tried:

find ./ -regextype posix-egrep -regex $myRegex

and it just spins and never gives me an answer, even though the file structure isn't that big.

what am I missing?

Tinkster 05-05-2010 09:19 PM

This works for me:

find ./ -regextype posix-egrep -regex $myRegex

Bear in mind that with regex:

"This is a match on the whole path, not a search."


grail 05-05-2010 09:20 PM

Have you tried it without using a variable?

Tinkster 05-05-2010 09:22 PM


Originally Posted by grail (Post 3958737)
Have you tried it without using a variable?

The variable works just fine ;}

bdoremus 05-05-2010 09:40 PM

I've narrowed it down to the OR that is giving me problems.

I tried creating a simple file structure with some blank files in it, but this doesn't return any matches.

# mkdir ./test
# mkdir ./test/folder1
# mkdir ./test/folder2
# touch ./test/folder2/file1
# touch ./test/folder2/file2
# mkdir ./test/folder1/file3
# mkdir ./test/folder1/file2
# find ./ -regextype posix-egrep -regex "./test/(folder1|folder2)/"

I've also tried giving the regex the full path "/home/user/Desktop/(folder1|folder2)/" to no avail.


Tinkster 05-05-2010 09:53 PM

Look at my first response ... it's NOT the or, it's the missing ".*" at the beginning/end,
or the fact that you're not searching from the location where your absolute path starts.

Looking at your example above,

$ cd tmp/
$ mkdir -p test/dir1
$ mkdir -p test/dir2
$ touch test/dir1/file1
$ touch test/dir1/file2
$ touch test/dir2/file3
$ touch test/dir2/file4
$ find ./ -regextype posix-egrep -regex "./(dir1|dir2)/"
$ find ./ -regextype posix-egrep -regex ".*/(dir1|dir2)/.*"


bdoremus 05-05-2010 09:58 PM

wow, I've spent hours on this.

Thanks, Tink!!!!!

Tinkster 05-05-2010 10:24 PM

Pleasure ... ;}

Feel free to push the thanks button. :D

All times are GMT -5. The time now is 01:25 AM.