Dave Halter
Dave Halter

Reputation: 16335

How do I get a list of all available shell commands

In a typical Linux shell (bash) it is possible to to hit tab twice, to get a list of all available shell commands.

Is there a command which has the same behaviour? I want to pipe it into grep and search it.

Upvotes: 8

Views: 13623

Answers (10)

Dave Halter
Dave Halter

Reputation: 16335

An answer got deleted, I liked it most, so I'm trying to repost it:

compgen is of course better

echo $PATH | tr ':' '\n' | xargs -n 1 ls -1

I found this to be the most typical shell thing, I think it works also with other shells (which I doubt with things like IFS=':' )

Clearly, there maybe problems, if the file is not an executable, but I think for my question, that is enough - I just want to grep my output - which means searching for some commands.

Upvotes: 0

msbrogli
msbrogli

Reputation: 493

You could use compgen. For example:

compgen -c

You also could grep it, like this:

compgen -c | grep top$

Source: http://www.cyberciti.biz/open-source/command-line-hacks/compgen-linux-command/

Upvotes: 12

ganesshkumar
ganesshkumar

Reputation: 1377

List all the files in your PATH variable (ls all the directories in the PATH). The default user and system commands will be in /bin and /sbin respectively but on installing some software we will add them to some directory and link it using PATH variable.

Upvotes: 1

Tomasz Elendt
Tomasz Elendt

Reputation: 1485

(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename {} \; | sort | uniq)

It doesn't include shell builtins though.

Upvotes: 0

ams
ams

Reputation: 25599

Bash uses a builtin command named 'complete' to implement the tab feature.

I don't have the details to hand, but the should tell you all you need to know:

help complete 

Upvotes: 0

Barton Chittenden
Barton Chittenden

Reputation: 4418

Similar to @ghoti, but using find:

#!/bin/sh
for d in ${PATH//:/ }; do
    find $d -maxdepth 1 -type f -executable
done

Upvotes: 0

Bohemian
Bohemian

Reputation: 425368

tabtaby

Upvotes: 0

DigitalRoss
DigitalRoss

Reputation: 146231

You can list the directories straight from $PATH if you tweak the field separator first. The parens limit the effect to the one command, so use: (...) | grep ...

(IFS=': '; ls -1 $PATH)

Upvotes: 3

P.P
P.P

Reputation: 121427

"tab" twice & "y" prints all files in the paths of $PATH. So just printing all files in PATH is sufficient.

Just type this in the shell:

 # printf "%s\n" ${PATH//:/\/* } > my_commands

This redirect all the commands to a file "my_commands".

Upvotes: 1

ghoti
ghoti

Reputation: 46886

There may be things on your path which aren't actually executable.

#!/bin/sh
for d in ${PATH//:/ }; do
  for f in "$d"/*; do
    test -x "$f" && echo -n "$f "
  done
done
echo ""

This will also print paths, of course. If you only want unqualified filenames, it should be easy to adapt this.

Funny, StackOverflow doesn't know how to handle syntax highlighting for this. :-)

Upvotes: 0

Related Questions