Command anatomy
On this page
Since module B02, you have typed commands exactly as shown: ls ~, whoami, sudo ls /root, sleep 300. Each time, the lesson said that module B04 explains how commands are built. This module keeps that promise: it teaches you to read a command before you run it. You start with seven more lines, which build a place to practice. By the end of this lesson, you can take them apart word by word: which program each line runs, how it is told to behave, and what it works on.
Before you start
Section titled “Before you start”You need a terminal with a Bash prompt, as in module B03, Your First Linux Session, which ends with Practice: Your First Linux Session.
Build your practice directory
Section titled “Build your practice directory”The examples in this course use a directory called lab in your home directory. A directory is what a graphical file manager calls a folder; this course uses the word directory.
Run these lines one at a time: type or paste each line, then press Enter.
cd ~mkdir -p lab/Documents lab/projectsprintf 'Buy printer paper\nRenew lab VM snapshot\n' > lab/Documents/notes.txtprintf 'Quarterly lab report\n' > lab/Documents/report.txtprintf 'Install Ubuntu\nVerify the ISO checksum\nTake a snapshot\n' > lab/todo.txtprintf 'Lab checklist (draft)\n' > lab/.draft.txtcd ~/labYour prompt now ends in ~/lab$. Every example in this lesson runs there. The examples show the course’s lab account, learner@rootvolt; your prompt shows your own user and computer name.
You just ran seven more commands without knowing what they do. Typing exactly what a lesson shows was the right way to start, and this lesson replaces it with a better habit. A line you cannot read could create files, or delete them, and from the outside both look the same: a few short words. So before you press Enter on any command, you should be able to say what it will do.
One fact to keep in mind: you created four things inside lab. Two directories, todo.txt, and .draft.txt.
Start with one word
Section titled “Start with one word”You have used ls before: ls ~ in lesson B02.4 and ls /root in lesson B03.3. Now take it apart. Type it with nothing after it, and press Enter:
lsDocuments projects todo.txtls lists what is in a directory. With nothing after it, it lists the directory you are in.
That word is the command name: the name of the program to run. Every command line starts with one. Bash reads the line, splits it into words wherever there is a space, and starts the program named by the first word.
Look at the output again. You created four things, but ls shows three. The next section explains why.
Change how the program behaves
Section titled “Change how the program behaves”Add a second word, separated by a space. Before you run it, make a guess: will ls -l show more files, or more about the same files?
ls -lRun it, then compare with your guess
total 12drwxr-xr-x 2 learner learner 4096 Oct 1 09:00 Documentsdrwxr-xr-x 2 learner learner 4096 Oct 1 09:00 projects-rw-r--r-- 1 learner learner 55 Oct 1 09:00 todo.txtThe same three names, with more about each one: one line per entry, with the size and the date before the name. Module B11 explains the first columns.
-l is an option. An option changes how the program works, not what it works on. Without options, a program uses its defaults. Some documentation calls options flags or switches; this course uses option.
Notice that ls decides what -l means, not Bash. Bash only passes the words on. So each program chooses its own options, and the same letter can mean something different to another program. Lesson B04.3 shows where each program documents its options.
The missing file
Section titled “The missing file”Here is the fourth file. Names that begin with a dot, such as .draft.txt, are hidden: ls leaves them out unless you ask for them. The option that asks is -a, for “all”:
ls -a. .. Documents .draft.txt projects todo.txtThere it is. The two extra entries, . and .., stand for the current directory and the one above it; lesson B05.1 explains them.
The same option has a second spelling:
ls --all. .. Documents .draft.txt projects todo.txtThe output is identical, because -a and --all are the same option written two ways:
- A short option is one dash and one letter:
-a. - A long option is two dashes and a word:
--all.
Many options have both forms. The short form is quicker to type. The long form is easier to read when you come back to a command later, or when you send it to someone else.
Capital letters count
Section titled “Capital letters count”Now try the capital letter. Guess first: will ls -A print the same line as ls -a?
ls -ARun it, then compare with your guess
Documents .draft.txt projects todo.txtNot the same. -A (long form --almost-all) shows hidden entries but leaves out . and ...
-a and -A are two different options. In Linux, a capital letter is a different character: in options, in command names, and in file names.
Several options at once
Section titled “Several options at once”You can give a program more than one option. Short options can even share one dash: ls -la means the same as ls -l -a. Both print:
total 24drwxr-xr-x 4 learner learner 4096 Oct 1 09:00 .drwxr-x--- 1 learner learner 4096 Nov 5 08:54 ..drwxr-xr-x 2 learner learner 4096 Oct 1 09:00 Documents-rw-r--r-- 1 learner learner 22 Oct 1 09:00 .draft.txtdrwxr-xr-x 2 learner learner 4096 Oct 1 09:00 projects-rw-r--r-- 1 learner learner 55 Oct 1 09:00 todo.txtLong options cannot share dashes. Each long option is its own word, so ls -l --all needs a space between the two options.
Options that need a value
Section titled “Options that need a value”Some options are not complete on their own. They need a value: a number, a word, or a name.
The command head prints the first lines of a file, ten by default. Its -n option says how many lines, so it needs a number:
head -n 2 todo.txtInstall UbuntuVerify the ISO checksumThe 2 belongs to -n. It is not something head works on; it is part of the option.
A value can be written in three ways, and all three print the same two lines:
| You type | How the value is attached |
|---|---|
head -n 2 todo.txt |
Short option, value as the next word |
head -n2 todo.txt |
Short option, value right after the letter |
head --lines=2 todo.txt |
Long option, value after = |
Tell the program what to work on
Section titled “Tell the program what to work on”The last kind of word is the argument. Arguments tell the program what to work on, such as a file or a directory. For ls, an argument names the directory to list:
ls Documentsnotes.txt report.txtYou already used an argument in the previous section: in head -n 2 todo.txt, the argument is todo.txt.
Mistakes to recognize
Section titled “Mistakes to recognize”Everyone makes these at first. Each one breaks a rule from this lesson, and each one has a message that tells you where to look.
A missing option value. -n always takes the next word as its value, even when that word is a file name:
head -n todo.txthead: invalid number of lines: ‘todo.txt’The message says it plainly: head tried to read todo.txt as a number of lines. Put the value back: head -n 2 todo.txt.
A capital letter in the command name. The program is called ls, so LS does not exist:
LSLS: command not foundLS here is not a program speaking: it is the name you typed, and command not found says that no program has that name. So nothing ran. When Ubuntu Desktop finds commands with similar names, it adds a list of them to this message; lesson B04.4 explains it. On a system without that feature, Bash prints the message itself and starts it with bash:, as in bash: LS: command not found.
A capital letter in a name. Documents exists; documents does not:
ls documentsls: cannot access 'documents': No such file or directoryThis time the message starts with ls:, and it does not say command not found. So ls ran, but it could not find the argument you gave it.
A missing space. Without the space, Bash sees one word, ls-l, and looks for a program with that name:
ls-lls-l: command not foundLesson B04.4 practices reading errors like these.
Read a command someone sends you
Section titled “Read a command someone sends you”Commands often come from someone else: a colleague, a forum answer, a guide. Here is one:
ls -l --all /etcYou can read it now:
ls is the command name: list a directory. -l and --all are options: use the long format, and include hidden entries. /etc is the argument: the directory to list. /etc holds the system’s settings files.
So the command means “list everything in /etc, including hidden entries, with details”. It only reads; nothing on your system changes. Its output is long. Here are its first and last lines:
total 564drwxr-xr-x 1 root root 4096 Nov 5 08:54 .drwxr-xr-x 1 root root 4096 Nov 5 08:54 ..-rw-r--r-- 1 root root 3444 Jul 5 2023 adduser.conf…-rw-r--r-- 1 root root 460 Jan 20 2023 zsh_command_not_foundYour list will be different. The entries in /etc depend on what is installed, and the dates depend on when it was installed and changed.
Back to your seven lines
Section titled “Back to your seven lines”Now return to the lines that built your practice directory. Take this one:
mkdir -p lab/Documents lab/projectsmkdiris the command name: make directories.-pis an option: also create the directories above when they do not exist yet.labdid not exist, somkdircreated it first.lab/Documentsandlab/projectsare two arguments: the two directories to create.lab/Documentsmeans the directoryDocumentsinsidelab.
The first and last lines follow the same pattern. cd is a command name, and ~ or ~/lab is its argument: the directory to move into. Module B05 explains cd and ~.
The printf lines start the same way: printf is the command name, and the text in quotes is its argument. Two pieces in those lines are new. The quotes keep the text together as one word, spaces included; module B08 explains quoting. The > and the file name after it are not arguments to printf at all: they tell Bash to save the output in that file. Module B09 explains that.
Good to know
Section titled “Good to know”Option order. The GNU tools that Ubuntu ships, such as ls and head, also accept options after arguments:
ls Documents -ltotal 8-rw-r--r-- 1 learner learner 40 Oct 1 09:00 notes.txt-rw-r--r-- 1 learner learner 21 Oct 1 09:00 report.txtNot every program allows this. Put options before arguments, as in ls -l Documents, and the command works with any program.
Values that differ on your system. The dates on your files are the time you created them, not Oct 1 09:00, and the .. line shows when your home directory last changed. The names and the layout of the output match what this lesson shows.
Try it yourself
Section titled “Try it yourself”Goal: Take apart a command you have not seen yet, then write it in a different form.
- Look at
head --lines=1 Documents/notes.txt. Name the command name, the option, the option value, and the argument. - Run the command.
- Write the same command with the short form of the option, run it, and compare.
Expected: Both commands print Buy printer paper.
Hint
A long option and its value are joined by =. The short form of --lines is in the table under “Options that need a value”.
Solution
- Command name:
head - Option:
--lines, with the value1 - Argument:
Documents/notes.txt
The short form is -n, with the value as the next word:
head -n 1 Documents/notes.txtBuy printer paperhead -n1 Documents/notes.txt works as well.
- Bash splits a command line into words at the spaces. The first word is the command name: the program to run.
- Options change how the program works. A short option is one dash and one letter (
-l). A long option is two dashes and a word (--all). Some options take a value:-n 2or--lines=2. - Arguments tell the program what to work on, such as a file or a directory.
- Capital letters matter:
-aand-Aare different options, andDocumentsanddocumentsare different names. - In an error, read who is speaking, which word it names, and what went wrong.
command not foundmeans no program has the name you typed, so nothing ran. An error that starts with the program’s name, such asls:, comes from that program.
Next, in Editing and completing commands, you let Bash finish names for you and fix a command before you run it.
Further reading
Section titled “Further reading”man ls,man head, andman mkdiron your system, or the Ubuntu 24.04 manual pages online: ls(1), head(1), mkdir(1).- GNU Bash Reference Manual: Simple Commands, how Bash splits a line into a command name and its arguments.
- GNU Coreutils manual: Common options, including the rule that options and operands can appear in any order.
- POSIX: Utility Conventions, the standard syntax for options, option values, and operands (another word for arguments).