Linux keeps almost everything in plain text files: settings, user lists, logs. This lesson shows how to read any of them safely: print a small file, measure a big one, look at its start or its end, scroll through it, search inside it, and chain commands with the pipe. Every command here only reads, so nothing on the system changes.
Part of the Linux for Beginners series. This article is the written version of lesson 2 of the Bigland Network video course. All outputs were captured on AlmaLinux 9.8 as a normal user. Your own files and numbers may differ, so the examples use
your-userwhere your account name would appear.
Contents
- 1. cat: print a whole file
- 2. wc: measure before you read
- 3. head and tail: the start and the end
- 4. less: scroll a long file
- 5. grep: search inside files
- 6. The pipe: combine commands
- 7. Common mistakes
- 8. Practice
- 9. Cheat sheet
1. cat: print a whole file
cat prints the full content of a file. It is perfect for small files. Most system settings live in /etc.
$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Add -n to number the lines:
$ cat -n /etc/shells
1 /bin/sh
2 /bin/bash
3 /usr/bin/sh
4 /usr/bin/bash
2. wc: measure before you read
Before you cat a file, check how big it is. wc -l counts the lines:
$ wc -l /etc/services
11473 /etc/services
Eleven thousand lines would flood your screen. Without options, wc prints lines, words and bytes; with several files it adds a total:
$ wc /etc/shells
4 4 44 /etc/shells
$ wc -l /etc/hosts /etc/shells /etc/os-release
2 /etc/hosts
4 /etc/shells
19 /etc/os-release
25 total
3. head and tail: the start and the end
head shows the first lines of a file (10 by default), tail the last ones. Choose the number with -n.
$ head -n 5 /etc/os-release
NAME="AlmaLinux"
VERSION="9.8 (Olive Jaguar)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.8"
$ tail -n 3 /etc/os-release
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.8"
SUPPORT_END=2032-06-01
$ tail -n 4 /etc/services
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures
Logs grow at the end, so tail is the command for logs. tail -f file (f for follow) keeps the file open and prints new lines as they arrive; stop it with Ctrl+C. A good habit: run tail -f on the log in a second terminal, then repeat the action that fails.
$ head -n 2 /var/log/dnf.log
2026-11-12T14:13:51+0100 INFO --- logging initialized ---
2026-11-12T14:13:51+0100 DDEBUG timer: config: 9 ms
4. less: scroll a long file
less file opens a viewer that shows one screen at a time and never changes the file.
| Key | What it does |
|---|---|
| Space / b | One page down / one page back up |
| G / g | Jump to the end / to the start |
| /word, then n | Search forward, then the next match |
| q | Quit |
5. grep: search inside files
grep prints every line that contains a pattern. Put the pattern in single quotes; ^ means at the start of the line.
$ grep '^ssh ' /etc/services
ssh 22/tcp # The Secure Shell (SSH) Protocol
ssh 22/udp # The Secure Shell (SSH) Protocol
ssh 22/sctp # SSH
| Option | What it does |
|---|---|
-c |
Count the matching lines |
-n |
Show the line numbers |
-i |
Ignore upper and lower case |
-v |
Invert: show the lines that do NOT match |
-l |
Only list the names of the files that match |
-w |
Match whole words only |
$ grep -c ssh /etc/services
17
$ grep -n '^ftp ' /etc/services
42:ftp 21/tcp
43:ftp 21/udp fsp fspd
592:ftp 21/sctp # FTP
$ grep -i 'SECURE SHELL' /etc/services
ssh 22/tcp # The Secure Shell (SSH) Protocol
ssh 22/udp # The Secure Shell (SSH) Protocol
$ grep -l AlmaLinux /etc/os-release /etc/hosts /etc/shells
/etc/os-release
$ grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
your-user:x:1000:1000::/home/your-user:/bin/bash
grep is case sensitive:
$ grep -c Ssh /etc/services
0
$ grep -ci Ssh /etc/services
17
-v '^#' hides the comment lines, so only the real settings remain:
$ grep -v '^#' /etc/fstab
/dev/mapper/almalinux-root / xfs defaults 0 0
UUID=ab21f9ec-a2ba-450c-ac57-113ef4859c1c /boot xfs defaults 0 0
/dev/mapper/almalinux-swap none swap defaults 0 0
(The real output starts with one empty line, the blank line of the file, left out here.)
6. The pipe: combine commands
The pipe | sends the output of one command into the next one. Small commands become powerful together.
$ grep -w ssh /etc/services | head -n 3
ssh 22/tcp # The Secure Shell (SSH) Protocol
ssh 22/udp # The Secure Shell (SSH) Protocol
x11-ssh-offset 6010/tcp # SSH X11 forwarding offset
$ grep -w ssh /etc/services | wc -l
13
$ ls /etc | wc -l
164
$ cat /etc/os-release | grep VERSION
VERSION="9.8 (Olive Jaguar)"
VERSION_ID="9.8"
ALMALINUX_MANTISBT_PROJECT_VERSION="9.8"
REDHAT_SUPPORT_PRODUCT_VERSION="9.8"
$ head -n 20 /etc/services | tail -n 3
#
# Each line describes one service, and is of the form:
#
The last one prints lines 18 to 20: head keeps the first 20, then tail keeps the last 3 of those.
7. Common mistakes
- Reading a protected file. Some files are for root only:
$ cat /etc/shadow cat: /etc/shadow: Permission denied - A wrong path. Check the spelling and the leading slash:
$ cat /etc/missing.txt cat: /etc/missing.txt: No such file or directory - Case sensitivity.
Sshis notssh; add-iwhen you are not sure of the spelling. - Missing quotes. Put grep patterns in single quotes, especially with spaces or special characters.
- Using cat on a huge file. Measure with
wc -lfirst, then usehead,tail,greporless. - Empty output is not always an error. Sometimes grep simply found nothing; check with
-c.
8. Practice
- Count the lines of
/etc/hosts. - Show the first two lines of
/etc/os-release. - Count how many lines in
/etc/servicescontain the word telnet, ignoring case. - List the entries in
/etcwhose name containsconf, using a pipe.
Answers: (1) wc -l /etc/hosts prints 2. (2) head -n 2 /etc/os-release prints the NAME and VERSION lines. (3) grep -ci telnet /etc/services; the number depends on your system. (4) ls /etc | grep conf.
9. Cheat sheet
| Command | Use it to |
|---|---|
cat file / cat -n file |
Print a small file / with line numbers |
wc -l file |
Count the lines first |
head -n 5 file |
The first lines |
tail -n 5 file / tail -f file |
The last lines / follow a live log |
less file |
Scroll a long file (q to quit) |
grep 'word' file |
Lines that contain a word (-c -n -i -v -l -w) |
cmd1 | cmd2 |
Send the output of one command into another |
Previous lesson: Where am I? whoami, pwd, ls, cd, uname and cat. Next lesson: Find any file with find, which, file, stat, du and df.
More tutorials like this
Linux Permission Denied Error: Causes and How to Fix It (chmod, namei, id)
Fix the Linux Permission denied error: read ls -l, namei -l and id, then add only the permission letter you need with chmod. Real…
systemd Basics: Manage Services and Read Logs with systemctl and journalctl
Start, stop and enable services with systemctl, read logs with journalctl, write your own service and timer, and troubleshoot a service that will not…
Bash Scripting for Beginners: Variables, Loops, Functions and Exit Codes
Learn Bash scripting from your first script to a safe backup script: variables, quoting, loops, functions, exit codes and set -euo pipefail, with real…


