2.2 Lesson 1
Certificate: |
Linux Essentials |
---|---|
Version: |
1.6 |
Topic: |
2 Finding Your Way on a Linux System |
Objective: |
2.2 Using the Command Line to Get Help |
Lesson: |
1 of 1 |
Introduction
The command line is a very complex tool. Each command has its own unique options, therefore documentation is key when working with a Linux system. Besides the /usr/share/doc/
directory, which stores most of the documentation, various other tools provide information on using Linux commands. This chapter focuses on methods to access that documentation, with the purpose of getting help.
There are a multitude of methods to get help within the Linux command line. man
, help
and info
are just a few of them. For Linux Essentials, we will be focusing on man
and info
as they are the most commonly used tools for obtaining help.
Another topic of this chapter will be locating files. You will mainly work with the locate
command.
Getting Help on the Command Line
Built-in Help
When started with the --help
parameter, most commands display some brief instructions about their usage. Although not all commands provide this switch, it is still a good first try to learn more about the parameters of a command. Be aware that the instructions from --help
often are rather brief compared to the other sources of documentation which we will discuss in the rest of this lesson.
Man Pages
Most commands provide a manual page or “man” page. This documentation is usually installed with the software and can be accessed with the man
command. The command whose man page should be displayed is added to man
as an argument:
$ man mkdir
This command opens the man page for mkdir
. You can use the up and down arrow keys or the space bar to navigate through the man page. To exit the man page, press Q.
Each man page is divided in maximum of 11 sections, though many of these sections are optional:
Section | Description |
---|---|
NAME |
Command name and brief description |
SYNOPSIS |
Description of the command’s syntax |
DESCRIPTION |
Description of the effects of the command |
OPTIONS |
Available options |
ARGUMENTS |
Available arguments |
FILES |
Auxiliary files |
EXAMPLES |
A sample of the command line |
SEE ALSO |
Cross-references to the related topics |
DIAGNOSTICS |
Warning and Error messages |
COPYRIGHT |
Author(s) of the command |
BUGS |
Any known limitations of the command |
In practice, most man pages don’t contain all of these parts.
Man pages are organized in eight categories, numbered from 1 to 8:
Category | Description |
---|---|
1 |
User command |
2 |
System calls |
3 |
Functions of the C library |
4 |
Drivers and device files |
5 |
Configuration files and file formats |
6 |
Games |
7 |
Miscellaneous |
8 |
System administrator commands |
9 |
Kernel functions (not standard) |
Each man page belongs to exactly one category. However, multiple categories can contain man pages with the same name. Let’s take the passwd
command as an example. This command can be used to change a user’s password. Since passwd
is a user command, its man page resides in category 1. In addition to the passwd
command, the password database file /etc/passwd
also has a man page which is called passwd
, too. As this file is a configuration file, it belongs to category 5. When referring to a man page, the category is often added to the name of the man page, as in passwd(1)
or passwd(5)
to identify the respective man page.
By default, man passwd
displays the first available man page, in this case passwd(1)
. The category of the desired man page can be specified in a command such as man 1 passwd
or man 5 passwd
.
We have already discussed how to navigate through a man page and how to return to the command line. Internally, man
uses the less
command to display the man page’s content. less
lets you search for text within a man page. To search for the word linux
you can just use /linux
for forward searching from the point that you are on the page, or ?linux
to start a backward search. This action highlights the all the matching results and moves the page to the first highlighted match. In both cases you can type N to jump to the next match. In order to find more information about these additional features, press H and a menu with all the information will be displayed.
Info Pages
Another tool that will help you while working with the Linux system are the info pages. The info pages are usually more detailed than the man pages and are formatted in hypertext, similar to web pages on the Internet.
The info pages can be displayed like so:
$ info mkdir
For each info page, info
reads an info file that is structured into individual nodes within a tree. Each node contains a simple topic and the info
command contains hyperlinks that can help you move from one to the other. You can access the link by pressing enter while placing the cursor on one of the leading asterisks.
Similar to man
, the info
tool also has page navigation commands. You can find out more about these command by pressing ? while being on the info page. These tools will help you navigate the page easier as well as understand how to access the nodes and move within the node tree.
The /usr/share/doc/
directory
As mentioned before, the /usr/share/doc/
directory stores most documentation of the commands that the system is using. This directory contains a directory for most packages installed on the system. The name of the directory is usually the name of the package and occasionally its version. These directories include a README
or readme.txt
file that contains the package’s basic documentation. Alongside the README
file, the folder can also contain other documentation files, such as the changelog which includes the history of the program in detail, or examples of configuration files for the specific package.
The information within the README
file varies from one package to another. All files are written in plain text, therefore they can be read with any preferred text editor. The exact number and kinds of files depend on the package. Check some of the directories to get an overview of their contents.
Locating files
The locate
command
A Linux system is built from numerous directories and files. Linux has many tools to locate a particular file within a system. The quickest one is the command locate
.
locate
searches within a database and then outputs every name that matches the given string:
$ locate note /lib/udev/keymaps/zepto-znote /usr/bin/zipnote /usr/share/doc/initramfs-tools/maintainer-notes.html /usr/share/man/man1/zipnote.1.gz
The locate
command supports the usage of wildcards and regular expressions as well, therefore the search string does not have to match the entire name of the desired file. You will learn more about regular expressions in a later chapter.
By default, locate
behaves as if the pattern would be surrounded by asterisks, so locate PATTERN
is the same as locate *PATTERN*
. This allows you just provide substrings instead of the exact filename. You can modify this behavior with the different options that you can find explained in the locate
man page.
Because locate
is reading from a database, you may not find a file that you recently created. The database is managed by a program named updatedb
. Usually it is run periodically, but if you have root privileges and you need the database to be updated immediately, you can run the updatedb
command yourself at any time.
The find
command
find
is another very popular tool that is used to search for files. This command has a different approach, compared to the locate
command. find
command searches a directory tree recursively, including its subdirectories. find
does such a search at each invocation, it does not maintain a database like locate
. Similar to locate
, find
also supports wildcards and regular expressions.
find
requires at least the path it should search. Furthermore, so-called expressions can be added to provide filter criteria for which files to display. An example is the -name
expression, which looks for files with a specific name:
~$ cd Downloads ~/Downloads $ find . -name thesis.pdf ./thesis.pdf ~/Downloads $ find ~ -name thesis.pdf /home/carol/Downloads/thesis.pdf
The first find
command searches for the file within the current Downloads
directory, whereas the second one searches for the file in the user’s home directory.
The find
command is very complex, therefore it will not be covered in the Linux Essentials exam. However, it is a powerful tool which is particularly handy in practice.
Guided Exercises
-
Use the
man
command to find out what each command does:Command Description ls
Display the contents of a directory.
cat
cut
cd
cp
mv
mkdir
touch
wc
passwd
rm
rmdir
more
less
whereis
head
tail
sort
tr
chmod
grep
-
Open the
ls
info page and identify the MENU.-
What options do you have?
-
Find the option that allows you to sort the output by modification time.
-
-
Display the path to the first 3
README
files. Use theman
command to identify the correct option forlocate
. -
Create a file called
test
in your home directory. Find its absolute path with thelocate
command. -
Did you find it immediately? What did you have to do in order for
locate
to find it? -
Search for the test file that you previously created, using the
find
command. What syntax did you use and what is the absolute path ?
Explorational Exercises
-
There is one command in the table above that doesn’t have a
man
page. Which one is it and why do you think that the command doesn’t have a man page? -
Using the commands in the table above, create the following file tree. The names that start with a capital are Directories and the ones in lower case are files.
User ├── Documents | ├──Hello | | ├──hey2 | | ├──helloa | | └──ola5 | └──World | └──earth9 ├── Downloads | ├──Music | └──Songs | ├──collection1 | └──collection2 ├── Test | └── passa └── test
-
Display on the screen the present working directory, including the subfolders.
-
Search within the tree for all files that end with a number.
-
Remove the entire directory tree with a single command.
Summary
In this lesson you learned:
-
How to get help
-
How to use the
man
command -
How to navigate the
man
page -
Different sections of the
man
page -
How to use the
info
command -
How to navigate between different nodes
-
How to search for files within the system
Commands used in the exercises:
man
-
Display a man page.
info
-
Display an info page.
locate
-
Search the
locate
database for files with a specific name. find
-
Search the file system for names matching a set of selection criteria.
updatedb
-
Update the
locate
database.
Answers to Guided Exercises
-
Use the
man
command to find out what each command does:Command Description ls
Display the contents of a directory.
cat
Concatenates or views text files
cut
Removes sections from a text file
cd
Changes to a different directory
cp
Copies a file
mv
Moves a file (it can also be used to rename)
mkdir
Creates a new directory
touch
Creates a file or modifies an existing file’s last modified time and date
wc
Counts the number of words, lines or bytes of a file
passwd
Changes a user’s password
rm
Deletes a file
rmdir
Deletes a directory
more
Views text files one screen at a time
less
Views text files, allows scrolling up and down a line or page at a time
whereis
Displays the file path to a specified program and related manual files
head
Displays the first few lines of a file
tail
Displays the last few lines of a file
sort
Orders a file numerically or alphabetically
tr
Translates or removes characters of a file
chmod
Changes a file’s permissions
grep
Searches within a file
-
Open the
ls
info page and identify the MENU.*-
What options do you have?
-
Which files are listed
-
What information is listed
-
Sorting the output
-
Details about version sort
-
General output formatting
-
Formatting file timestamps
-
Formatting the file names
-
-
Find the option that allows you to sort the output by modification time.
-t
or--sort=time
-
-
Display the path to the first 3
README
files. Use theman
command to identify the correct option forlocate
.$ locate -l 3 README /etc/alternatives/README /etc/init.d/README /etc/rc0.d/README
-
Create a file called
test
in your home directory. Find its absolute path with thelocate
command.$ touch test $ locate test /home/user/test
-
Did you find it immediately? What did you have to do in order for
locate
to find it?$ sudo updatedb
The file is newly created, therefore there is no record of it in the database.
-
Search for the test file that you previously created, using the
find
command. What syntax did you use and what is the absolute path ?$ find ~ -name test
or
$ find . -name test /home/user/test
Answers to Explorational Exercises
-
There is one command in the table above that doesn’t have a
man
page. Which one is it and why do you think that the command doesn’t have a man page?The
cd
command. It doesn’t have a man page because it is a built-in shell command. -
Using the commands in the table above, create the following file tree. The names that start with a capital are Directories and the ones in lower case are files.
User ├── Documents | ├──Hello | | ├──hey2 | | ├──helloa | | └──ola5 | └──World | └──earth9 ├── Downloads | ├──Music | └──Songs | ├──collection1 | └──collection2 ├── Test | └── passa └── test
The solution is a combination of
mkdir
andtouch
commands. -
Display on the screen the present working directory, including the subfolders.
$ ls -R
-
Search within the tree for all files that end with a number.
$ find ~ -name "*[0-9]" $ locate "*[0-9]"
-
Remove the entire directory tree with a single command.
$ rm -r Documents Downloads Test test