Linux commands

One of the most common problems encountered by newcomers is how to find the Linux command path that was just installed on Linux using a package manager (e.g. the apt command). There are many developers from the Windows world. Many of them are first time Linux users. Some use Linux with WSL and some deal with cloud servers directly via ssh. Let’s look at some common commands to list or find the path to Linux commands.

What are Linux commands?

On Windows, the default user interface is the GUI. however, Linux on the server side is set to a text interface by default. So terms like Bash, shell, console, command prompt, terminal, and many other names are often used. They all refer to the text interface.

Similarly, in a terminal, you type commands called Linux commands. For example, you can start a Linux terminal by pressing Ctrl- Alt-T. You can then type a simple command, such as the date command, to see the date and time of today. After pressing Enter to run it.

1
2
date
2021年 11月 05日 星期五 15:47:49 CST

date, pwd, hello and ping are all Linux commands.

The different types of Linux commands

There are different types of Linux commands that you type in the terminal.

  1. internal or built-in shell commands builtin
  2. an external command/executable file/file file
  3. a shell function function
  4. an alias alias
  5. a keyword keyword

How to find out the Linux command types

We need to use the type command to show the path of a Linux command. It will also determine whether the command is a built-in shell, alias, function or external command. The syntax is :

1
2
3
type command
type -t command
type -a command

For example, let’s find out the pwd command type.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
type pwd
type date
type hello
type ping
# Display a single word which is one of 'alias', 'keyword', 'function', 'builtin', 
# 'file or '' , if command is an alias, shell reserved word, shell function, shell builtin, 
# disk file, or not found, respectively
type -t ping
type -t if
type -t vi
type -t nano
 
# The '-a' option shows all locations containing an executable named ping
type -a ping

What is the variable $PATH?

The PATH variable contains a set of directories where executable programs (such as ping, date, vi, docker, etc.) are stored on Linux or Unix-like systems. To see the current PATH, use the echo command/printf command.

1
2
3
4
5
6
7
echo  " $PATH " 
 
# OR 
# 
# 更友好的阅读格式
# 
echo  " ${PATH//:/$'\n'} "

Here’s what I saw.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
 /media/common/linux/gradle-7.2/bin$'
'/home/myfreax/.pulumi/bin$'
'/media/common/linux/go/bin/bin$'
'/media/common/linux/go/bin$'
'/media/common/linux/Android/sdk/cmdline-tools/latest/bin$'
'/media/common/linux/node-v14.17.3-linux-x64/bin$'
'/media/common/yarn/bin$'
'/media/common/linux/flluter/flutter/bin$'
'/home/myfreax/.deno/bin$'
'/home/myfreax/.cargo/bin$'
'/usr/local/sbin$'
'/usr/local/bin$'
'/usr/sbin$'
'/usr/bin$'
'/sbin$'
'/bin$'
'/usr/games$'
'/usr/local/games$'
'/snap/bin$'
'/home/myfreax/.pub-cache/bin$'
'/home/myfreax/.pulumi/bin 

To view all executable files stored in the /bin/ directory, run the ls command, as follows.

1
2
ls  / bin / 
ls  -l  / bin /

How to display information about commands

Use the command command as follows to list the path to Linux commands.

1
2
3
4
command -v date
command -v pwd
command -v ping
command -v docker

If you want a shell script to check if a command exists, use command check.

How to locate Linux commands

We can also easily get the path of a Linux command using the which command. For example: To print all possible matching paths, pass them as follows.

1
2
3
4
which gcc
which nano

which -a ls

Get the path to a Linux command or manual page

Use the whereis command to find binaries, source code, and manual pages on disk for a given program or command. The syntax is:

1
2
3
4
5
whereis command
whereis gcc
whereis docker
whereis lxc
whereis vim

How do I search the manuals and information pages only?

1
2
whereis -m date
whereis -m gcc

Find and locate command

We can also find files by name. For example, to search for a file named ‘date’ type.

1
2
3
4
5
locate -b '\date'
## OR ##
find / -name "date" -ls
# sudo for all files 
sudo find / -name "date" -ls

Output of the positioning command.

1
2
3
4
5
6
7
8
/snap/core/11316/bin/date
/snap/core/11420/bin/date
/snap/core18/2066/bin/date
/snap/core18/2074/bin/date
/snap/core20/1026/usr/bin/date
/snap/core20/1081/usr/bin/date
/usr/bin/date
/usr/lib/byobu/date

Show help about Linux commands

We can use the whatis command, the help command and the man command or the info command.

Each Linux command comes with a manual page (a help page describing usage and syntax). In addition, it contains a short description. For example, the whatis command searches for a man page name. It displays the manual page description for any matching name in a short form.

1
2
3
4
whatis ls
whatis clear
whatis date
whatis gcc

For all external commands, we use the following man command or info command for detailed manuals.

1
2
3
4
5
6
7
man date
man ls
man gcc
man bash
info ls
info bash
man which

Example manual page on Linux

For all Bash keywords and built-in functions, we use the help command.

1
2
3
4
5
help if
help exit
help logout
help type
help command

A little exercise

The main obstacle for new Linux users is locating commands. But with the help of this simple page, you can now find command paths and even use the manual page to get help about them. Suppose you have Docker installed on your Ubuntu server. you can then use the following commands to find the path and get help.

1
2
3
4
5
6
7
whatis docker
whereis docker
type -a docker
which docker
find / -iname "docker"
locate -b '\docker'
man docker

I hope new Linux users and developers will find these commands useful. Happy coding.