Get all commands linux

List all commands that a shell knows

What can I type at my shell (which happens to be bash ) that will list all the commands that are recognized? Also, does this differ by shell? Or do all shells just have a «directory» of commands they recognize? Secondly, different question, but how can I override any of those? In other words how can I write my own view command to replace the one existing on my Ubuntu system, which appears to just load vim .

7 Answers 7

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. 

A shell knows four kinds of commands.

  • Aliases: these are nicknames for a command with some options. They are defined in the shell’s initialization file ( ~/.bashrc for bash).
  • Functions: they are snippets of shell code given a name. Like aliases, they are defined in the shell’s initialization file.
  • Builtins: the shell comes with a small number of built-in commands. Most builtins manipulate the shell state ( cd changes the current directory, set changes options and positional parameters, export changes the environment, …). Most shell offer largely the same builtins but each shell has a few extensions to the basic set.
  • External commands: they are independent of the shell. Like other programs, the shell executes external programs by looking them up in the executable search path. The PATH environment variable contains a colon-separated list of directories to search for programs.

In case there are commands of several types by the same name, the first match in the order above is executed¹.

You can see what type of command a name corresponds to by running type some_name .

You can list aliases by running the alias built-in with no argument. There is no way to list functions or builtins that works in all shells. You can find a list of builtins in the shell’s documentation.

In bash, the set builtin lists functions with their definitions as well as variables. In bash, ksh or zsh, typeset -f lists functions with their definitions. In bash, you can list all command names of any type with compgen -c . You can use compgen -A alias , compgen -A builtin compgen -A function to list commands of a specific type. You can pass an additional string to compgen to list only commands that start with that prefix.

In zsh, you can list the currently available commands of a given type with echo $ , echo $ , echo $ and echo $ (that last one lists external commands only).

The following shell-agnostic snippet lists all available external programs:

