Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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 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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.