Unit 2: Reading and checking files · Lesson 3 of 6 · 12 min

Getting help

--help, help and type: answers without leaving the terminal.

mission

You are
You are working on a server with no internet and no manual pages.
Situation
You need an option you cannot remember, and you want to be sure which program actually runs when you type a name.
Goal
Get usage help for programs and builtins, and find where commands live.

Where help lives

  1. typeWhat will run: builtin, alias or program
  2. --helpUsage for programs
  3. helpUsage for builtins
  4. manFull manuals, when installed
01

Programs and builtins

Some commands are programs on disk. Others are built into bash. type tells you which.

learner@root-lab
type cd
cd is a shell builtin
type ls
ls is /usr/bin/ls
type -a echo
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

you should see type -a lists every match in order. The first one wins.

type
Shows whether a name is a shell builtin, an alias or a program on disk. A command built into the bash shell.
02

Read the usage

learner@root-lab
ls --help | head -n 1
Usage: ls [OPTION]... [FILE]...
help cd | head -n 1
cd: cd [-L|[-P [-e]] [-@]] [dir]
ls --version | head -n 1
ls (GNU coreutils) 9.4

defender tip Pipe long help into grep: ls --help | grep -i time finds the sorting options in a second.

ls
Lists the files in a folder. Built into Linux and macOS.
head
Prints the first lines of a file or of piped text. Built into Linux and macOS.
03

Search inside help text

Help text is plain text, so grep finds the one option you need. compgen -b lists every builtin.

learner@root-lab
ls --help | grep -- '--sort='
                             use of --sort=none (-U) disables grouping
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                             with --sort=time, sort by WORD (newest first)
compgen -b | wc -l
61

you should see The --sort option and 61 builtins.

grep
Prints the lines of text that match a pattern. Built into Linux and macOS.
compgen
Lists the commands, builtins or names the shell knows about. A command built into the bash shell.
wc
Counts lines, words and bytes. Built into Linux and macOS.

What a defender takes away

  1. 1--help for programs, help for builtins.
  2. 2type and which show what will really run.
  3. 3Grep the help text instead of reading all of it.

Questions

0 of 2 solved

Answer each one from what you find on your lab machine.

  1. Show hint

    which ls

  2. Show hint

    type pwd