Посмотреть все алиасы 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 <>

Читайте также:  Linux sudo one command

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 как посмотреть syslog

Источник

Linux alias Command: How to Use It With Examples

Depending on the type of work you do on your Linux system, you may need to enter the same long and complicated commands frequently. The alias command lets you create shortcuts for these commands, making them easier to remember and use.

In this tutorial, we will show you how you can create, review, and remove command aliases in Linux.

Linux alias command

  • A system running a Linux distribution
  • An account with sudo privileges
  • Access to the terminal window or command line
  • A text editor, such as Vim or nano

What Is an Alias in Linux?

In Linux, an alias is a shortcut that references a command. An alias replaces a string that invokes a command in the Linux shell with another user-defined string.

Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors. Aliases can also replace commands with additional options, making them easier to use.

Linux Alias Syntax

The alias command uses the following syntax:

The different elements of the alias command syntax are:

  • alias : Invokes the alias command.
  • [option] : Allows the command to list all current aliases.
  • [name] : Defines the new shortcut that references a command. A name is a user-defined string, excluding special characters and ‘alias’ and ‘unalias’, which cannot be used as names.
  • [value] : Specifies the command the alias references. Commands can also include options, arguments, and variables. A value can also be a path to a script you want to execute.

Note: Enclosing the value in single quotation marks () will not expand any variables used with the command. To expand the variables, use double quotation marks («).

Читайте также:  Hp scanjet g3010 драйвер linux

Create Aliases in Linux

There are two types of aliases to create in Linux:

  • Temporary. Add them using the alias command.
  • Permanent. These require to edit system files.

Create a Temporary Alias in Linux

Use the alias command to create a temporary alias that lasts until the end of the current terminal session. For instance, creating c as an alias for the clear command:

Note: The alias command allows you to include multiple commands as the value by dividing them with the pipe symbol (|).

If you want to reference any additional command options when creating an alias, include them as a part of the value. For example, adding move as an alias for the mv command with the option of asking for confirmation before overwriting:

Note: Learn more about the mv command in our guide to moving directories in Linux.

Another use for aliases is to create a shortcut for running scripts. To do this, provide the absolute path to the script as the value:

alias frename='Example/Test/file_rename.sh'

In this example, using frename as a command runs the file_rename.sh bash script.

Create a Permanent Alias in Linux

To make an alias permanent, you need to add it to your shell configuration file. Depending on the type of shell you are using, use:

Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and nano text editor:

Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the alias command syntax.

#Custom aliases alias c='clear' alias move='mv -i' alias frename='Example/Test/file_rename.sh' 

Adding custom aliases to the bash configuration file

Once you add all of the new alises, press Ctrl+X, type Y and press Enter to save the changes to the configuration file.

The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the source command:

List All Aliases in Linux

Using the alias command on its own displays a list of all currently set aliases:

Listing all current aliases in Linux

Another method is to add the -p flag. This option displays the list in a format suitable for input to the shell:

Remove Aliases in Linux

To remove an alias, use the unalias command with the following syntax:

For instance, to remove the frename alias:

Removing an alias using the unalias command

Adding the -a option allows you to remove all aliases:

Removing all aliases using the unalias command

The example above shows how alias does not return any results after the unalias -a command.

After reading this tutorial, you should be able to use the alias command to create and manage aliases on your Linux system. This will help streamline your work and make terminal commands easier to use.

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

Источник

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