Learning more about a command

Learning more about a command#

So far, we have provided explanations to you that describe what each command does and which options can be used to run the same command to receive variable outputs. However, in a real life scenario it is more likely that you want to find the answer to a question without knowing exactly how. Most often, you will have to figure out which commands to run with which options to receive the answer to your question.

The first recommended step to learn more about a command is to use the command man followed the command of interest directly on the command line to read the manual and try to understand more about the main function of a command, available options, different output files and other useful information. Manual pages are typically structured in a consistent manner and split into the sections: NAME, SYNOPSIS, DESCRIPTION, AUTHOR, COPYRIGHT and SEE ALSO. Remember that after inspecting a manual page (similar to an open file), you need to close this page by typing ‘q’ which stands for quit to return to the command line.

For instance, you can run man on some of the commands you recently used.

# Check the manual of the command echo
man echo

# Check the manual of the command wc
man wc

# Check the manual of the command grep
man grep

# Close an open manual page by typing q

Instead, you may want to print the most important parts of the manual page to the command line to have it visible while typing your command and selecting options. Instead of entering the manual page interactively, you can type your command followed by the option –help to print a shorter version of the manual page to the command line.

# Print the help page of the command wc
wc --help

# Print the help page of the command grep
grep --help

Since developers can decide how their command for requesting the help page is built, for some commands command –help will not lead to the help page and is invalid. Instead either command -h or simply command will print the help page

# Different ways to request of help page
command --help
command -h
command

Exercise 2.1#

Exercise 2.1

  • Check the manual of the command echo. Which section describes the options for a command?

# Check the manual of echo
man echo
# Note that the -n option that you used last week is described under the section DESCRIPTION
# To close the open manual page, type q which stands for quit
  • Check the manual of the command wc. Can you find the descriptions for the options that you used previously with the command wc? Do they contain any additional information?

# Check the manual of wc
man wc
# Note the options -m, -l, -w which we explained in the material of last week
# Note the short(-)/long(--) versions of each of the options (-m = --chars, -l = --lines, -w = --words) which can be used interchangeably
# To close the open manual page, type q which stands for quit
  • Check the manual of the command grep. What are the main differences between this manual page and the previous ones?

# Check the manual of grep
man grep
# Note the complexity of the manual page with many more sections and descriptions
# This shows you that grep is a complex but powerful command, when using it correctly
# To close the open manual page, type q which stands for quit
  • Imagine you want to extract the number of words in a text file named example.txt and do not remember which option of the command wc to use. How would you proceed?

# Print the help page of wc
wc --help

# Select the right option to perform a word count
wc -w example.txt
  • In the examples above, we show that you might want to learn more about the options of echo. What happens if you use –help to get further information?

# Print the help page of echo
echo --help
# Note that instead of printing the help page, echo simply prints --help as a word
# Keep this example in mind, as on the next page you may want to find out how to print the help page of echo
  • What happens if you compare different approaches to request the help page for grep, namely by typing grep –help, grep -h or simply grep?

# Print the help page of echo by alternative requests
grep --help
grep -h
grep
# Note that for the second and third option, instead of printing the help page grep prints an error message saying try 'grep --help' for more information.