Linux command line 3 - Working with commands

LHT

This article mainly references the book The Linux Command Line (2nd Edition). The shell tool used is Xshell, and the operating system is CentOS 7.6. This means that some shortcuts may not work in other shell tools, and there may be slight differences in files across different Linux operating systems. Please analyze based on your actual situation.

Since CentOS 7.6 is EOL, consider using Rocky Linux or Alma Linux instead.

Using Commands

What is a Command?

A command can take one of the following four forms:

  1. An Executable Program: Programs like those found in the /usr/bin directory. These programs can be binary files compiled from languages like C or C++, or scripts written in languages such as shell, perl, python, or ruby.
  2. A Built-in Shell Command: Commands integrated into the shell itself. Bash includes several of these, called shell builtins. For example, the cd command is a shell builtin.
  3. A Shell Function: These are small shell scripts integrated into the environment. We will discuss configuring environment variables and writing shell functions in later sections, but for now, simply be aware of their existence.
  4. An Alias: You can define your own commands as aliases for existing commands.

type - Display Command Type

The type command is a shell builtin that displays the type of a given command. It works as follows:

1
type command

Where “command” is the name of the command you want to inspect. Some examples:

1
2
3
4
5
6
7
8
9
10
[root@lht bin]# type type
type is a shell builtin
[root@lht bin]# type ls
ls is aliased to `ls --color=auto'
[root@lht bin]# type date
date is /usr/bin/date
[root@lht bin]# type cp
cp is aliased to `cp -i'
[root@lht bin]# type ll
ll is aliased to `ls -l --color=auto'

As you can see, these commands yield different results.

Note that the ls command (in CentOS) is actually an alias for ls --color=auto, which explains why the output of ls is colored.

which - Show the Location of an Executable Program

Sometimes multiple versions of an executable program are installed on the same system. While uncommon on desktop systems, it is common on larger servers. To find the exact location of a given executable, use the which command:

1
2
3
4
5
[root@lht ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@lht ~]# which cd
/usr/bin/cd

Viewing Command Documentation

help - Get Help for Shell Built-in Commands

To get help on a shell built-in command, type help followed by the command name:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@lht ~]# help cd
cd: cd [-L|[-P [-e]]] [dir]
Change the shell working directory.

Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.

The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.

Options:
-L force symbolic links to be followed
-P use the physical directory structure without following symbolic
links
-e if the -P option is supplied, and the current working directory
cannot be determined successfully, exit with a non-zero status

The default is to follow symbolic links, as if `-L` were specified.

Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.

In the line:

1
cd [-L|[-P [-e]]] [dir]

Square brackets represent optional elements, and the vertical bar (|) indicates mutually exclusive options.

--help - Displays usage information

Shows the syntax and options supported by the command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@lht ~]# cd --help
-bash: cd: --: invalid option
cd: usage: cd [-L|[-P [-e]]] [dir]
[root@lht ~]# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
-m, --mode=MODE set file mode (as in chmod), not a=rwx - umask
-p, --parents no error if existing, make parent directories as needed
-v, --verbose print a message for each created directory
-Z set SELinux security context of each created directory
to the default type
--context[=CTX] like -Z, or if CTX is specified then set the SELinux
or SMACK security context to CTX
--help display this help and exit
--version output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'mkdir invocation'

Some programs do not support the --help option, which often leads to error messages being output, but can still display command usage information, just like the cd command above.

man - Displays program manual pages

To browse the manual for the ls command:

1
[root@lht ~]# man ls

For more related parameters and content, refer to:

man command, Linux man command explanation: view command help in Linux - Linux Command Search Engine

apropos - Displays appropriate commands

apropos: Searches for keywords in a specific database file that contains brief descriptions of system commands.

If you don’t know the name of the command needed to complete a specific task, you can use a keyword to search for it using the Linux apropos utility. This utility searches for the keyword and displays all the short descriptions of man pages that contain matches.

Additionally, by using the man utility with the -k (keyword) option, you can get the same result as using the apropos utility (it’s actually the same command).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@lht ~]# man -k who
at.allow (5) - determine who can submit jobs via at or batch
at.deny (5) - determine who can submit jobs via at or batch
btrfs-filesystem (8) - command group of btrfs that usually work on the whole filesystem
docker-trust-signer (1) - Manage entities who can sign Docker images
w (1) - Show who is logged on and what they are doing.
who (1) - show who is logged on
whoami (1) - print effective userid
[root@lht ~]# apropos who
at.allow (5) - determine who can submit jobs via at or batch
at.deny (5) - determine who can submit jobs via at or batch
btrfs-filesystem (8) - command group of btrfs that usually work on the whole filesystem
docker-trust-signer (1) - Manage entities who can sign Docker images
w (1) - Show who is logged on and what they are doing.
who (1) - show who is logged on
whoami (1) - print effective userid

whatis - Displays a concise description of a command

The whatis command is used to query what a command does and prints the query result to the terminal.

The whatis command searches the database created using the catman -w command for the command parameter specified, including system calls, library functions, or special file names. The whatis command displays the header line of the manual section. Additional information can then be retrieved using the man command. The whatis command is equivalent to using the man -f command.

1
2
3
4
5
6
[root@lht ~]# whatis ls
ls (1) - list directory contents
[root@lht ~]# whatis cp
cp (1) - copy files and directories
[root@lht ~]# whatis man
man (1) - an interface to the on-line reference manuals

info - Displays program Info entries

The info command is used in Linux to view help documentation in Info format.

In terms of content, Info pages are written better, easier to understand, and more user-friendly than man pages, but man pages are easier to use. A man page consists of only one page, whereas Info pages usually organize content into multiple sections (called nodes), and each section may also contain subsections (called subnodes). The trick to understanding how to use the info command is learning how to navigate through individual Info pages and how to switch between nodes and subnodes. It might be difficult at first to move between nodes in Info pages and find the content you need. Ironically, Info pages are supposed to be better for beginners compared to the man command, but in reality, learning and using them is more challenging.

Refer to the parameters and usage examples at:

info 命令,Linux info 命令详解:Linux下info格式的帮助指令 - Linux 命令搜索引擎

Creating custom commands with alias

1
2
3
4
[root@lht ~]# alias mycommand='cd /usr; ls; cd -'
[root@lht ~]# mycommand
bin etc games include java lib lib64 libexec local sbin share src tmp
/root

After the alias command, enter the name followed (without space) by an equal sign, and after the equal sign is a string enclosed in quotes. The content of the string is assigned to the name. Once we define an alias, this alias can be used anywhere.

Check the command we defined:

1
2
3
4
5
[root@lht ~]# type mycommand
mycommand is aliased to `cd /usr; ls; cd -'
[root@lht ~]# which mycommand
alias mycommand='cd /usr; ls; cd -'
/usr/bin/cd

You can delete the alias using the unalias command:

1
2
3
[root@lht ~]# unalias mycommand
[root@lht ~]# type mycommand
-bash: type: mycommand: not found
  • Title: Linux command line 3 - Working with commands
  • Author: LHT
  • Created at : 2023-01-03 08:00:00
  • Link: https://blog.327774.xyz/2023/01/03/linux/Linux command line 3 - Working with commands/
  • License: This work is licensed under CC BY-NC-SA 4.0.