users.txt contains one user name per line.
Count the number of lines in the file.
Which command is correct?
wc -l counts newline characters. For a conventional one-record-per-line file, this gives the line count.
Detailed explanation
wc -w users.txtIncorrect. -w counts words rather than lines.
Incorrect. -w counts words rather than lines.
wc -c users.txtIncorrect. -c counts bytes rather than lines.
Incorrect. -c counts bytes rather than lines.
wc -l users.txtCorrect. -l reports the number of newline characters.
Correct. -l reports the number of newline characters.
nl users.txtIncorrect. nl numbers lines but does not return only the count.
Incorrect. nl numbers lines but does not return only the count.
Try it yourself
An example you can run in a temporary verification environment.
printf 'alice
bob
carol
' | wc -lExpected result
3(前の空白幅は環境により異なる)Key points
- wc aggregates input
- -l counts lines
- -w and -c count words and bytes
Notes
- Environment: GNU coreutils 9.x
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.