Arguments tell a program what to work on. Some arguments tell it how to do the work. These are flags. A flag starts with a dash, -.
wc: count the lines and words in a file
wc counts what is in a file. With no flag, it writes three numbers:
learner@tidepool:~$ wc notes.txt
4 8 49 notes.txtA byte is a unit of size. In a text file, each letter, space and line end is one byte.
Add a flag
With a flag, wc writes only what you ask for:
learner@tidepool:~$ wc -l notes.txt
4 notes.txt
learner@tidepool:~$ wc -w notes.txt
8 notes.txtShort and long flags
Many flags have two forms. A short flag has one dash and one letter. A long flag has two dashes and a word. The two forms do the same thing.
| Short | Long | wc counts |
|---|---|---|
-l | --lines | Lines |
-w | --words | Words |
-c | --bytes | Bytes |
Short flags are fast to type. Long flags are easier to read.
Put short flags together
Short flags can share one dash. These two lines do the same thing:
wc -l -w notes.txt
wc -lw notes.txtA dash makes the flag
Only an argument that starts with - is a flag. Without the dash, wc reads lw as the name of a file:
learner@tidepool:~$ wc lw
wc: lw: No such file or directoryEach program has its own flags
cat -n puts a number before each line:
learner@tidepool:~$ cat -n notes.txt
1 deploy started
2 build ok
3 tests ok
4 deploy finishedThe letter n means "number the lines" for cat. In another program, -n can mean something different. The next lesson shows one.