find -regex option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find -regex option
# 1  
Old 04-26-2011
find -regex option

I'm trying to use regular expression arguments as variables. I have to surround the regular expression with double quotes or else it automatically expands that regular expression to whatever is in that directory.

Unfortunately when I run 'find' it further surrounds the double quotes with single quotes and the regular expression doesn't even get evaluated. I tried removing the double quotes inside the command but once again the regular expression gets expanded. I guess if there was a way to pass the argument only as double quotes without being wrapped in the annoying single quotes would be a fix?
# 2  
Old 04-26-2011
The following should work just fine, since pattern matching does not occur within double quotes:
Code:
re=your_regular_expression
find path -regex "$re"

If this is not what you're trying to do, please clarify with some example code.

Regards,
Alister
# 3  
Old 04-26-2011
I don't know how I can describe the problem without talking all day, especially since this code is proprietary.

Basically the entire ! -regex "regex" as well as possible other ! -regex and ! -name are contained within a single variable, I'm running this on a dir to find non-compliant files for an audit.

For example:
Code:
var="! -regex \"regex\" ! -name path"

Run as:
Code:
find start_path $var -mindepth 1 -maxdepth 1 -type 'd' | ...

Within this variable all the -regex arguments are surrounded in double quotes to prevent them from blowing up, otherwise everything else is left unquoted.

Both the ! and the -regex arguments are getting surrounded in single quotes. I cannot get rid of these single quotes no matter what it seems, Linux puts them there.
# 4  
Old 04-26-2011
Have you tried using eval? It's dangerous if the information is untrusted, but that may not be a concern in this case.

Code:
eval find "$var" -mindepth 1 -maxdepth 1 -type d | ...

It'll probably require backslash quoting exclamation points if the shell supports history expansion.

Regarding the single quotes you're seeing, it sounds like you enabled tracing with set -x.

This example works fine to find all files under the current directory that are not c source:
Code:
$ re_args='\! -regex '\''.*\.c$'\'
$ echo "$re_args"
\! -regex '.*\.c$'
$ eval find . "$re_args" -type f
... <my list of non-C source files here> ...

Regards,
Alister
# 5  
Old 04-26-2011
What if '.*\.c$' is in itself represented by a variable? Surrounding it with single quotes would prevent the variable from being substituted.
# 6  
Old 04-26-2011
Code:
$ re_pattern="'.*\.c\$'"
$ re_args='\! -regex '"$re_pattern"
$ eval find . "$re_args" -type f
... <my list of non-C source files here> ...

This User Gave Thanks to alister For This Post:
# 7  
Old 04-26-2011
Seem to finally be over that pain in the butt, thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find with mtime option

Hi, Please give me more details on the following examples, about "mtime" option. When I try this, I could not get the expected output, please help. find . -mtime -1 -print find . -mtime +1 -print find . -mtime 1 -print How do I get the files modified between two dates, say from... (4 Replies)
Discussion started by: Dev_Dev
4 Replies

2. UNIX for Dummies Questions & Answers

find with prune option help needed

Hello, I am using ksh93 (/usr/dt/bin/dtksh) on Solaris and am stuck when trying to use find with the -prune option. I need to search a directory (supplied in a variable) for files matching a certain pattern, but ignore any sub-directories. I have tried: find ${full_path_to_dir_to_search}... (9 Replies)
Discussion started by: gary_w
9 Replies

3. Shell Programming and Scripting

regex help with 'find'

How to do alternation using regular expressions in the 'find' command? Like say you want to find all files that do not match the names specifically "this" or "that" within a directory using regular expressions? (10 Replies)
Discussion started by: stevensw
10 Replies

4. Shell Programming and Scripting

Help with find -perm option

How to find all files for instance that match the permission rwxr*x--- where * is a wildcard which can be optionally asserted but all the others must match? Thanks in advance (7 Replies)
Discussion started by: stevensw
7 Replies

5. Shell Programming and Scripting

Find regex

Hi There, Can anybody help me out for searching this regular expression? xxxxx.yyy.zzzz.From-ABCD.To-XYZ.xxxxxx I would like the ID1 and ID2 (knowing which one is Id1 and id2) .From-<ID1>. and .To-<ID2>. Thanks in advance!! Regards, Bhaskar (4 Replies)
Discussion started by: bhaskar_m
4 Replies

6. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

7. Shell Programming and Scripting

Find in Bash with -a option

Hi, The proble is below: Assume i have files starting from "process" then date/time then ".log". ex . process.20100504092942.log process.20100503152213.log process.20100430144217.log process.20100429153644.log process.20100428121200.log process.20100427130746.log... (2 Replies)
Discussion started by: meetvipin
2 Replies

8. Shell Programming and Scripting

find with prune option

Hi, I want to list files only from the current dir and its child dir (not from child's child dir). i have the following files, ./ABC/1.log ./ABC/2.log ./ABC/ABC1/A.log ./ABC/ABC1/B.log ./ABC/ABC1/XYZ/A1.log ./ABC/ABC1/XYZ/A2.log Here i want to list only the log file from current... (1 Reply)
Discussion started by: apsprabhu
1 Replies

9. UNIX for Dummies Questions & Answers

Find and EXECDIR option

Hello, I was reading the man pages of find and it says that the -exec option should not be used. I read the following about the recommended option, -execdir: -execdir command {} + Like -exec, but the specified command is run from the subdirec‐ tory containing... (1 Reply)
Discussion started by: mojoman
1 Replies

10. Shell Programming and Scripting

Find with RegEx

I have some files in unix ls -1 TMH.backend.tar.421E-03.Z TMH.backend.tar.421E-04.Z TMH.backend.tar.421E-05.Z TMH.backend.tar.421E-06.Z TMH.backend.tar.421E-07.Z TMH.backend.tar.421E-08.Z TMH.backend.tar.421E-08.Z.bak20081223164844 TMH.backend.tar.421E-09.Z... (1 Reply)
Discussion started by: on9west
1 Replies
Login or Register to Ask a Question