case "$PATH" in (*[!:]:) PATH="$PATH:" ;; esac set -f; IFS=: for dir in $PATH; do set +f [ -z "$dir" ] && dir="." for file in "$dir"/*; do if [ -x "$file" ] && ! [ -d "$file" ]; then printf '%s = %s\n' "$" "$file" fi done done 

There is an edge case in Bash: hashed commands.

A full search of the directories in $PATH is performed only if the command is not found in the hash table

set -h mkdir ~/dir-for-wat-command echo 'echo WAT!' >~/dir-for-wat-command/wat chmod +x ~/dir-for-wat-command/wat hash -p ~/dir-for-wat-command/wat wat wat 

The PATH environment variable doesn’t contain ~/dir-for-wat-command , compgen -c doesn’t show wat , but you can run wat .

Читайте также:  Gparted linux red hat

If you want to shadow an existing command, define an alias or a function.

¹ Exception: a few builtins (called special builtins) cannot be shadowed by a function — bash and zsh don’t comply with POSIX on that point in their default mode though.

Источник

List All Available Commands and Aliases in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

Sometimes when working in Linux, we’d like to know all the commands and aliases supported by the system.

An alias, as we know, is a custom shortcut used to refer to the actual command. It can help us save time by customizing long commands into short strings.

In this tutorial, we’ll see three approaches for listing all the available commands and aliases in Linux – using the compgen command, using the alias command, and by writing a Bash script.

2. Using the compgen Command

Using the compgen command, we can list commands, aliases, built-ins, keywords, and functions using different options.

2.1. List Commands

We use the compgen -c command to list all available commands:

$ compgen -c alert egrep fgrep grep l la ll ls . 

Here, the -c option tells compgen to list all the commands that we can execute on our system.

2.2. List Aliases

We use the compgen -a command to list all the available aliases:

$ compgen -a alert egrep fgrep grep l la ll ls . 

Here, the -a option tells compgen to list all the aliases.

3. Using the alias Command

Using the alias command, we can list the defined aliases:

$ alias -p | cut -d= -f1 | cut -d' ' -f2 alert egrep fgrep grep l la ll ls . 

Here, the -p option tells the alias command to print all the defined aliases. Then, we pipe the output to the first cut command. The cut command uses the = sign as the delimiter to divide a line into fields and the -f1 means we take the first field.

We pipe the output of the first cut command to the second cut command. It uses space as a delimiter and we select the second field and display it on the terminal.

4. Using Bash Script

We can write a Bash script to list all the available commands on our system:

#!/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

Let’s break down the above script. First, we get all the directory paths that contain executables using the $PATH environment variable. Then, we pipe the output to the tr command. The tr command translates the : from the input to a newline and pipes the output to the while loop.

Читайте также:  Linux config without comments

The while loop uses the read command to read each line and stores the content of each step in $e. Using the for loop, we iterate over each directory and check if each file is an executable using the -x option. The -f option checks if a file exists and if it’s a regular file.

Once the filename passes both tests, its path is displayed on the terminal using the echo command.

Now, let’s run the script and check its output:

$ bash commands.sh /usr/sbin/aa-remove-unknown /usr/sbin/aa-status /usr/sbin/aa-teardown /usr/sbin/accessdb /usr/sbin/add-shell /usr/sbin/addgnupghome /usr/sbin/addgroup /usr/sbin/adduser /usr/sbin/agetty . 

Here, we can see the absolute paths of all the commands.

5. Conclusion

In this article, we saw how to use the compgen command to list all the available commands and aliases in Linux. We also learned how to list all the available aliases using the alias command and the available commands using a Bash script.

Источник

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 −

Источник

How to get list of commands used in a shell script?

I have a shell script of more than 1000 lines, i would like to check if all the commands used in the script are installed in my Linux operating system. Is there any tool to get the list of Linux commands used in the shell script? Or how can i write a small script which can do this for me? The script runs successfully on the Ubuntu machine, it is invoked as a part of C++ application. we need to run the same on a device where a Linux with limited capability runs. I have identified manually, few commands which the script runs and not present on Device OS. before we try installing these commands i would like to check all other commands and install all at once. Thanks in advance

Читайте также:  Get permissions on file linux

Arek Thanks, it helps little bit but not exactly what i was looking for. I need to know what all commands the script uses. i can not run the entire script at one shot as it runs part by part based on the arguments. as of now we do not have all the information about arguments.

1000 lines isn’t that long, and most of it is probably either shell-specific (like if , while , etc) or repetitive (the same command being called repeatedly with different arguments.) You’ll probably spend less time scanning it manually than you will looking for a way to do it automatically.

Your best bet: update your script to wrap command execution with your own function, then in this function log the command and just pass through to the command itself. run() < echo "$1" >/tmp/commands; $@; > or such.

4 Answers 4

I already tried this in the past and got to the conclusion that is very difficult to provide a solution which would work for all scripts. The reason is that each script with complex commands has a different approach in using the shells features. In case of a simple linear script, it might be as easy as using debug mode. For example: bash -x script.sh 2>&1 | grep ^+ | awk » | sort -u In case the script has some decisions, then you might use the same approach an consider that for the «else» cases the commands would still be the same just with different arguments or would be something trivial (echo + exit).

In case of a complex script, I attempted to write a script that would just look for commands in the same place I would do it myself. The challenge is to create expressions that would help identify all used possibilities, I would say this is doable for about 80-90% of the script and the output should only be used as reference since it will contain invalid data (~20%).

Here is an example script that would parse itself using a very simple approach (separate commands on different lines, 1st word will be the command):

# 1. Eliminate all quoted text # 2. Eliminate all comments # 3. Replace all delimiters between commands with new lines ( ; | && || ) # 4. extract the command from 1st column and print it once cat $0 \ | sed -e 's/\"/./g' -e "s/'[^']*'//g" -e 's/"[^"]*"//g' \ | sed -e "s/^[[:space:]]*#.*$//" -e "s/\([^\\]\)#[^\"']*$/\1/" \ | sed -e "s/&&/;/g" -e "s/||/;/g" | tr ";|" "\n\n" \ | awk '' | sort -u 

There are many more cases to consider (command substitutions, aliases etc.), 1, 2 and 3 are just beginning, but they would still cover 80% of most complex scripts. The regular expressions used would need to be adjusted or extended to increase precision and special cases.

In conclusion if you really need something like this, then you can write a script as above, but don’t trust the output until you verify it yourself.

Источник

Оцените статью
Adblock
detector