Skip to content
Laviyx Learn

Try

Linux and Bash course menu

Reading Files in Linux: cat, less, head, tail and wc

View files in the terminal with cat, less, head and tail, follow logs live with tail -f, count lines with wc, compare files with diff, and identify file types.

Beginner5 min readUpdated
On this page

We use a small log file, app.log, in this lesson.

cat#

cat prints a whole file. Use it for short files:

Terminal
cat app.log
Output
2026-11-05 09:00 INFO Server started
2026-11-05 09:01 INFO User asha logged in
2026-11-05 09:02 WARN Disk 80 percent full
2026-11-05 09:03 INFO User ravi logged in
2026-11-05 09:04 ERROR Payment failed
2026-11-05 09:05 INFO Report created
2026-11-05 09:06 ERROR Database timeout
2026-11-05 09:07 INFO User asha logged out

cat -n adds line numbers:

Terminal
cat -n app.log | head -n 2
Output
     1	2026-11-05 09:00 INFO Server started
     2	2026-11-05 09:01 INFO User asha logged in

less#

For long files, use less. It lets you scroll without loading everything at once:

Terminal
less /var/log/syslog
KeyAction
Space, bForward, back a page
g, GStart, end of the file
/wordSearch forward
nNext match
qQuit

head and tail#

head shows the first lines, and tail the last. Both default to 10 lines:

Terminal
head -n 2 app.log
echo ---
tail -n 2 app.log
Output
2026-11-05 09:00 INFO Server started
2026-11-05 09:01 INFO User asha logged in
---
2026-11-05 09:06 ERROR Database timeout
2026-11-05 09:07 INFO User asha logged out

tail -n +7 starts from line 7 to the end, which is useful for skipping a header:

Terminal
tail -n +7 app.log
Output
2026-11-05 09:06 ERROR Database timeout
2026-11-05 09:07 INFO User asha logged out

Follow a log live#

tail -f keeps running and prints new lines as they are added. It is one of the most useful commands on a server:

Terminal
tail -f app.log

Press Ctrl+C to stop. Here we run it for one second, while another command adds a line:

Terminal
(sleep 0.3; echo "2026-11-05 09:08 INFO New line" >> app.log) &
timeout 1 tail -f -n 1 app.log
Output
2026-11-05 09:07 INFO User asha logged out
2026-11-05 09:08 INFO New line

Count with wc#

wc counts lines, words and bytes:

Terminal
wc app.log
wc -l app.log
Output
  9  53 353 app.log
9 app.log

Compare files#

diff shows the differences between two files:

Terminal
echo -e "apple\nbanana\ncherry" > a.txt
echo -e "apple\nblueberry\ncherry" > b.txt
diff a.txt b.txt
Output
2c2
< banana
---
> blueberry

2c2 means that line 2 changed. < is from the first file, and > from the second. diff -u gives the format used by Git.

What kind of file is it?#

File names do not need extensions. file looks at the content:

Terminal
file app.log /bin/ls /etc
Output
app.log: ASCII text
/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=44900829b39271d878615772bb2a6ffa422f0891, for GNU/Linux 3.2.0, stripped
/etc:    directory

Common mistakes#

Practice#

Practice exercise

Print lines 3 and 4 of app.log by combining head and tail.

Show hint

Take the first 4 lines, then the last 2 of those.

Show solution
Terminal
head -n 4 app.log | tail -n 2
Output
2026-11-05 09:02 WARN Disk 80 percent full
2026-11-05 09:03 INFO User ravi logged in

Key takeaway#

Next: Pipes and Redirection.

Finished this lesson?