LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-26-2014, 12:59 PM   #1
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Rep: Reputation: 5
Question Use a variable for find criteria


Can someone assist me with something?

I can't figure out the proper syntax for what I'm trying to do, or maybe a better way of doing it. I'll be using the find command several times but in slightly different ways, using using the same search criteria.

I want to save some search criteria as a variable (I think) and use the value in a find command, something like this:

Code:
Crit="-iname '*\.txt' -o -name '*\.log'"
Code:
echo $Crit
-iname '*\.txt' -o -name '*\.log'

Code:
find /directory -type f \( $Crit \) -ls
or essentially echo'ing out that $Crit into the find command.

I've tried several different methods and I'm just not getting it right.

Thanks,

Last edited by bits45; 03-27-2014 at 10:34 AM.
 
Old 03-26-2014, 01:10 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
What problem you're facing? Ain't you getting appropriate file as result? Or getting any error?
Note: Please post commands (that you used) with proper spaces and use [code][code] around them.
 
Old 03-26-2014, 01:19 PM   #3
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Original Poster
Rep: Reputation: 5
If I were to simply write out the find command as one would at the command line, it works fine. But getting that variable information for $Crit inserted within the 'find' command is kicking my butt.

If I run it as I posted it here on this thread, I get nothing back. Other attempts with different syntax will give me different errors.

I suppose the real question is how do I project the value of $Crit to have the find command to use it correctly?

Thanks,
 
Old 03-26-2014, 01:41 PM   #4
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 505

Rep: Reputation: 137Reputation: 137
How about this...

Code:
# Crit='-name '*.txt' -o -name '*.log''
# find / -type f \( $Crit \) -ls

Last edited by mddnix; 03-26-2014 at 01:46 PM.
 
Old 03-26-2014, 03:10 PM   #5
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Original Poster
Rep: Reputation: 5
mddesai, that didn't work. I didn't think it would with the single quotes like that are.

Did that work for you? If so what shell are you using? I'm using Bash on CentOS 6

Thanks,
 
Old 03-26-2014, 04:09 PM   #6
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 505

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by bits45 View Post
mddesai, that didn't work. I didn't think it would with the single quotes like that are.

Did that work for you? If so what shell are you using? I'm using Bash on CentOS 6

Thanks,
Sorry, forgot to mention i had disabled globbing.

Just use set -f before find
Code:
# set -f
# Crit='-name '*.txt' -o -name '*.log''
# find / -type f \( $Crit \) -ls
 
Old 03-26-2014, 04:52 PM   #7
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Original Poster
Rep: Reputation: 5
Thumbs up

thanks mddesai! That was the piece I was missing. It worked.

I've only used set -x before in scripts for troubleshooting.

I suppose I'll work into my 80s (forty years from now) and never know everything about Unix/Linux and all it's tools it comes with, even if you freeze the builds now.

Take care!

bits
 
Old 03-26-2014, 09:02 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Just as an update, shivaa forgot to place a slash in the closing code block, so it should be [code][/code].

I do have a query about the following:
Quote:
If I were to simply write out the find command as one would at the command line, it works fine.
So your saying that if you write the following it works fine:
Code:
find /directory -type f -iname '*\.txt' -o -name '*\.log'
So these escapes work fine for you?

If you took those out of the variable declaration could you not use:
Code:
Crit="-name '*.txt' -o -name '*.log'"
 
Old 03-27-2014, 10:30 AM   #9
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Original Poster
Rep: Reputation: 5
Wink

grail, thanks for the ideas and the code correction. I've not posted must here on this site even though I've been a member for years.

Concerning the find command, I always thought you should quote the patterns, but I tried it both ways and checked the man page. The find man page states that "Please note that you should quote patterns as a matter of course, otherwise the shell will expand any wildcard characters in them."

Now with that said, I did try without quotes, but had to escape out the wildcard symbols as well when NOT using the quotes. Otherwise find returned this message:

Code:
 find: paths must precede expression: some_file_name.txt
This is what appeared to work fine at the command line, not using my little $Crit junk idea:

Code:
 find . -type f -name \*\.txt
I purposely escaped out the period just to ensure I do find a file with a '.txt' extension and not something like '*btxt' or what not.

I've tried different variations but the previous solution with "set -f" appears to be best so far.

Thanks!

Last edited by bits45; 03-27-2014 at 10:33 AM.
 
Old 03-27-2014, 11:16 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
I think we have gotten off on the wrong direction here somehow

I was simply saying to remove the escapes ... not the quotes, as you are correct that expansion will be a killer.

I would also point out that the following is flawed information:
Quote:
I purposely escaped out the period just to ensure I do find a file with a '.txt' extension and not something like '*btxt' or what not.
-name uses globbing and not regular expressions, hence the dot (.) is always just a dot

Glad you found a solution

Here is another way, just for variety:
Code:
Crit=( -name "*.txt" -o
       -name "*.log" )

find / -type f \( "${Crit[@]}" \) -ls
I like this one as it is easy to add to the array and can look clean when trying to read the code (IMO)
 
Old 03-27-2014, 04:06 PM   #11
bits45
Member
 
Registered: Jul 2009
Posts: 44
Blog Entries: 2

Original Poster
Rep: Reputation: 5
grail,

That last idea with the array is awesome. I'll tried that too. I too like "clean" even at the expense of more syntax. I think anyone coming in behind you to read the script will be able to understand the script easier if it's clean and even using some "older" ways of doing things. Everyone in an IT shop is normally at different skill levels.

Great help, thanks again!

bits
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] find, print and count specific characters after string criteria Alkass Linux - Newbie 2 05-03-2011 10:36 AM
LXer: Time Criteria with the find Command LXer Syndicated Linux News 0 03-16-2011 03:20 PM
Find ommand multiple criteria wave05 Programming 1 05-16-2009 01:17 AM
Using a single "Find" Command to find files bases on multiple criteria roboxooo Linux - Newbie 6 01-15-2009 05:13 AM
Find variable name by variable content using a subshell UltramaticOrange Programming 3 11-17-2008 04:51 PM


All times are GMT -5. The time now is 08:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration