Skip to content

Finding trustworthy help

Module
Understanding Commands
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21
On this page

ls shows what is in ~/lab. ls Documents shows what is in Documents. You want both in one listing: everything in ~/lab, and everything inside each directory in it. None of the options you know does that.

ls has many more options, but guessing letters is not a method: one wrong letter, and the command does something you did not ask for. The answer is already on your computer. Each program comes with a description of its options, written for the version you have. By the end of this lesson, you can open that description, search it, and run the option it names.

Many programs print a short guide to themselves when you give them the option --help:

Terminal window
ls --help
Output (shortened)
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
…
Full documentation <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'

Read the first line. Usage: shows the shape of the command: the name ls, then options, then files. Square brackets mean the part is optional. The three dots mean you can give more than one. In this guide, FILE means any name, a directory included.

Below that, each option gets one line: the short form, the long form, and what it does. You know the first two from lesson B04.1.

The line about “mandatory arguments” means: when the long form of an option needs a value, the short form needs one too. You saw this with head, where -n and --lines both need a number.

There is one problem. On the lab system, ls --help prints 138 lines. They scroll past, and you see only the end.

The full description of a program is its manual page, or man page for short. The command man opens it:

Terminal window
man ls

This time the prompt does not come back. The page fills the terminal, and it starts like this:

Top of the page
LS(1) User Commands LS(1)
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
  • NAME says in one line what the program does.
  • SYNOPSIS is the same usage line that --help printed.
  • DESCRIPTION explains the options, one paragraph each: the option names on one line, and the explanation indented below them.

man shows the page in a pager: a program that shows text one screen at a time and waits for your keys. On Ubuntu, the pager is less. The last line of the screen names the page, ls(1), and reminds you that h shows help and q quits.

While the page is open, your keys go to the pager, not to Bash. These are the ones you need:

Key What it does
Space Show the next screen
b Show the previous screen
Down, Up Move one line
/, a word, then Enter Search forward for the word
n Go to the next match
N Go to the previous match (capital N)
q Quit and return to the prompt
h Show every key the pager knows (q closes it)

Back to the problem. A directory inside another directory is called a subdirectory. An option that lists them would likely use that word, so search for it:

  1. Run man ls.
  2. Type /subdirectories. The / and the word appear on the last line of the screen.
  3. Press Enter. The pager moves to the first line that contains the word and marks it: list subdirectories recursively.
  4. The pager puts the match on the top line of the screen, so the option names are just above it, out of view. Press Up once: the line reads -R, --recursive.
  5. Press q. The prompt comes back.

If the first match is not the one you want, press n until you reach it.

Recursively means: list each subdirectory, then the ones inside it, and so on, until none are left. Run the option you found:

Terminal window
ls -R
Output
.:
Documents projects todo.txt
./Documents:
notes.txt report.txt
./projects:

Each block starts with a directory name and a colon. . is the current directory, ~/lab, and projects is empty. That is the listing you wanted at the start.

Notice the capital letter in -R. Guess first: what does ls -r, with a lowercase r, do?

Terminal window
ls -r
Run it, then compare with your guess
Output
todo.txt projects Documents

Something else entirely. The manual lists -r, --reverse right above -R: “reverse order while sorting”. As with -a and -A in lesson B04.1, the capital letter makes it a different option.

When you already know the word, you can search without opening the pager:

Terminal window
ls --help | grep subdirectories
Output
-R, --recursive list subdirectories recursively

The | passes the output of ls --help to the program grep, which prints only the lines that contain subdirectories. Module B09 explains |, and module B10 explains grep. This works best with --help, which puts the option names and their explanation on one line.

Finding an option in the help text, then using it
learner@rootvolt:~/lab$ lsDocuments  projects  todo.txtlearner@rootvolt:~/lab$ ls --help | grep subdirectories  -R, --recursive            list subdirectories recursivelylearner@rootvolt:~/lab$ ls -R.:
Documents  projects  todo.txt

