Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
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
up vote 4 down vote accepted

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

share|improve this answer
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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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