As a DevOps engineer or a sys admin, it’s essential to master Linux. While the command-line interface (CLI) is a powerful tool, navigating it can be daunting, especially for new users. That’s why getting help in Linux is essential to efficiently and effectively using the CLI.
In this tutorial, we’ll explore four fundamental commands that can help you get the help you need: man
pages, type
, help
, and apropos
. These commands can provide helpful information about the usage, syntax, and options of other commands, making them indispensable tools for any Linux user.
Whether you’re new to Linux or a seasoned professional, mastering these commands will undoubtedly improve your productivity and make your life easier.
Man Pages
Man pages, short for “manual pages,” are built-in documentation in Linux that provides detailed information on commands, utilities, and other functions available in the system. Many commands in Linux has a corresponding man page that explains its usage, syntax, and options. Man pages can be accessed via the command line by typing man
followed by the name of the command you want to learn about.
If you want to quit from a man page, you can type the lowercase letter q
. This will immediately exit the man page and return you to the command line.
Here are 5 ways you can consider when using the man
command:
Viewing the man page for the ls
command
To view the man page for the ls
command, open the terminal and type the following command:
man ls
This will display the man page for the ls
command, which provides detailed information on its usage, syntax, and options. You can use the arrow keys to scroll through the man page. You can also move quickly by using space to move forward one page or CTRL+b to move backward.
Using the -f
option to view a brief description of a command
To view a brief description of a command, use the -f
or --whatis
option with the man
command. For example, to view a brief description of the ls
command, type the following command:
man -f ls
Output:
ls (1) - list directory contents
As you can see, it displays a brief description of the ls
command, which can be useful if you’re not sure what a particular command does.
Using the -k
or apropos
command to search for man pages related to a keyword
To search for man pages related to a keyword, use the -k
option. For example, to search for man pages related to the keyword “ifconfig”, type the following command:
man -k ifconfig
Output:
ifcfg (8) - simplistic script which replaces ifconfig IP management
ifconfig (8) - configure a network interface
As you can see, it searches for the “ifconfig” keyword in all man pages, which can be useful if you’re not sure which command to use for a specific task.
If the keyword consists of multiple words, you can put them between double (or single) quotes, such as the following:
man -k "copy file"
This will list all commands in your system related to copying files.
The man -k
is similar to the apropos
command which I will discuss in a later section.
Using the -P
option to specify a pager other than less
By default, the man
command uses the less
program to display man pages. If you prefer to use a different pager program, you can use the -P
option to specify it. For example, to use the more
pager to display the man
page for the ls
command, type the following command:
man -P more ls
This will display the man page for the ls
command using the more
pager instead of less
.
Searching inside the man page
Man pages can be lengthy, and scrolling through them can be time-consuming. Fortunately, you can search for specific text within a man page to quickly find the information you need. To search for text within a man page, you can use the /
command followed by the search term.
For example, if you want to search for the term “recursive” within the man page for the ls
command, you can enter the man page of the ls
command and type the following:
/recursive
This will highlight the first occurrence of the term “recursive” within the man page. To search for the next occurrence, press the n
key. To search for the previous occurrence, press the N
key.
In addition to searching forward within a man page using the /
command, you can also search in reverse order using the ?
command. The ?
command works in a similar way to the /
command, but it searches backwards from the current position instead of forwards.
Let’s test this and navigate the end of the man page by typing the G
command. Btw, to navigate to the top of the page you can type the g
command.
So assuming you’re at the ls
command and at the end of its man page, type a question mark followed by the search term you want to find. For example, to search for the term “recursive” in reverse order within the man page for the ls
command, you can type the following:
?recursive
This will highlight the last occurrence of the term “recursive” within the man page. To search for the previous occurrence, press the uppercase n
key. To search for the next occurrence, press the lowercase N
key.
Type
In addition to the man
command, the type
command is another useful tool for working with Linux commands. The type
command allows you to determine the location of a command or executable file, as well as its type.
To use the type
command, simply type “type” followed by the name of the command you want to query.
Let’s say you have two versions of the python
command installed on your system - one in /usr/bin/python
and another in /usr/local/bin/python3.9
. To determine which version of the python command will be executed when you run python
, you can use the type
command like this:
$ type python
python is /usr/bin/python
Here, the type
command shows that the version of python
located in /usr/bin
will be executed when the python
command is run.
The type
command can also tell you whether the command is an alias or a shell function.
If you use the following command:
type cd
You may find the following output “cd is a shell builtin”, as it is a built-in command in most shells. The built-in shell commands don’t typically have separate man pages, since they are part of the shell itself. Instead, you can use the type
command to get information about built-in commands.
If you try to use man
to view the man page for cd
, you may see an error message like “No manual entry for cd”.
This should take us to the following section to know how we can get a similar page to the manual for the cd
command case.
Help
The help
command is a built-in command in many shells that provides help documentation for built-in commands and scripts. It’s similar to the man
command, but is typically used for shorter, more concise help documentation that is specific to the shell.
As we saw in the previous section, there is no manual page for the cd
command. So let’s use the help
command to show a help message that provides basic information about the command syntax and usage. Here’s an example:
help cd
Also, the --help
option will do the trick for a command like ls
which is not a built-in shell command. You can try:
ls --help
You can customize the help functionality to give information about your shell scripts as well.
Assuming that the script is in your current working directory, you can simply type ./scriptname.sh --help
or bash scriptname.sh --help
to see the help information for that script.
Note: this is not the help
linux command, but it gives a customized version of your own shell script that you wrote or a script you’re working on.
Here’s a simple example of a Bash script that takes a filename as an argument and counts the number of lines in that file:
#!/bin/bash
echo "The file '$1' has $(wc -l < $1) lines"
To test this script, you could save it as linecount.sh
and then make it executable:
chmod u+x linecount.sh
You could fill in a text file (called, myfile.txt) with (e.g. 7) dummy lines and then run ./linecount.sh myfile.txt
to see the line count for that file. You’ll see an output like this:
The file myfile.txt has 7 lines
Typically, you want to catch the error when the user enters an argument that is not a file. So the linecount.sh
is now as follows:
#!/bin/bash
if [ ! -f $1 ]; then
echo "Error: '$1' is not a file"
exit 1
fi
echo "The file '$1' has $(wc -l < $1) lines"
Now, if you run ./linecount.sh --help
, you’ll get the following error message:
Error: '--help' is not a file
What if you want to add a help functionality so that when the user enters no argument, he’ll get a useful help message to remind him of the usage of this script?
Then you can edit the bash script to be as follows:
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
if [ ! -f $1 ]; then
echo "Error: '$1' is not a file"
exit 1
fi
echo "The file '$1' has $(wc -l < $1) lines"
So if you run ./linecount.sh
without any arguments, you’ll get an error message:
Usage: ./linecount.sh filename
But adding the --help
option to that script will be much better than if we left the user fall into the no argument error. So you can add the --help
argument as follows:
#!/bin/bash
if [ $# -eq 1 ] && [ "$1" == "--help" ]; then
echo "This script counts the number of lines in a file."
echo "Usage: $0 filename"
exit 0
fi
if [ $# -ne 1 ]; then
echo "Usage: $0 filename"
exit 1
fi
if [ ! -f $1 ]; then
echo "Error: '$1' is not a file"
exit 1
fi
echo "The file '$1' has $(wc -l < $1) lines"
Now, you can get the help of the script by typing ./linecount.sh --help
which outputs the following:
This script counts the number of lines in a file.
Usage: ./linecount.sh filename
In the previous modifier version of the script, we first check to see if the --help
option has been passed as the only argument. If it has, we display some help information and then exit the script with a status of 0
(which means “success”).
If the --help
option has not been used and the number of arguments is not exactly 1, we display a usage message and exit with a status of 1
(which means “error”). Otherwise, we check to make sure that the argument is a file and then count the number of lines in it and display the result.
Apropos
The apropos
command is used to search the system’s manual pages for a list of commands related to a particular keyword. It is particularly useful when you know what you want to do, but you don’t know the specific command to use.
To use apropos
, simply run the command followed by the keyword you want to search for. For example, to find all commands related to “compression,” you would run:
apropos compression
This will return a list of all the commands related to compression on your system. Each entry in the list will include the command name, a brief description of what the command does, and the name of the manual page that describes the command in more detail.
Keep in mind that the apropos
command searches only the manual pages that are installed on your system, so if a particular command does not have a manual page, it will not be found by apropos
.
This command is similar to the man
command with the -k
option as we discussed earlier.
Conclusion
Getting help is an essential skill for any Linux user, whether you are a beginner or an experienced developer. With so many commands and tools available on Linux, it can be difficult to remember all the details of each one, which is why having access to help resources is so important.
Throughout this article, we have discussed several commands that can help you get the help you need on Linux, including man
, type
, help
, and apropos
. These commands each offer unique features and functionality, but all can be used to quickly and easily find information about the commands you need to use.
Keep in mind that this is just the tip of the iceberg when it comes to Linux, and there is always more to learn.