LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   find with a regex path (http://www.linuxquestions.org/questions/programming-9/find-with-a-regex-path-806206/)

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:
"(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/"
to match files like:
monthly.9/home/user1/.mailbox/l23131564
nightly.15/home/user2/.mailbox/cur/6546213

I've tried:
Code:

myRegex="(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/"
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:
Code:

myRegex='.*(monthly|nightly).[0-9]+/home/(user1|user2)/.mailbox/.*'
find ./ -regextype posix-egrep -regex $myRegex

Bear in mind that with regex:
Code:

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

Cheers,
Tink

grail 05-05-2010 09:20 PM

Have you tried it without using a variable?

Tinkster 05-05-2010 09:22 PM

Quote:

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.
Code:

# 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.

Help?!

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,
Code:

$ 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)/.*"
./test/dir1/file1
./test/dir1/file2
./test/dir2/file3
./test/dir2/file4
$


Cheers,
Tink

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 11:25 AM.