How to pass multiple file types search pattern as argument to find?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass multiple file types search pattern as argument to find?
# 1  
Old 10-12-2010
How to pass multiple file types search pattern as argument to find?

How can I pass $var_find variable as argment to find command?

test.sh
Code:
 
var_find=' \( -name "*.xml" -o -name "*.jsp" \) '
 
echo "${var_find}"
 
find . -type f ${var_find} -print
 
# Below statement works fine.. I want to replace this with the above..
#find . \( -name "*.xml" -o -name "*.jsp" \)  -print

output -->
Quote:

\( -name "*.xml" -o -name "*.jsp" \)
find: bad option \(
find: path-list predicate-list
I tried escape character to escape " quotes,, and different types of braces for calling var_find,, none seem to be working..

Could someone help with this ?
# 2  
Old 10-12-2010
Try:
Code:
eval find . -type f "${var_find}" -print

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-12-2010
Thanks Mr. Scrutinizer,, that makes it work..
# 4  
Old 10-12-2010
Quote:
Originally Posted by kchinnam
Code:
var_find[/SIZE]=' \( -name "*.xml" -o -name "*.jsp" \) '
 
# Below statement works fine.. I want to replace this with the above..
#find . \( -name "*.xml" -o -name "*.jsp" \)  -print

Could someone help with this ?
The reason, why this fails (and why Scrutinizers solution works) is subtle: by declaring the variable "var_find" with some content you make this content one string, even if this string contains several blanks, which usually separate strings in the shell.

Lines are read in fields separated by so-called "field separators" (white space, semicolon, etc.) by the shell. But in quoted strings these field separators are ignored. The following script will show that:

Code:
#!/bin/ksh

print - "Number of arguments: $#"

exit 0

call the script with ./script a b c and it will print the expected value 3. Issue ./script a "b c" and it will print only 2, because "b c" is considered to be one string.

To come back to your find-command: find knows how to handle the "-name"-option and the "-name"-option knows that it expects exactly one argument: a filename mask. But find doesn't know about a "\( -name "*.xml" -o -name "*.jsp" \)"-option and this is what it complains about.

I hope this helps.

bakunin
# 5  
Old 10-12-2010
It still feels like I do not fully understand what really goes behind the scenes in shell..
But I will try to remember this,, for now..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies

2. Shell Programming and Scripting

Search multiple pattern in a file

I have a sample file with following output: HTTP/1.1 200 OK User: admin Set-Cookie: AMBARISESSIONID=y3v3648yqcno32nq478kw7ar;Path=/;HttpOnly Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Type: text/plain Vary: Accept-Encoding, User-Agent Content-Length: 6057 Server:... (4 Replies)
Discussion started by: saurau
4 Replies

3. Shell Programming and Scripting

Pass file as one of argument in shell

Hi, Is there any way that we can pass one file as one of the argument in shell script ? (1 Reply)
Discussion started by: Selva_2507
1 Replies

4. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

5. Shell Programming and Scripting

Accepting search pattern to script argument

I have a script in tcsh and I want to have a find option to which I can pass a file search pattern I want using the command: /home/chrisd/tatsh/trunk/hstmy/bin/tcsh/raytrac.tcsh -f=*rc* The set command seems to fail when user does not supply a search pattern (if user just supplies -f, a... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

bash: find multiple types[solved]

Hi, I would like to use find to search for multiple types. For example search for symlink and regular file but not directories, sockets etc.... Something like: find . -type l,f -name "stuff" But of course it does not work. Is there any way to avoid an if statement and to do it faster? ... (0 Replies)
Discussion started by: Dedalus
0 Replies

7. Shell Programming and Scripting

Help to search multiple pattern in file with grep/sed/awk

Hello All, I have a file which is having below type of data, Jul 19 2011 | 123456 Jul 19 2011 | 123456 Jul 20 2011 | 123456 Jul 20 2011 | 123456 Here I wanted to grep for date pattern as below, so that it should only grep "Jul 20" OR "Jul ... (9 Replies)
Discussion started by: gr8_usk
9 Replies

8. Linux

Simplified find command to find multiple file types

Hi, I'm using the following command to find the multiple requierd file types and its working fine find . -name "*.pl" -o -name "*.pm" -o -name "*.sql" -o -name "*.so" -o -name "*.sh" -o -name "*.java" -o -name "*.class" -o -name "*.jar" -o -name "*.gz" -o -name "*.Z" -type f Though... (2 Replies)
Discussion started by: vickramshetty
2 Replies

9. Shell Programming and Scripting

find multiple file types and tar

There are these ksh files and config files that are written and updated on a daily basis. All I want to do is write a script that finds both these types of files and archive them on a daily basis, to help in restoring in times of system outages and so on. Particulary I'm interested in .ksh ,... (9 Replies)
Discussion started by: manthasirisha
9 Replies
Login or Register to Ask a Question