Practice: Understanding Commands
On this page
Before you take a snapshot of your lab VM, you want to know how long your lists are: how many lines todo.txt has, and how many lines each file in Documents has. A colleague who keeps the same practice directory sends you two lines:
wc -L todo.txtwc -l Documents“The first one counts the lines in your to-do list,” they write. “The second one does the same for every file in Documents.”
You have never used wc. One of these lines runs without any error and still gives you the wrong answer. The other one fails. This practice puts all four lessons of the module to work on that situation: a program you do not know, commands from someone else, and only your own system to check them against.
Before you start
Section titled “Before you start”- The four lessons of this module, from Command anatomy (B04.1) to Reading errors and identifying commands (B04.4).
- The
~/labpractice directory from lesson B04.1 (setup). If you opened another terminal, runcd ~/labfirst.
Every command in this practice only reads files. Nothing in ~/lab changes.
Your task
Section titled “Your task”Work in ~/lab. Reach these four goals, in order:
- You know what kind of command
wcis, and so where its documentation is. - Before you trust either line, you can name the command name, the option, and the argument in each one, and say what each option does according to the manual on your system.
- You know how many lines
todo.txthas, and what the number from the colleague’s first line means. - You have the number of lines of each file in
Documents, from one command.
For goals 3 and 4, start from the colleague’s lines: run each one, then bring it back with Up and change only what is wrong. Do not type a line again, and do not type a file name in full.
Expected evidence
Section titled “Expected evidence”When you are done, your terminal shows:
- A command that tells you which file runs when you type
wc. - The number
3next totodo.txt, from a line that differs from the colleague’s first line by one character. - One line for
Documents/notes.txt, one forDocuments/report.txt, each with its number of lines, and a last line markedtotal.
You can also say in one sentence what the colleague’s first line printed instead of the number of lines.
Open a hint only when you are stuck on that goal.
Hint for goal 1
Lesson B04.4 has a command that answers “what kind of command is this name?”. A builtin is described by help. A program in its own file usually has a manual page.
Hint for goal 2
Open man wc. The page is short, and the options are under DESCRIPTION. Compare the options in the two lines with the ones on the page, letter by letter. A newline is the invisible character at the end of each line in a file, so counting newlines counts lines.
Hint for goal 3
The number the first line prints is correct. It answers a different question. To change one character in the middle of a recalled line, Ctrl+A takes the cursor to the start, and Right moves it one character at a time.
Hint for goal 4
Read the message as in lesson B04.4: who is speaking, which word it names, and what went wrong. A program can take more than one argument. After Up, the cursor is at the end of Documents, so you can go on typing the path from there.
Solution
Section titled “Solution”Solution
Goal 1: what kind of command wc is
type wcwc is /usr/bin/wcwc is an external program: the file wc in the directory /usr/bin. So its documentation is a manual page, man wc, not help.
If you ran one of the colleague’s lines first, type gives a slightly different answer:
wc is hashed (/usr/bin/wc)As with head in lesson B04.4, hashed means that Bash remembers where it found the program the last time it ran. The file is the same.
Goal 2: read both lines before you trust them
| Line | Command name | Option | Argument |
|---|---|---|---|
wc -L todo.txt |
wc |
-L |
todo.txt |
wc -l Documents |
wc |
-l |
Documents |
The two options differ only in case, so they are two different options, as -a and -A are for ls. Open man wc and look under DESCRIPTION. Both are there:
-l, --lines print the newline counts… -L, --max-line-length print the maximum display width-l prints the number of newlines, which is the number of lines. -L prints something else: the width of the longest line, in characters. So the second line uses the right option, and the first line does not count lines at all.
The same letter can also mean something different in another program: ls -l is the long listing format. Each program chooses its own options.
Goal 3: the lines in todo.txt
Run the colleague’s first line and look at what it prints:
wc -L todo.txt23 todo.txtThere is no error, and 23 looks like a count. But todo.txt has three lines. 23 is the number of characters in its longest line, Verify the ISO checksum. A command that runs without an error can still answer a different question. Only the manual tells you which question it answers.
Now change the one wrong character without typing the line again:
| You press or type | The line becomes |
|---|---|
| Up, until the line appears | wc -L todo.txt |
| Ctrl+A | wc -L todo.txt, with the cursor before w |
| Right, five times | wc -L todo.txt, with the cursor right after L |
| Backspace | wc - todo.txt |
l |
wc -l todo.txt |
| Enter | runs wc -l todo.txt |
3 todo.txtThree lines, with the file name after the number.
Goal 4: the lines of each file in Documents
Run the colleague’s second line:
wc -l Documentswc: Documents: Is a directory0 DocumentsRead the first line in parts, as in lesson B04.4. It starts with wc:, so wc ran: the command name is fine, and the message says nothing about the option. The word it names is Documents, and the end says what went wrong: Documents is a directory. wc counts the lines of files, so it had nothing to count. The 0 Documents line below is not a result for that reason.
The smallest fix is to give wc the two files instead of their directory. A program can take several arguments, as mkdir did when it created lab/Documents and lab/projects in lesson B04.1. Bring the line back and extend the path with Tab:
| You press or type | The line becomes |
|---|---|
| Up | wc -l Documents |
/n, then Tab |
wc -l Documents/notes.txt |
Doc, then Tab |
wc -l Documents/notes.txt Documents/ |
r, then Tab |
wc -l Documents/notes.txt Documents/report.txt |
| Enter | runs the line |
2 Documents/notes.txt 1 Documents/report.txt 3 totalOne line per file, then a total line. The first sentence under DESCRIPTION in man wc says that wc adds this line when it gets more than one file. wc puts the numbers in a column, so they can start with a space.
Your answer to the colleague: todo.txt has 3 lines, notes.txt has 2, and report.txt has 1. Their first line needs a lowercase -l; with -L it printed the width of the longest line. Their second line needs the file names instead of the directory.
Recall check
Section titled “Recall check”Answer each question without running anything, then open the answer.
1. In wc -w -l Documents/notes.txt, which words are options, and which word is the argument? Write the two options in one word.
Answer
-w and -l are options; Documents/notes.txt is the argument. Short options can share one dash: wc -wl Documents/notes.txt. (man wc describes -w: “print the word counts”.)
2. You type the start of a file name and press Tab twice. Nothing is added, and no list appears. What does that tell you?
Answer
No name in that place starts with what you typed. Completion only offers names that exist, and it matches capital letters exactly, so check the spelling and the case of what you typed so far.
3. You press Up and see a command that you do not want to run again. How do you get an empty prompt without running it?
Answer
Press Ctrl+C: Bash discards the line and shows a new prompt. Down, pressed until you are past the newest command, also gives you an empty line. Do not press Enter: Enter runs the whole line.
4. A guide refers to the page crontab(5). What does the 5 tell you, and how do you open that page?
Answer
The 5 is the manual section: section 5 describes file formats, so the page explains a file, not a program you run. Open it with man 5 crontab. man -f crontab lists every section that has a page with that name on your system.
5. Here are two messages. The first takes two lines. Which word do you change in each command?
Command 'wcc' not found, but can be installed with:sudo apt install wccwc: todo: No such file or directoryAnswer
The first names 'wcc' and says not found: no program ran, so the command name wcc is wrong. Change it to wc. Do not install the package it suggests: wcc is a different program that happens to have the name you typed. The second starts with wc:, so the program ran, and it names the path todo, which does not exist. Change it to todo.txt.
6. type cd says cd is a shell builtin. Where do you find its documentation, and why does man cd not find it?
Answer
Run help cd. A builtin is part of Bash itself, not a separate program, and the lab system has no manual page for cd. man reports No manual entry for cd.
- A command that runs without an error can still answer a different question. Look up each option in your own manual before you trust it.
typetells you where the documentation is:manfor a program in its own file,helpfor a builtin.- An error message names one word: the command name, an option, or a path. Change that word and keep the rest of the line.
- Up, Ctrl+A, the arrow keys, and Tab let you correct a line instead of typing it again.
Next, module B05 explains paths such as Documents/notes.txt and ~/lab, and how to move between directories. It starts with Root, home, and the current directory.
Further reading
Section titled “Further reading”man wcon your system, or the Ubuntu 24.04 wc(1) page online.- GNU Coreutils manual: wc invocation, the longer description of
wcand its options. - GNU Bash Reference Manual: Command Search and Execution, including how Bash remembers where it found a program (the hash table).