LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   make bash search a list of dirs (https://www.linuxquestions.org/questions/linux-newbie-8/make-bash-search-a-list-of-dirs-700198/)

agrestic 01-27-2009 08:19 AM

make bash search a list of dirs
 
Hi.
I would like a bash script to quickly search certain directories for executable foo. Can the directories be in a list, like {$HOME/bin,/bin,/usr/bin}? If so, what's the correct syntax?
This is what I have so far.
Code:

if [ ! -f /usr/bin/foo ]; then
...yadda, ya...

But I want it to search directories I specify. IDK if it could use /usr/bin/foo || /bin/foo (in double brackets?), or what.
Googling this i learned echo Total dirs: $(ls -F | grep /$ | wc -l) and a few other things, but no cigar. I'm not sure what a relevant search phrase would be.

TIA.

colucix 01-27-2009 08:56 AM

What about the following?
Code:

for dir in $HOME/bin /bin /usr/bin
do
  find $dir -maxdepth 1 -name foo
done

The -maxdepth 1 avoids duplicate entries in case one directory is a subdirectory of another one already specified (take in mind that find is recursive).

agrestic 01-27-2009 09:05 AM

Quote:

Originally Posted by colucix (Post 3422777)
The -maxdepth 1 avoids duplicate entries in case one directory is a subdirectory of another one already specified (take in mind that find is recursive).

Excellent! Many many thanks. Especially for the explanation of the maxdepth parameter; man find didn't yield much. :)


All times are GMT -5. The time now is 12:38 AM.