Learn / Linux Fundamentals / Shell Basics
Reading Files
First Bit is still up for grabs: be the first to finish this room.
Most of what happens on a Linux system gets written down somewhere: configuration files, logs, reports. Being able to read them quickly is half of system administration and most of security investigation.
This room covers the tools for that: cat and less for whole files, head, tail and wc for the edges and the size, and grep for finding the one line that matters among fifty thousand. The last task gives you a real machine with a very long log to practise on.
CYB//RANGE Live lab
Log in to launch this room's lab.
Task 1 Reading a whole file
cat prints a whole file to the screen:
$ cat notes.txt
Remember: the archive has one folder per month.
That's perfect for short files and useless for long ones: a 50,000-line log flies past faster than you can read. For those, use less, a pager that shows one screen at a time:
$ less app.log
Inside less:
| Key | Does |
|---|---|
| Space / Page Down | next screen |
b / Page Up |
back a screen |
g / G |
jump to the start / the end |
/word then Enter |
search forwards for "word" (n for the next match) |
q |
quit |
less never changes the file, so it's safe to open anything with it.
Task 2 The start, the end, and the size
Often you only care about the start or the end of a file.
head shows the first 10 lines, and tail the last 10. Use -n to choose how many:
$ head -n 3 app.log
$ tail -n 1 app.log
Logs grow at the bottom, so tail is the one you'll use most. With -f ("follow") it keeps running and prints each new line as it's written, which makes it ideal for watching a service while you test it. Press Ctrl+C to stop.
$ tail -f /var/log/syslog
wc ("word count") tells you how big a file is. With -l it prints only the number of lines:
$ wc -l app.log
50000 app.log
Task 3 Finding lines with grep
grep prints only the lines that contain a word or pattern:
$ grep "database timeout" app.log
2026-09-21 00:14:52 ERROR db database timeout
...
The options you'll use every day:
| Option | Does |
|---|---|
-i |
ignore case (error matches ERROR) |
-c |
print just the number of matching lines |
-v |
invert: show the lines that don't match |
-n |
show each match's line number |
-r |
search every file in a folder, and the folders inside it |
Combine them freely: grep -ci warn app.log counts the lines containing "warn" in any case, and grep -rn password /etc finds every mention of "password" under /etc along with its file and line number.
Put the search text in quotes when it contains spaces or symbols the shell might interpret, like {, * or $.
Which grep option prints only the number of matching lines? (Short or long form.)
Log in or sign up to answer. 10 pts
**
Task 4 Try it on a real machine
Cara looks after a busy application. Its log for 21 September is in her home folder, along with the logs of 300 web servers.
- Press Launch in CybRange at the top of this page, then Open terminal next to
boxonce it's ready. You're logged in ascara. - Don't
cat ~/app.log. It's 50,000 lines long. Trywc -l,head,tailandlesson it instead. - Use
grepto count the errors, then to search all of~/serversat once for the flag.
At what time was the first entry in ~/app.log written? (HH:MM:SS)
Log in or sign up to answer. 10 pts
**:**:**