Which command could I use list all commands that start with the letter 'g'. Let's assume I can't use Bash completion and a script must be written.

  • 3
    A Question (as on this site) should normally have a question: i.e. a sentence ending in a question mark (as is custom in the English language that we use here). What is your question? WHat have you tried that is not working, what was the (incorrect) output of what you tried. What did you expect? – Anthon Sep 27 '14 at 11:29
  • Welcome new user, some question cause you get negative reputation, before any ask, read the following manual to avoid nagative reputation: unix.stackexchange.com/help/asking – PersianGulf Sep 27 '14 at 20:26
  • Also for avoiding any negative reputation for any answer, read the following link: unix.stackexchange.com/help/answering – PersianGulf Sep 27 '14 at 20:27
  • 6
    Give us your instructor's email address so that we can hand in your assignment more quickly. – Gilles Sep 27 '14 at 23:01

You basically need to go through all directories listed in PATH and run a find to search for the pattern, in this case, binaries starting with the letter 'g'. Just run:

for i in $(echo $PATH | tr ":" "\n")
  find ${i} -iname "g*" 2> /dev/null

It's important to mention that some directories might not have one single file matching the regex, so you must discard the error output with 2> /dev/null.

This approach can be useful for many other situations, like searching for a static or dynamic library for example.


This is it, the find command to search all files in your system

find / -iname "g*"
  • it's time consuming, you can limit for every bin and sbin dir. – PersianGulf Sep 27 '14 at 20:22
  • Even you can locate command or slocate blahblah |egrep bin – PersianGulf Sep 27 '14 at 20:33
  • 1
    slocate is not real time search, it queries a previously indexed database. So if there are new files created with letter g and the slocate db hasn't been updated, it won't show up in the result from slocate. Yes, find is slow but will give you more accurate results. – GMaster Sep 28 '14 at 2:53
  • When slocate is updated it work. – PersianGulf Sep 28 '14 at 2:54
  • 1
    Yes, that's what I said in my previous comment, you just reiterated my comment – GMaster Sep 28 '14 at 2:55

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.