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?


  • 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

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

  • 1
    don't need grep, find can do searching with wildcards: find /bin /sbin /usr -name '*myprog' – glenn jackman Apr 30 '12 at 2:18

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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.