Why regex pattern doesn't work in find?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why regex pattern doesn't work in find?
# 1  
Old 04-04-2013
Why regex pattern doesn't work in find?

Code:
[rootie@typo1 ~]$ find /opt/data_* -maxdepth 3 -type d -name "main*" 2> /dev/null
/opt/data_025/maindblogs
/opt/data_026/maindblogs
/opt/data_027/maindblogs
/opt/data_028/maindblogs
[rootie@typo1 ~]$ find /opt/data_* -maxdepth 3 -type d -name "rvlogs*" 2> /dev/null
/opt/data_002/prod/rvlogs2_archive
/opt/data_003/prod/rvlogs2_archive
/opt/data_025/rvlogs2_archive
/opt/data_026/rvlogs2_archive
/opt/data_027/rvlogs2_archive
/opt/data_028/rvlogs2_archive
[rootie@typo1 ~]$ find /opt/data_* -maxdepth 3 -type d -regex "\(rvlogs.*\|main.*\)" 2> /dev/null
[rootie@typo1 ~]$

OS:RHEL5.3 shell:bash
# 2  
Old 04-04-2013
This baffled me at first too, so I checked the source:

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

I can see that this'd be inconvenient sometimes... I don't know why they did that.

You can specify multiple -file types instead, like:

Code:
'(' -name 'rvlogs*' -o -name 'main*' ')'

# 3  
Old 04-04-2013
As i understood this should do the trick
Code:
find /opt/data_* -maxdepth 3 -type d -regex \(.*rvlogs.*\|.*main.*\) 2> /dev/null

But it didn't.
# 4  
Old 04-04-2013
You forgot to quote it, so the shell ate all your backslashes, turning it into (.*rvlogs.*|.*main.*)

I wouldn't trust the -regex option though, if it considers the entire path, because how do you know you're matching 'main' in the folder name itself and not something like /opt/data_main/folder_you_do_not_want ? I'd stick with -name.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-04-2013
It works. Thanks alot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

2. Shell Programming and Scripting

Regex in sed to find specific pattern and assign to variable

(5 Replies)
Discussion started by: radioactive9
5 Replies

3. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

4. UNIX for Advanced & Expert Users

find -exec with 2 commands doesn't work (error incomplete staement)

Hi Gurues, I need to modify an existing script that uses find to search a folder, and then move its contents to a folder. What I need to do is run gzip on each file after it's moved. So, I ran this little test: Put a ls.tar file on my $HOME, mkdir tmp, and then: virtuo@tnpmprd01: find .... (3 Replies)
Discussion started by: llagos
3 Replies

5. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

6. Shell Programming and Scripting

perl - how do i find out if a file doesn't contain a pattern?

how do i check a file for a pattern and perform an action if it doesn't exist? i know how to search a file for a pattern. you just place it in an array like so. #!/usr/bin/perl my $data_file = "file.txt"; open DATA, "$data_file"; my @array_of_data = <DATA>; if ($_ =~ m/pattern/i) {... (4 Replies)
Discussion started by: mjays
4 Replies

7. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

8. UNIX for Dummies Questions & Answers

bash pattern matching echo *[! '/' ] doesn't work

without using ls, just using echo so purely pattern matching I can say echo */ <-- lists directories but how would I match files? surely something like *!/ or * but neither work ? it seems like there isn't much that I can put in but surely i should be able to put any ascii... (1 Reply)
Discussion started by: james hanley
1 Replies

9. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies

10. UNIX for Dummies Questions & Answers

How to find size of each subdirect? du -sk | ls doesn't work

Using solaris 2.5.1, and how can I get a summary of the size of each subdirectory, say for /export/home, all the users? usually I do a du -sk dirname but I have to manually type in each name, is there a better way? Thanks, (3 Replies)
Discussion started by: kymberm
3 Replies
Login or Register to Ask a Question