./Documents:
notes.txt  report.txt

./projects:learner@rootvolt:~/lab$ 

Files ~/lab

  • ~
    • lab/cwd
      • Documents/listed
        • notes.txtlisted-rw-r--r--
        • report.txtlisted-rw-r--r--
      • .draft.txthidden-rw-r--r--
      • projects/listed
      • todo.txtlisted-rw-r--r--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21. Searching the help text of ls for the word subdirectories names the option -R, and ls -R lists every directory in ~/lab.
Steps (10)
  1. Type ls — The starting point: only the top level of ~/lab
  2. Press Enter
  3. Output: Documents projects todo.txt
  4. Type ls --help | grep subdirectories — Keep only the help lines that contain the word
  5. Press Enter
  6. Output: -R, --recursive list subdirectories recursively
  7. Type ls -R — Use the option the help text named
  8. Press Enter
  9. Output: .: Documents projects todo.txt ./Documents: notes.txt report.txt ./projects:
  10. Search the help for a word that describes what you want, then run the option it names.

In words: ls prints Documents projects todo.txt. The search prints the one line that names -R, --recursive. Then ls -R lists ~/lab, ./Documents, and ./projects, each in its own block.

The top line of the page said LS(1). The manual is divided into numbered sections, and the number in parentheses is the section: ls(1) means “the page ls in section 1”. You meet these three most often:

Section What its pages describe Example
1 Programs you run as a user ls(1)
5 File formats: what the lines in a file mean passwd(5)
8 System administration commands, usually run as root useradd(8)

man man lists every section.

One name can have pages in several sections. passwd is the program that changes your password, and also the file /etc/passwd, which lists the accounts on the system. Guess first: which page does man passwd open, the program or the file?

Run it, then compare with your guess

The top line of the page:

Top line
PASSWD(1) User Commands PASSWD(1)

The program. man looks through the sections in a fixed order, starting with section 1, and opens the first page it finds.

To open a page from another section, put the section number before the name:

Terminal window
man 5 passwd
Top line
PASSWD(5) File Formats and Configuration PASSWD(5)

To see every page with a name before you open one, use man -f:

Terminal window
man -f passwd
Output
passwd (1) - change user password
passwd (1ssl) - OpenSSL application commands
passwd (5) - the password file

The middle page belongs to OpenSSL, a separate set of programs with its own openssl passwd command. The suffix ssl marks a group of pages inside section 1.

The last lines of ls --help point to a longer manual in a format called Info, read with the program info. GNU programs such as ls and head often have an Info manual that explains more than the man page. The second line names the command that opens it:

Terminal window
info '(coreutils) ls invocation'

The chapter about ls opens in a pager, as a man page does. It starts like this:

Top of the chapter
File: coreutils.info, Node: ls invocation, Next: dir invocation, Up: Directory listing
10.1 ‘ls’: List directory contents
==================================

The first line names the manual, coreutils.info, and the chapter, ls invocation. Next names the following chapter, and Up the part of the manual it belongs to. Space shows the next screen, and q quits and returns to the prompt.

Some systems do not have the program info. There, the command prints a not found message instead, and the same manual is on the GNU website, at the “Full documentation” address. Module B12 shows how to install programs.

Packages can also put files in /usr/share/doc, in a directory named after the package:

Terminal window
ls /usr/share/doc/bash
Output
changelog.Debian.gz INTRO.gz README.abs-guide README.md.bash_completion.gz
COMPAT.gz NEWS.gz README.commands.gz
copyright POSIX.gz README.Debian.gz
inputrc.arrows RBASH README.gz

You find README files, notes on changes (NEWS, changelog), and the license in copyright. Names ending in .gz are compressed; zless followed by the file name opens one in the same pager, with the same keys. These files rarely explain options; the man page does.

Asking for a page that does not exist. You have already used cd, but:

Terminal window
man cd
Error output
No manual entry for cd

