views:

2126

answers:

14

Is there a Linux command that will list all available commands and aliases for this terminal session?

As if you typed 'a' and pressed tab, but for every letter of the alphabet. Or running 'alias' but also returning commands.

Why? I'd like to run the following and see if a command is available:

ListAllCommands | grep searchstr
A: 

Why don't you just type:

seachstr

In the terminal.

The shell will say somehing like

seacrhstr: command not found

EDIT:

Ok, I take the downvote, because the answer is stupid, I just want to know: What's wrong with this answer!!! The asker said:

and see if a command is available.

Typing the command will tell you if it is available!.

Probably he/she meant "with out executing the command" or "to include it in a script" but I cannot read his mind ( is not that I can't regularly it is just that he's wearing a mind reading deflector )

OscarRyz
I want to know whether `formathdd` command exists. Oh wait, I just to run it and see. gee. Thanks :)
jeffjose
Probably safer to use 'which' to do that.
danio
+3  A: 

There is the

type -a mycommand

command which lists all aliases and commands in $PATH where mycommand is used. Can be used to check if the command exists in several variants. Other than that... There's probably some script around that parses $PATH and all aliases, but don't know about any such script.

sunny256
Even if it is not an answer to the question I think it is a better solution to the problem then the call to grep. So you can do type -a foo and if foo isn't available it returns command not found or something like that. So you are able to check for a command without calling the command itself.
Janusz
Actually it is an answer to the question, as the OP asked "I'd like to run the following and see if a command is available", so the purpose is to see if a command is available and this answer clearly works.
lothar
@lothar, what if the command you're looking for is, uh, what was it, "startserver"?, "serverstart"?, "server-something-or-other"?. I know, I'll just "grep -i" for server and see if it's there. Oops. Bzzz, not with this solution. matey :-) I'm not going to vote this answer down (since it's useful even if in a limited way) but a full blown solution would take into account that grep is for *regular expressions*, not just fixed strings.
paxdiablo
A: 

in debian: ls /bin/ | grep "whatImSearchingFor"

Gabriel Sosa
executables exist in all the directories in $PATH, not just /bin.
PhrkOnLsh
+1  A: 

You can always to the following:

1. Hold the $PATH environment variable value.
2. Split by ":"
3. For earch entry: 
    ls * $entry 
4. grep your command in that output.

The shell will execute command only if they are listed in the path env var anyway.

OscarRyz
Nice pseudo-code hehehe
victor hugo
:P .
OscarRyz
+3  A: 

Use "which searchstr". Returns either the path of the binary or the alias setup if it's an alias

Edit: If you're looking for a list of aliases, you can use:

alias -p | cut -d= -f1 | cut -d' ' -f2

Add that in to whichever PATH searching answer you like. Assumes you're using bash..

Aaron
A: 

The problem is that the tab-completion is searching your path, but all commands are not in your path.

To find the commands in your path using bash you could do something like :

for x in echo $PATH | cut -d":" -f1; do ls $x; done

nikudesu
+1  A: 

Try to press ALT-? (alt and question mark at the same time). Give it a second or two to build the list. It should work in bash.

Igor Krivokon
Or try hitting Esc at the start of a blank line four times.
ephemient
That's amazingly useful, and I didn't already know it thanks :-)
Chris Huang-Leaver
Or Press Tab twice.
danio
+2  A: 

Try this script:

#!/bin/bash
echo $PATH  | tr : '\n' | 
while read e; do 
    for i in $e/*; do
        if [[ -x "$i" && -f "$i" ]]; then     
            echo $i
        fi
    done
done
victor hugo
This is the only code solution so far that does it for all commands, not just to see if a given known command exists. +1.
paxdiablo
A: 

Here's a function you can put in your bashrc file:

function command-search
{
   oldIFS=${IFS}
   IFS=":"

   for p in ${PATH}
   do
      ls $p | grep $1
   done

   export IFS=${oldIFS}
}

Example usage:

$ command-search gnome
gnome-audio-profiles-properties*
gnome-eject@
gnome-keyring*
gnome-keyring-daemon*
gnome-mount*
gnome-open*
gnome-sound-recorder*
gnome-text-editor@
gnome-umount@
gnome-volume-control*
polkit-gnome-authorization*
vim.gnome*
$

FYI: IFS is a variable that bash uses to split strings.

Certainly there could be some better ways to do this.

Craig W. Wright
A: 

maybe i'm misunderstanding but what if you press Escape until you got the Display All X possibilities ?

LB
+6  A: 

Add to .bashrc

function ListAllCommands
{
    echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n' | sort -u
}

If you also want aliases, then:

function ListAllCommands
{
    COMMANDS=`echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
        -executable -type f -printf '%P\n'`
    ALIASES=`alias | cut -d '=' -f 1`
    echo "$COMMANDS"$'\n'"$ALIASES" | sort -u
}
Ants Aasma
This is very close but it's not including aliases. How can I appendalias | cut -f1to the results but before the sort?
AK
Why bother sorting if the only purpose is to put the output through grep anyway? Unix philosophy is to make simple tools and then chain them together if required, so leave sort out of ListAllCommands and if the user wants the output sorted they can do that.
danio
The sort is to remove duplicates.
Ants Aasma
A: 

Here's a solution that gives you a list of all executables and aliases. It's also portable to systems without xargs -d (e.g. Mac OS X), and properly handles paths with spaces in them.

#!/bin/bash
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u | grep "$@"

Usage: myscript.sh [grep-options] pattern, e.g. to find all commands that begin with ls, case-insensitive, do:

myscript -i ^ls
Adam Rosenfield
A: 

it depends, by that I mean it depends on what shell you are using. here are the constraints I see:

  1. must run in the same process as your shell, to catch aliases and functions and variables that would effect the commands you can find, think PATH or EDITOR although EDITOR might be out of scope. You can have unexported variables that can effect things.
  2. it is shell specific or your going off into the kernel, /proc/pid/enviorn and friends do not have enough information

I use ZSH so here is a zsh answer, it does the following 3 things:

  1. dumps path
  2. dumps alias names
  3. dumps functions that are in the env
  4. sorts them

here it is: feed_me() { (alias |cut -f1 -d= ;hash -f; hash -v |cut -f 1 -d= ; typeset +f)|sort}

If you use zsh this should do it.

later

marc

ms4720
+8  A: 

You can use the bash(1) built-in "compgen"

  • "compgen -c" will list all the commands you could run.
  • "compgen -a" will list all the aliases you could run.
  • "compgen -b" will list all the built-ins you could run.
  • "compgen -k" will list all the keywords you could run.
  • "compgen -A function" will list all the functions you could run.
  • "compgen -A function -abck" will list all the above in one go.

Check the man page for other completions you can generate.

To directly answer your question:

compgen -ac | grep searchstr

should do what yout want.

camh
Very much awesomeness, I never knew about that tool. That's ridiculously useful, and I dare say the *best* solution here as it uses the same tool as the terminal does to do autocompletion. It's the only non-wheel-remaking solution in this list.
Groxx