3

I am trying to write a script that will attempt to find if a certain program is installed. Lets say that the program is called, myprog. The problem is that the program can be named in different formats such as, 'prefix-myprog', 'myprog', and 'prefix_myprog'. If I use:

which myprog

then the command line will return the correct location only if it is named EXACTLY, myprog.

Is there a way that I can locate all possible instances with a wildcard of sorts?

Thanks

jw013
  • 51,212
Justin
  • 33
  • Are you trying to write a script to do that (and if so, what do you have so far/where are you at), or are you looking for something that already does it? – Mat Apr 29 '12 at 19:03
  • A simple PHP installer. I am trying to locate if the program is already installed, and if so, where it is at. I can not seem to locate all possible instances by using which. I figure that I can always loop through all of the possible choices, but just wondering if there is a better way. – Justin Apr 29 '12 at 19:06

2 Answers2

4

find /bin /sbin /usr -type f | grep -i myprog

Find all files in directories /bin, /sbin and /usr, then filter on 'myprog'.

man find

man grep

apropos myprog can be useful too.

man apropos

or what about locate -r myprog?

man locate

jippie
  • 14,086
1

You can loop over the entries in $PATH and expand wildcards in each directory in turn.

set -f; IFS=:
for dir in $PATH; do
  ls $dir/*myprog* 2>/dev/null
done
set +f; unset IFS

In zsh, this is a lot simpler:

ls $path/*myprog*(N)

By the way, avoid which, use type or command -v instead.