- Ways to List All Available Commands and Aliases in Linux
- Using the .bashrc
- To list all the command
- To list all the aliases
- Using built in shell library function
- Conclusion
- Shell command to list all commands in linux
- Linux command to list all available commands and aliases
- How to view list of all commands in Linux Shell terminal
- The 50 Most Popular Linux & Terminal Commands
- 30 BASIC COMMANDS IN LINUX / UNIX
- How to list down all the available commands and aliases on Linux?
- Syntax
- Output
- Output
- How to generate list of *all* available commands and functions?
- How to get a list of all the commands available for Ubuntu?
- First Method
- Third Method
- Fourth Method
- How do I get a list of all available shell commands
- 10 Answers 10
Ways to List All Available Commands and Aliases in Linux
In Linux, commands are the key things that are very interesting to run and execute the respective programs. Executing such commands and their aliases lets the user run many important tasks.
If you are curious about how to check all the commands and their aliases in Linux then let us explain the ways to list them. There are many ways to list them out of which one is to write the shell script. But Linux makes it easy with the keyword of the shell library which is compgen.
In this article, we are going to explain the ways to list all the available commands and aliases in Linux.
Using the .bashrc
One way is to write the shell script by adding it on .bashrc. Let us add a few lines of shell scripts so it can list the command and aliases.
To list all the command
Add these lines of shell script to list all the available commands.
function ListAllCommands < echo -n $PATH | xargs -d : -I <>find <> -maxdepth 1 \ -executable -type f -printf '%P\n' | sort -u >
To list all the aliases
Add these lines of shell script to list all the available aliases.
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 >
To list all the available commands and aliases in $PATH that is using mycommand, run the command as shown below.
Using built in shell library function
There is a built-in keyword of the shell library which is compgen that is very easy to use and execute to list all the commands and the aliases in Linux. Let us show you how to do it.
You are allowed to use any of the listed flags as per your requirement.
compgen -c → list all the commands that we can run.
compgen -a → list all the aliases that we can run.
compgen -b → list all the built-ins that we can run.
compgen -k → list all the keywords that we can run.
compgen -A function → list all the functions that we can run.
compgen -A function -abck → list all the above flags can do at once.
Here, we are going to list all the commands that we can run and aliases. So the -c and -a flag is used in such cases.
To list all the commands that we can run, let’s run the command as shown below.
To list the files on commands.txt, let’s print the contents with the cat command as shown below.
Here, we have successfully printed the available commands that we can run and there are many more commands which we could not capture on the screenshot.
To list all the aliases that we can run, let’s run the command as shown below.
To list the files on aliases.txt, let’s print the contents with the cat command as shown below.
Here, we have successfully printed the available aliases that we can run.
Creating a script with the use of compgen command is also one way to list the commands and aliases that we could run. Check the example below for further details regarding the script.
$ echo "compgen -c" > commands.sh
Here, we have created a list.sh script file with content “compgen -c” in it.
Let’s give the execute permission to the script with the command as shown below.
Now, run the script with the command as shown below.
Here, all the available commands are listed with the above script by using the compgen command.
Conclusion
In this article, you have learnt how to list all the available commands and aliases that we can run on Linux. Now, you are able to take the reference of the commands as per your requirements to complete the certain tasks. Thank you!
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
Shell command to list all commands in linux
Solution 3: Open terminal Ctrl + Alt + t and run this command: This will list all commands and a simple description of each command. In order to use that just create a shell file with the command shown below − Now insert the code shown below − And then give permission to the script before running it − Output In order to print the aliases, we just need to replace the code inside the sample.sh file to the code shown below − Output Solution 1: The solution I chose was to run the command: which appends all runnable commands, functions and aliases to a text file Taken from: https://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases
Linux command to list all available commands and aliases
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
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 >
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.
Unix / Linux — Useful Commands, Misc Commands ; 35. umask. Show the permissions that are given to view files by default ; 36. uname. Displays name of the current system ; 37. uptime. Gets the
How to view list of all commands in Linux Shell terminal
Force an External Mac Drive to Mount via Command Line Terminal Commands in Mac OS X
Duration: 1:24
The 50 Most Popular Linux & Terminal Commands
Learn the 50 most popular Linux commands from Colt Steele. All these commands work on Duration: 5:00:17
30 BASIC COMMANDS IN LINUX / UNIX
LINUX TERMINAL (UBUNTU) IN WINDOWS 10 || HOW TO USE LINUX TERMINAL IN WINDOWS
Duration: 40:39
How to list down all the available commands and aliases on Linux?
Linux provides us with a huge amount of commands along with their aliases that we can make use of. Although these commands serve different purposes we can still make use of all these commands anywhere we like in the terminal.
There are different ways with which we can interact with Linux commands. When it comes to listing all the available commands to the terminal we also have different approaches, either we can write a shell script by ourselves or we can use a shell library function that does that for us.
Let’s consider the first approaches where I’ll make use of a shell library keyword named compgen , which is a bash builtin command that can be used to list all the available commands.
Syntax
In the above syntax, the flag is a placeholder that can be replaced according to our needs.
The flag can have all these different values −
- -c − used to list all the commands you could run.
- -a − used to list all the aliases you could run.
- -k − used to list all the keywords you could run.
- -b − used to list all the built-ins you could run.
- -A function − used to list all the functions you could run
- -A function -abck − used to list everything shown above in one go.
Since, we only need to list the commands and their aliases we will make use of the -c and -a flags only.
In order to use that just create a shell file with the command shown below −
Now insert the code shown below −
And then give permission to the script before running it −
chmod 777 sample.sh ./sample.sh
Output
immukul@192 Downloads % ./sample.sh if then else elif fi case esac for select while until do done in . . .
In order to print the aliases, we just need to replace the code inside the sample.sh file to the code shown below −
Output
101 Bash Commands and Tips for Beginners to Experts, [ andrew@pc01 ~ ]$ myvar=»hello, world!» && echo $myvar hello, world! Env vars can also be defined using the export command. When
How to generate list of *all* available commands and functions?
The solution I chose was to run the command:
$ compgen -A function -abck | sort -u >> cmds.txt
which appends all runnable commands, functions and aliases to a text file cmds.txt
Taken from: https://stackoverflow.com/questions/948008/linux-command-to-list-all-available-commands-and-aliases
Edit: added sort -u to command to remove duplicates as suggested by glenn jackman
It seems compgen outputs duplicates: perhaps programs that appear in multiple locations in your PATH:
$ [tab][tab] Display all 2328 possibilities? (y or n)
$ compgen -A function -abck | wc -l 2647 $ compgen -A function -abck | sort -u | wc -l 2328
I don’t know if this is important for you.
Read the Source Code of Shell Commands, The which command prints the full path of the shell commands, helping us to find out where it is on our file system. It searches through the
How to get a list of all the commands available for Ubuntu?
First Method
NB : Thanks to @Rmano. This method doesn’t work with zsh shell.
This will list all commands in your $PATH environment variable.
To store the result in a file you can redirect the output to a file.
Note that this will return an error if any directory names in your $PATH contain spaces. In that case, use this instead:
while read -d ':' dir; do echo "$dir"; done
Second Method
compgen -c | sort -u > commands && less commands
Third Method
Another method is a double Tab click.
Fourth Method
Another method using find command:
If you are using bash, which is the default shell in all official Ubuntu flavors, run compgen -c to see the available commands including aliases.
Open terminal Ctrl + Alt + t and run this command:
This will list all commands and a simple description of each command.
If you want to save the list you can redirect the result into an output file
whatis `compgen -c` > listOfCommands.txt
So why I used whatis command. The command man whatis gives:
Each manual page has a short description available within it.
whatis searches the manual page names and displays the manual page descrip‐ tions of any name matched.
so in easy words whatis give a general. description of each command
Getting Started with Linux Shell Commands, As part of this video, you will get started with Linux Shell Commands by running the first Duration: 29:53
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.
10 Answers 10
You could use compgen. For example:
You also could grep it, like this:
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 .
"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:
This redirect all the commands to a file "my_commands".
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.
There may be things on your path which aren't actually executable.
#!/bin/sh for d in $; 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. 🙂
Similar to @ghoti, but using find:
#!/bin/sh for d in $; do find $d -maxdepth 1 -type f -executable done
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:
(IFS=':'; find $PATH -maxdepth 1 -type f -executable -exec basename <> \; | sort | uniq)
It doesn't include shell builtins though.
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.