Linux command line 3 - Working with commands
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:
- An Executable Program: Programs like those found in the
/usr/bindirectory. These programs can be binary files compiled from languages like C or C++, or scripts written in languages such asshell,perl,python, orruby. - A Built-in Shell Command: Commands integrated into the shell itself. Bash includes several of these, called shell builtins. For example, the
cdcommand is a shell builtin. - 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.
- 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 | [root@lht bin]# type type |
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 | [root@lht ~]# which ls |
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 | [root@lht ~]# help cd |
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 | [root@lht ~]# cd --help |
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 | [root@lht ~]# man -k who |
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 | [root@lht ~]# whatis ls |
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 | [root@lht ~]# alias mycommand='cd /usr; ls; cd -' |
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 | [root@lht ~]# type mycommand |
You can delete the alias using the unalias command:
1 | [root@lht ~]# unalias mycommand |
- 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.