man found no page with that name in any section. The lab system has no page for cd, because cd is not a separate program: it is part of Bash. Bash describes its own commands with help:

Terminal window
help cd
Output (shortened)
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
…

Lesson B04.4 shows how to find out whether a command is part of Bash.

Asking for the wrong section. A section number limits the search to that section:

Terminal window
man 5 ls
Error output
No manual entry for ls in section 5

The page exists, but only in section 1. Leave out the number, or run man -f ls to see which sections have a page.

One dash in front of help. --help is a long option, so it needs two dashes:

Terminal window
ls -help
Error output
ls: invalid option -- 'e'
Try 'ls --help' for more information.

With one dash, ls reads -help as four short options, -h, -e, -l, and -p, and stops at -e, which does not exist. The second line tells you the spelling ls expects.

When you do not know the command name. man -k searches the names and one-line descriptions of every page. The quotes keep the two words together as one search:

Terminal window
man -k 'list directory'
Output
dir (1) - list directory contents
ls (1) - list directory contents
vdir (1) - list directory contents

Values that differ on your system. The files in /usr/share/doc depend on what is installed, and the date on the last line of a man page depends on the package version.

A command on a web page was written for someone else’s system. The same option can mean something different in another version of a program, or in another program with the same name, and the page rarely says which one its author had. The manual on your computer names the version on its last line:

Terminal window
man ls | tail -n 1
Output
GNU coreutils 9.4 August 2026 LS(1)

tail -n 1 prints only the last line. Compare it with the version of the program itself:

Terminal window
ls --version | head -n 1
Output
ls (GNU coreutils) 9.4

They match, because Ubuntu installs the page and the program together, in the same package. So before you run a command from a page you do not know, look up each option in your own manual. If an option is missing there, or the manual describes it differently, do not run the command.

Goal: Print todo.txt with a number in front of each line, using cat from lesson B04.2 and an option you find yourself.

  1. Open the manual page for cat.
  2. Search it for a word that the explanation of such an option would contain.
  3. When you have found the option, quit the page and run it on todo.txt.

Expected: The three lines of todo.txt, numbered 1 to 3.

Hint

Search for /number. The first match may not be the option you want; read its explanation, then press n for the next match.

Solution

In man cat, /number first finds -b, --number-nonblank: “number nonempty output lines”. Press n for -n, --number: “number all output lines”. The search from the prompt shows both:

Terminal window
man cat | grep number
Output
-b, --number-nonblank
number nonempty output lines, overrides -n
-n, --number
number all output lines

Run the option:

Terminal window
cat -n todo.txt
Output
1 Install Ubuntu
2 Verify the ISO checksum
3 Take a snapshot

cat -b todo.txt prints the same, because todo.txt has no empty lines; -b only skips empty lines when it counts.

  • --help prints a short guide to a program’s options. In the usage line, square brackets mean optional and three dots mean “more than one”.
  • man opens the manual page in a pager. Space and b move by screens, /word searches, n and N move between matches, and q quits.
  • The number in ls(1) or passwd(5) is the manual section. man 5 passwd opens the page from section 5, and man -f lists every page with a name.
  • help describes Bash’s own commands, such as cd. Longer manuals may be installed as Info documents and in /usr/share/doc.
  • Your own man page describes the version you have. Check a copied command’s options there before you run it.

Next, in Reading errors and identifying commands, you read an error message and find out what kind of command a name refers to.

  • man man and man less on your system, or the Ubuntu 24.04 manual pages online: man(1) (with the list of sections), less(1), ls(1), cat(1).
  • GNU Coreutils manual: ls invocation, the Info chapter that ls --help points to.
  • GNU Bash Reference Manual: Bash Builtin Commands, including help.
  • GNU Texinfo: Stand-alone GNU Info, the manual for the info program.
  • Debian Policy Manual: Documentation, what packages install in /usr/share/doc. Ubuntu follows the same layout.