List all aliases linux

Linux command to list all available commands and aliases

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 

20 Answers 20

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 

Is there an equivalent to this for csh/tcsh? Those terminals also have some sort of autocompleting function used on tab, so maybe something exists?

Instead of compgen | grep , it can be more efficient to pass the string as argument to compgen itself (if it’s known to be a prefix, as implied in the question). In this case, that would be compgen -ac searchstr .

@MarAvFe: That’s because it is a bash built-in, not a separate command with its own man page. You’ll need to read the bash(1) man page, or run help compgen at a bash command line.

By extension, doing compgen -c | sort | uniq | less will print all commands available without duplicated lines and sorted alphabetically.

@endolith It’s a bash built-in. sh won’t have it (assuming bourne — I have no idea what /system/bin/sh is — that’s a rather non-standard path)

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 > 

This is very close but it’s not including aliases. How can I append alias | cut -f1 to the results but before the sort?

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.

This does not find commands that are symlinks to executables. Use the option -L on to follow symlinks to their destination. Note: -L is an option and not part of the matching expression, as such it has to be placed before the path on the command line. In this case find -L <>

Читайте также:  Настройка парольной политики astra linux

Might want to redirect STDERR to /dev/null to suppress nonexistent directory warnings. echo -n $PATH | xargs -d : -I <> find <> -maxdepth 1 -executable -type f -printf ‘%P\n’ 2> /dev/null | sort -u (+1 for zsh compatibility)

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.

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.

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, 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.

The others command didn’t work for me on embedded systems, because they require bash or a more complete version of xargs (busybox was limited).

The following commands should work on any Unix-like system.

List all commands by name

ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort 

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..

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

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.

Alternatively, you can get a convenient list of commands coupled with quick descriptions (as long as the command has a man page, which most do):

apropos -s 1 '' -s 1 returns only "section 1" manpages which are entries for executable programs. '' is a search for anything. (If you use an asterisk, on my system, bash throws in a search for all the files and folders in your current working directory.) 

Then you just grep it like you want.

xdg-desktop-icon (1) - command line tool for (un)installing icons to the desktop xdg-desktop-menu (1) - command line tool for (un)installing desktop menu items xdg-email (1) - command line tool for sending mail using the user's preferred e-mail composer xdg-icon-resource (1) - command line tool for (un)installing icon resources xdg-mime (1) - command line tool for querying information about file type handling and adding descriptions for new file types xdg-open (1) - opens a file or URL in the user's preferred application xdg-screensaver (1) - command line tool for controlling the screensaver xdg-settings (1) - get various settings from the desktop environment xdg-user-dir (1) - Find an XDG user dir xdg-user-dirs-update (1) - Update XDG user dir configuration 

The results don’t appear to be sorted, so if you’re looking for a long list, you can throw a | sort | into the middle, and then pipe that to a pager like less/more/most. ala:

apropos -s 1 '' | sort | grep zip | less 

Which returns a sorted list of all commands that have «zip» in their name or their short description, and pumps that the «less» pager. (You could also replace «less» with $PAGER and use the default pager.)

Читайте также:  Сборка linux mint xfce

Источник

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.

Читайте также:  Установка линукс минт grub

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.

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 all the linux aliases

I am aware that in Linux I can use the alias command to get a list of defined aliases. I am now trying to do the same through Go code with:

func ListAlias() error < out, err := exec.Command("alias").Output() if err != nil < fmt.Println(err) return err >fmt.Println(out) return nil > 
exec: "alias": executable file not found in $PATH 

The alternative I’ve considered is to parse the ~/.bashrc file for the list of aliases defined but I have encountered this scenario where the bashrc lists another custom_aliases.sh file and all the aliases are listed there. That’s why I am trying to use the alias command to list all the aliases.

1 Answer 1

alias isn’t an executable but a shell builtin. You can easily see that by running

$ type alias alias is a shell builtin 

Therefore you need to call the shell’s alias command depending on which shell you’re using. For example with bash you’ll need to use

out, err := exec.Command("/bin/bash", "-c", "alias").Output() 

But that still won’t give you the answer because bash doesn’t source the .bashrc file in that case so aliases won’t be available in the subshell. You’ll need the —rcfile or —login / -l option and also need to specify the shell as interactive with -i

out, err := exec.Command("/bin/bash", "-lic", "alias").Output() // or out, err := exec.Command("/bin/bash", "--rcfile", "~/.bashrc", "-ic", "alias").Output() 

exec.Command(«/bin/bash», «-ic», «alias») would also possibly work depending on where your aliases are sourced. Other shells like zsh, sh, dash. may source different files with different options, so check your shell’s documentation if -ic or -lic doesn’t work

Источник

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