Q1Which command shows a file's contents from start to end all at once?
Viewing Contents — cat / head / tail / wc
Show the whole file with cat, the start and end with head and tail, and count lines with wc -l — plus the log-checking flow, hands-on in a browser terminal.
Show the Whole File — cat
cat is the command that prints a file's entire contents to the screen.
Use it when you want to see the whole thing at a glance, like a short config file or memo.
Just pass the filename and it outputs from the first line to the last as-is.
For very long files, on Ubuntu and similar you'd use less to scroll one screen at a time and q to quit.
In this course's console you display the whole file with cat, and for long files you take just the part you need with the head and tail you'll learn next.
cat for the whole, head / tail for parts, and wc -l for the amount.echo one > memo.txt # write line 1
echo two >> memo.txt # append line 2
echo three >> memo.txt # append line 3
cat memo.txt # show every line
View the Start — head
head takes just the given number of lines from the start of a file.
With no option it shows the first 10 lines, and writing a number after -n limits it to that many.
It's handy for checking the beginning of a long log or the first few records of data.
| Usage | Meaning |
|---|---|
head f | Show the first 10 lines (default) |
head -n N f | Show the first N lines |
head shows from the start. Use -n to set the number of lines.echo l1 > log.txt # create material
echo l2 >> log.txt
echo l3 >> log.txt
head -n 2 log.txt # first 2 lines (l1 l2)
View the End — tail
tail takes just the given number of lines from the end of a file.
With no option it shows the last 10 lines, and -n sets the number of lines.
It's often used to check the latest lines of a log.
| Usage | Meaning |
|---|---|
tail f | Show the last 10 lines (default) |
tail -n N f | Show the last N lines |
tail shows from the end. It's good for checking the latest lines.echo l1 > log.txt # create material
echo l2 >> log.txt
echo l3 >> log.txt
tail -n 2 log.txt # last 2 lines (l2 l3)
Count Lines — wc
wc is the command that counts words and lines.
Add -l to show just the line count of a file.
You can quickly see how many lines a log has piled up or how many records there are without reading the whole thing.
| Usage | Meaning |
|---|---|
wc f | Show line, word, and character counts |
wc -l f | Show just the line count |
wc -w f | Show just the word count |
wc counts. Use -l to take just the line count.echo a > data.txt # create material
echo b >> data.txt
echo c >> data.txt
wc -l data.txt # line count (3)
Specify the line count with `-n`
When you know how many lines you want with head or tail, specifying it with -n takes exactly the part you want.
Knowledge Check
Answer each question one by one.
Q2Which do you use to show just the last few lines of a file?
Q3What does running wc -l data.txt tell you?