Linux run commands list

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

Читайте также:  Универсальный драйвер принтера canon 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 base64 decode string

Источник

Linux List Processes – How to Check Running Processes

Bolaji Ayodeji

Bolaji Ayodeji

Linux List Processes – How to Check Running Processes

Every day, developers use various applications and run commands in the terminal. These applications can include a browser, code editor, terminal, video conferencing app, or music player.

For each of these software applications that you open or commands you run, it creates a process or task.

One beautiful feature of the Linux operating system and of modern computers in general is that they provide support for multitasking. So multiple programs can run at the same time.

Have you ever wondered how you can check all the programs running on your machine? Then this article is for you, as I’ll show you how to list, manage, and kill all the running processes on your Linux machine.

Prerequisites

  • A Linux distro installed.
  • Basic knowledge of navigating around the command-line.
  • A smile on your face 🙂

A Quick Introduction to Linux Processes

A process is an instance of a running computer program that you can find in a software application or command.

For example, if you open your Visual Studio Code editor, that creates a process which will only stop (or die) once you terminate or close the Visual Studio Code application.

Likewise, when you run a command in the terminal (like curl ifconfig.me ), it creates a process that will only stop when the command finishes executing or is terminated.

How to List Running Processes in Linux using the ps Command

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

To test this, just open your terminal and run the ps command like so:

Screenshot-2021-06-28-at-3.25.33-PM

This will display the process for the current shell with four columns:

  • PID returns the unique process ID
  • TTY returns the terminal type you’re logged into
  • TIME returns the total amount of CPU usage
  • CMD returns the name of the command that launched the process.

You can choose to display a certain set of processes by using any combination of options (like -A -a , -C , -c , -d , -E , -e , -u , -X , -x , and others).

If you specify more than one of these options, then all processes which are matched by at least one of the given options will be displayed.

Screenshot-2021-06-28-at-3.55.10-PM

Type man ps in your terminal to read the manual for the ps command, which has a complete reference for all options and their uses.

To display all running processes for all users on your machine, including their usernames, and to show processes not attached to your terminal, you can use the command below:

Читайте также:  Linux mint команда sudo

Here’s a breakdown of the command:

Screenshot-2021-06-28-at-4.39.05-PM

  • ps : is the process status command.
  • a : displays information about other users’ processes as well as your own.
  • u : displays the processes belonging to the specified usernames.
  • x : includes processes that do not have a controlling terminal.

This will display the process for the current shell with eleven columns:

  • USER returns the username of the user running the process
  • PID returns the unique process ID
  • %CPU returns the percentage of CPU usage
  • %MEM returns the percentage memory usage
  • VSV returns the virtual size in Kbytes
  • RSS returns the resident set size
  • TT returns the control terminal name
  • STAT returns the symbolic process state
  • STARTED returns the time started
  • CMD returns the command that launched the process.

How to List Running Processes in Linux using the top and htop Commands

You can also use the top task manager command in Linux to see a real-time sorted list of top processes that use the most memory or CPU.

Type top in your terminal and you’ll get a result like the one you see in the screenshot below:

Screenshot-2021-06-28-at-4.27.28-PM

An alternative to top is htop which provides an interactive system-monitor to view and manage processes. It also displays a real-time sorted list of processes based on their CPU usage, and you can easily search, filter, and kill running processes.

htop is not installed on Linux by default, so you need to install it using the command below or download the binaries for your preferred Linux distro.

sudo apt update && sudo apt install htop

Just type htop in your terminal and you’ll get a result like the one you see in the screenshot below:

Screenshot-2021-06-29-at-4.49.09-AM

How to Kill Running Processes in Linux

Killing a process means that you terminate a running application or command. You can kill a process by running the kill command with the process ID or the pkill command with the process name like so:

To find the process ID of a running process, you can use the pgrep command followed by the name of the process like so:

To kill the iTerm2 process in the screenshot above, we will use any of the commands below. This will automatically terminate and close the iTerm2 process (application).

Conclusion

When you list running processes, it is usually a long and clustered list. You can pipe it through less to display the command output one page at a time in your terminal like so:

or display only a specific process that matches a particular name like so:

I hope that you now understand what Linux processes are and how to manage them using the ps , top , and htop commands.

Make sure to check out the manual for each command by running man ps , man top , or man htop respectively. The manual includes a comprehensive reference you can check if you need any more help at any point.

Thanks for reading – cheers! 💙

Источник

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