Ravindra BagaleCourses & study guides मराठी Track your progress

Linux cat, less, head, tail, wc and tee Commands with Examples (View and Follow Files)

Most of your Linux day is reading text: logs, configs, CSV files and command output. cat, less, head, tail, wc and tee are the six tools you use to look at text, follow it live, count it and save it while you watch. Learn these well and every later tool (grep, sed, awk) becomes easy.

What you'll learn in this chapter

  • Print and join files with cat, tac and nl
  • Read big files safely with less
  • Take the start or end with head and tail
  • Follow live logs with tail -f and -F
  • Count lines, words and bytes with wc
  • Save and show output together with tee

1. An everyday example: reading a long WhatsApp chat

Why do we need this?

Sunita exports her society WhatsApp group chat to a text file. It has 40,000 lines. She does not read it all. She looks at the first few messages to see when the group started, jumps to the last messages to see today's news, scrolls slowly through the middle, counts how many messages there are, and keeps watching for new ones. She also forwards the news to her husband while reading it herself.

What is it?

First messages = head. Last messages = tail. Slow scrolling = less. Counting = wc. Watching for new ones = tail -f. Reading and forwarding at the same time = tee. Printing the whole chat = cat.

How to do it (step by step)

How text flows through a pipeA file or a command makes textcat or tail sends it to the screen (stdout)A pipe passes it to the next toolhead, wc or less shapes ittee saves a copy and passes it onYou read the result
Each tool does one small job. Pipes join them.

2. cat: print, join and create files

Why do we need this?

You often need to see a short file fully, or join parts into one file.

What is it?

cat (concatenate) prints files one after another. cat a b > c joins files, >> appends. Options: -n number all lines, -b number non-empty lines, -A show tabs (^I), line ends ($) and Windows CR (^M), -s squeeze repeated empty lines. cat > file <<'END' writes a quick file. Use cat only for short files; for long ones use less.

How to do it (step by step)

  1. Print a small file.
cat /etc/hostname

Output:

web-pune-01

What it means: The machine's name is in this file.

  1. Number the lines.
cat -n shopping.txt

Output:

     1	milk
     2	bread
     3	
     4	eggs

What it means: Line 3 is empty but still numbered.

  1. Show hidden characters.
cat -A config.yml

Output:

name: shop$
port:^I8080$
mode: prod ^M$

What it means: ^I is a tab, ^M is a Windows line end. Both often break YAML and scripts.

  1. Join parts into one file.
cat part1.csv part2.csv > all.csv && wc -l all.csv

Output:

2000 all.csv

What it means: > creates or overwrites all.csv. Use >> to add to the end.

  1. Write a quick file with a here-doc.
cat > note.txt <<'END'
Server: web-01
Owner: Aditi
END
cat note.txt

Output:

Server: web-01
Owner: Aditi

What it means: Everything until END goes into the file.

cat with > and >>: overwrite vs append

Why do we need this?

Saving output to a file is daily work. One extra > is the difference between adding a line and wiping a file.

What is it?

> sends output to a file and first empties it (overwrite). >> adds to the end (append). Both create the file if missing. cat > file reads what you type until Ctrl+D.

Common options

OptionWhat it does
cat a > bWrite a into b; old b content is lost.
cat a >> bAdd a to the end of b.
cat > fType text into f; Ctrl+D to save.
cat >> fType text and add it to f.
set -o noclobberMake > refuse to overwrite.

How to do it (step by step)

  1. Write a new file by typing.
cat > guests.txt
Rohan
Meera
(press Ctrl+D)

What it means: Two lines saved in guests.txt.

  1. Add one more name with >>.
cat >> guests.txt
Kabir
(press Ctrl+D)
cat guests.txt

Output:

Rohan
Meera
Kabir

What it means: >> kept the old lines and added Kabir.

  1. See what > does to it.
cat > guests.txt
Zoya
(press Ctrl+D)
cat guests.txt

Output:

Zoya

What it means: > emptied the file first. The other names are gone.

  1. Protect files from >.
set -o noclobber
cat menu.txt > guests.txt

Output:

bash: guests.txt: cannot overwrite existing file

What it means: Now > refuses. Use >| if you really mean it.

Practise this in the tasks below: Practice tasks

3. tac: print lines in reverse

Why do we need this?

Logs keep the newest lines at the bottom. Sometimes you want the newest first.

What is it?

tac prints the last line first and the first line last. It is cat spelled backwards. Do not confuse it with rev, which reverses the characters inside each line.

How to do it (step by step)

  1. Newest lines first with tac.
tac app.log | head -3

Output:

10:05 order 1003 paid
10:04 order 1002 paid
10:03 order 1001 created

What it means: tac = cat backwards.

  1. Compare with rev.
echo 'Pune' | rev

Output:

enuP

What it means: rev flips characters, tac flips lines.

4. nl: number lines

Why do we need this?

In reviews and tickets you say 'see line 14'. Numbers make that easy.

What is it?

nl numbers non-empty lines by default (like cat -b). -ba numbers all lines, -w3 sets the width, -s': ' sets the separator.

How to do it (step by step)

  1. nl skips empty lines.
nl shopping.txt

Output:

     1	milk
     2	bread

     3	eggs

What it means: Good for code reviews.

  1. Number every line with a custom separator.
nl -ba -w2 -s': ' shopping.txt

Output:

 1: milk
 2: bread
 3: 
 4: eggs

What it means: -ba includes the empty line 3.

5. less: read big files without fear

Why do we need this?

A 2 GB log printed with cat floods your screen and can freeze a slow SSH session. You need to scroll, search and jump.

What is it?

less opens a file one screen at a time and does not load it all into memory. Keys: Space or f next page, b back, g top, G bottom, /word search forward, ?word search back, n next match, N previous, q quit, F follow like tail -f (Ctrl+C to stop). Options: -N line numbers, -S no wrap for wide lines, +G open at the end, -R keep colours. more is the older, simpler pager.

KeyWhat it does
Space / bNext / previous page
g / GTop / bottom
/textSearch forward
n / NNext / previous match
FFollow new lines
&textShow only lines with text
qQuit

How to do it (step by step)

  1. Open a big log.
less /var/log/syslog

Output:

Oct 25 09:00:01 web-01 CRON[1201]: (root) CMD (backup.sh)
:

What it means: The : at the bottom means less waits for a key.

  1. Open at the end with line numbers.
less -N +G /var/log/nginx/access.log

Output:

  48211 10.0.0.7 - - [25/Oct/2026:10:15:02] "GET /cart HTTP/1.1" 200

What it means: You land on the last line, number 48211.

  1. Search for an error inside less.
/ERROR

Output:

10:12:44 ERROR payment timeout order=8812

What it means: Press n to jump to the next ERROR.

  1. Read command output page by page.
ps aux | less -S

Output:

USER  PID %CPU %MEM COMMAND
root    1  0.0  0.1 /sbin/init

What it means: -S stops long lines from wrapping. Use arrow keys left and right.

  1. Read a compressed log without unzipping.
zless /var/log/syslog.2.gz

Output:

Oct 23 00:00:01 web-01 systemd[1]: Started Daily apt upgrade

What it means: zless, zcat and zgrep work on .gz files directly.

6. more: the simple pager

Why do we need this?

On tiny systems (rescue mode, BusyBox) less may not exist, but more always does.

What is it?

more shows one screen at a time. Space next page, Enter next line, /text search, q quit. It cannot scroll back well. Prefer less when it is there.

How to do it (step by step)

  1. Page through a file.
more /etc/services

Output:

# /etc/services
tcpmux 1/tcp
--More--(2%)

What it means: --More-- shows how far you are.

  1. Page command output.
dmesg | more

Output:

[    0.000000] Linux version 6.1.0
--More--

What it means: Works in a pipe too.

7. head and tail: the start and the end

Why do we need this?

Usually you only need the header of a CSV or the last few lines of a log.

What is it?

head shows the first 10 lines, tail the last 10. -n N picks N lines. head -c N gives N bytes. head -n -5 gives all but the last 5. tail -n +2 starts at line 2 (skip a header). Combine them to cut a middle range: head -n 30 | tail -n 11 gives lines 20 to 30.

How to do it (step by step)

  1. First 3 lines of a CSV.
head -n 3 sales.csv

Output:

date,city,amount
2026-10-01,Pune,4500
2026-10-01,Nashik,3200

What it means: Line 1 is the header.

  1. Last 5 lines of a log.
tail -n 5 /var/log/auth.log

Output:

Oct 25 10:20:11 web-01 sshd[2231]: Accepted publickey for ravi from 10.0.0.5

What it means: The newest events are at the end.

  1. Skip the header.
tail -n +2 sales.csv | head -n 2

Output:

2026-10-01,Pune,4500
2026-10-01,Nashik,3200

What it means: +2 means start from line 2.

  1. Lines 20 to 30.
head -n 30 app.log | tail -n 11

Output:

(11 lines: 20..30)

What it means: Or use sed -n '20,30p' app.log (next chapters).

  1. Several files at once.
head -n 1 *.csv

Output:

==> jan.csv <==
date,city,amount

==> feb.csv <==
date,city,amount

What it means: head prints a ==> name <== banner per file. -q hides it.

  1. Top 5 biggest folders.
du -sh /var/* 2>/dev/null | sort -rh | head -5

Output:

2.1G /var/log
800M /var/lib
120M /var/cache

What it means: head is the last step of many 'top N' pipelines.

8. tail -f and tail -F: watch logs live

Why do we need this?

When you deploy or debug, you want to see new log lines the moment they are written.

What is it?

tail -f keeps the file open and prints new lines. If the log is rotated (renamed and a new file made), -f keeps watching the old file and goes silent. tail -F (same as --follow=name --retry) re-opens the new file by name, so use -F on servers. Stop with Ctrl+C. --pid=PID stops when a process ends. For systemd services use journalctl -u name -f.

How to do it (step by step)

  1. Follow a web log.
tail -f /var/log/nginx/access.log

Output:

10.0.0.9 - - [25/Oct/2026:10:30:01] "GET / HTTP/1.1" 200 612
(waits for new lines...)

What it means: The cursor stays. New requests appear live.

  1. Follow safely across log rotation.
tail -F /var/log/app/app.log

Output:

tail: '/var/log/app/app.log' has been replaced;  following new file
10:31:00 INFO started

What it means: -F noticed the new file and kept going.

  1. Follow two logs together.
tail -F app.log worker.log

Output:

==> app.log <==
10:32 GET /api/orders

==> worker.log <==
10:32 job 77 done

What it means: Banners show which file each line came from.

  1. Follow and filter only errors.
tail -F app.log | grep --line-buffered ERROR

Output:

10:33:12 ERROR db timeout

What it means: --line-buffered makes grep print at once in a pipe.

  1. Follow a systemd service.
journalctl -u nginx -f

Output:

Oct 25 10:34:01 web-01 nginx[880]: reload done

What it means: journalctl is the tail -f for services that log to the journal.

9. wc: count lines, words and bytes

Why do we need this?

How many users? How many errors today? Is this file empty? Counting answers many ops questions fast.

What is it?

wc prints lines, words and bytes. -l lines, -w words, -c bytes, -m characters (Unicode safe), -L longest line. With a pipe it prints only the number (no file name), which is great in scripts. Note: wc -l counts newline characters, so a last line without a newline is not counted.

How to do it (step by step)

  1. Count everything.
wc notes.txt

Output:

  12  85 512 notes.txt

What it means: 12 lines, 85 words, 512 bytes.

  1. Count only lines.
wc -l /etc/passwd

Output:

38 /etc/passwd

What it means: 38 user accounts.

  1. Only the number, for scripts.
wc -l < /etc/passwd

Output:

38

What it means: Redirect < hides the file name.

  1. Count errors in a log.
grep ERROR app.log | wc -l

Output:

27

What it means: Same as grep -c ERROR app.log.

  1. Bytes vs characters in Marathi text.
echo -n 'पुणे' | wc -c; echo -n 'पुणे' | wc -m

Output:

12
4

What it means: Each Devanagari character takes 3 bytes in UTF-8.

  1. Count files in a folder.
ls /etc/nginx/conf.d | wc -l

Output:

4

What it means: Quick count. For odd names use find (chapter 12).

10. tee: save output and see it too

Why do we need this?

During an upgrade you want to watch the output and also keep a log file for later. Or you need to write into a root-owned file from a normal shell.

What is it?

tee reads input and writes it to the screen and to one or more files, like a T-shaped pipe joint. -a appends instead of overwriting. sudo tee file is the right way to write into protected files, because in sudo echo x > file the > runs as you, not as root. Add > /dev/null to hide the screen copy.

How to do it (step by step)

  1. Watch and save an upgrade.
sudo dnf -y upgrade 2>&1 | tee upgrade-$(date +%F).log

Output:

Upgrading: openssl 3.0.7 ...
Complete!

What it means: 2>&1 also captures errors. The log is named by date.

  1. Append to a log.
echo "$(date +%T) deploy v42 by Neha" | tee -a deploy.log

Output:

10:40:02 deploy v42 by Neha

What it means: -a keeps old lines.

  1. Write a root file the right way.
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swap.conf

Output:

vm.swappiness=10

What it means: Works. sudo echo ... > file would fail with Permission denied.

  1. Save to two files.
df -h | tee disk-now.txt disk-$(hostname).txt > /dev/null && ls disk-*

Output:

disk-now.txt  disk-web-01.txt

What it means: > /dev/null hides the screen copy.

  1. Save the middle of a pipe.
cat access.log | tee raw.txt | grep ' 500 ' | wc -l

Output:

14

What it means: raw.txt has all lines, the count is only for 500 errors.

11. Real examples from many industries

Why do we need this?

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

What is it?

Each row is a made-up team. It shows who uses this and how.

How to do it (step by step)

Team and cityHow they use itCommand
Priya, e-commerce SRE, BengaluruFollows checkout logs live during sale hourstail -F /var/log/shop/checkout.log
Lukas, bank ops, FrankfurtKeeps a signed log of every patch runsudo apt upgrade 2>&1 | tee -a patch.log
Fatima, telecom NOC, DubaiCounts dropped calls per hourgrep DROP cdr.log | wc -l
Arjun, hospital IT, NagpurChecks the CSV header before importing patient datahead -n 1 patients.csv
Chen, game studio, TaipeiReads crash logs page by page with searchless -N crash.log
Olivia, media, LondonJoins daily view files into one monthly filecat views-2026-10-*.csv > views-oct.csv

12. Common errors and how to fix them

Why do we need this?

Everyone hits these errors. Knowing the cause saves hours.

What is it?

Read the exact message first. Then match it to a row below.

How to do it (step by step)

Error you seeWhy it happensHow to fix
cat: app.log: No such file or directoryWrong path or name (Linux is case sensitive)Check with ls and use Tab completion.
ls -l /var/log/app/
cat: /var/log/secure: Permission deniedThe file is readable only by root or a groupUse sudo, or join the adm group on Ubuntu.
sudo less /var/log/secure
bash: /etc/sysctl.d/x.conf: Permission denied (with sudo echo >)The > redirect runs as your user, not rootPipe into sudo tee.
echo 'x=1' | sudo tee /etc/sysctl.d/x.conf
tail -f shows nothing after midnightlogrotate replaced the file; -f watches the old oneUse tail -F.
tail -F /var/log/app.log
Screen full of strange symbols after catYou printed a binary fileCheck with file first, run reset to fix the terminal.
file data.bin; reset
cat: big.log: input file is output fileYou redirected a file into itselfWrite to a new file, then move.
cat a big.log > new.log && mv new.log big.log
wc -l shows one less than expectedThe last line has no newline at the endAdd a newline or count with grep -c ''.
grep -c '' file.txt
head: invalid number of lines: '5x'Bad number after -nGive a plain number.
head -n 5 file.txt
less: WARNING: terminal is not fully functionalTERM is not set (often in cron or odd SSH)Set TERM.
export TERM=xterm-256color
Pipe output appears late with tail -f | grepgrep buffers output in a pipeAdd --line-buffered.
tail -F app.log | grep --line-buffered ERROR

13. Troubleshooting step by step

Why do we need this?

When a log looks wrong or empty, check the file, its size, permissions and whether it is still being written.

What is it?

Go top to bottom. Stop at the first step that shows the problem.

How to do it (step by step)

  1. Does the file exist and how big is it?
ls -lh /var/log/app/app.log
  1. Is it text or binary?
file /var/log/app/app.log
  1. Can you read it?
head -n 2 /var/log/app/app.log || sudo head -n 2 /var/log/app/app.log
  1. Is anything writing to it right now?
sudo lsof /var/log/app/app.log
  1. Was it rotated? Look for .1 or .gz files.
ls -lt /var/log/app/ | head
  1. Is the app logging to the journal instead?
journalctl -u app --since '10 min ago'
  1. Check for Windows line ends or tabs.
cat -A /var/log/app/app.log | head -3

14. Lab 1: Inspect and follow a web server log

Why do we need this?

Make a fake access log, read it with head, tail and less, follow it live and count errors.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Create a folder and a sample log.
mkdir -p ~/lab7 && cd ~/lab7 && for i in $(seq 1 200); do echo "10.0.0.$((i%9)) GET /p$i $([ $((i%10)) -eq 0 ] && echo 500 || echo 200)"; done > access.log

Output:

(no output)
  1. Count lines.
wc -l access.log

Output:

200 access.log
  1. Look at the start and the end.
head -n 3 access.log; tail -n 2 access.log

Output:

10.0.0.1 GET /p1 200
10.0.0.2 GET /p2 200
10.0.0.3 GET /p3 200
10.0.0.1 GET /p199 200
10.0.0.2 GET /p200 500
  1. Count 500 errors.
grep -c ' 500$' access.log

Output:

20
  1. Follow live in one terminal.
tail -F access.log

Output:

(waits)
  1. In a second terminal, add a line.
echo '10.0.0.7 GET /new 200' >> ~/lab7/access.log

Output:

(first terminal shows the new line)
  1. Stop with Ctrl+C and open in less at the end.
less -N +G access.log

Output:

   201 10.0.0.7 GET /new 200

15. Lab 2: Save a patch run with tee

Why do we need this?

Run a command, watch it and keep a dated log. Write a root-owned file safely.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Run and log at the same time.
uname -a 2>&1 | tee -a ~/lab7/run-$(date +%F).log

Output:

Linux web-01 6.1.0 x86_64 GNU/Linux
  1. Run again and check the log grew.
uptime | tee -a ~/lab7/run-$(date +%F).log >/dev/null; wc -l ~/lab7/run-*.log

Output:

2 /home/ravi/lab7/run-2026-10-25.log
  1. Try the wrong way to write a root file.
sudo echo 'hello' > /etc/lab7.conf

Output:

bash: /etc/lab7.conf: Permission denied
  1. Do it the right way.
echo 'hello' | sudo tee /etc/lab7.conf

Output:

hello
  1. Clean up.
sudo rm /etc/lab7.conf

Output:

(no output)

Ravindra Bagale's Tip

On servers always use tail -F, not tail -f. Logs get rotated at night, and -F keeps following the new file so you do not miss the error you are waiting for.

16. Frequently asked questions

Q. Why not use cat for everything?

cat is fine for short files. For long files it floods the screen. less lets you scroll and search, and head or tail give just the part you need.

Q. What is 'useless use of cat'?

cat file | grep x works but starts an extra process. grep x file does the same. It matters little, but interviewers like the cleaner form.

Q. Difference between tail -f and tail -F?

-f follows the open file. -F follows the name and retries, so it survives log rotation.

Q. Does less load the whole file?

No. It reads what it needs, so it opens huge files fast.

Q. Why does wc -c differ from wc -m?

-c counts bytes, -m counts characters. Non-English letters use more than one byte in UTF-8.

Q. How do I see lines 100 to 120?

head -n 120 file | tail -n 21, or sed -n '100,120p' file.

Q. Can tee write to many files?

Yes, list them all: tee a.txt b.txt.

Q. How do I read .gz logs?

Use zcat, zless or zgrep. No need to unzip.

17. Interview questions and answers

Beginner level

Q. What does cat do?

It prints one or more files to the screen and can join them into one.

Q. How do you see the last 20 lines of a file?

tail -n 20 file.

Q. How do you count lines in a file?

wc -l file, or wc -l < file for only the number.

Q. How do you quit less?

Press q.

Mid level level

Q. Why use tail -F on production?

Logs rotate. -F follows by name and reopens the new file; -f would watch the old renamed one.

Q. Why does 'sudo echo x > /etc/file' fail?

The shell does the > redirect as your user before sudo runs. Use echo x | sudo tee /etc/file.

Q. How do you print lines 50 to 60?

head -n 60 f | tail -n 11, or sed -n '50,60p' f.

Q. How do you skip a CSV header?

tail -n +2 file.csv.

Senior level

Q. A script counts lines with wc -l and is off by one. Why?

The last line has no trailing newline. wc counts newline characters. Fix the producer or use grep -c ''.

Q. tail -f | grep shows lines late. Why and fix?

grep block-buffers when stdout is a pipe or file. Use --line-buffered, or stdbuf -oL.

Q. How would you safely view a 20 GB log on a small server?

less (does not load all), or tail/head, zless for gz. Avoid cat and editors that load the file into RAM.

Q. Disk shows full but you deleted a big log. What happened?

A process still has the file open, so space is not freed. Find it with lsof +L1 and restart it, or truncate with : > file before deleting next time.

Try at home

  1. Export any chat or long text to a file. Use wc to count lines and words, head and tail to see the first and last messages, and less to search a name.
  2. Run 'ping -c 5 google.com | tee ping.txt' and check ping.txt has the same output you saw.

Practice Tasks

About this scenario

Maple Leaf Books is a bookselling business in Toronto. Emily Tremblay looks after its IT and cloud work. They count money in CAD and run their AWS resources in the ca-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 21

Easy Print a file with line numbers

Why: Line numbers help in reviews.

What to do: Emily Tremblay wants to see app.conf of Maple Leaf Books with every line numbered, using cat.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Newest log lines first

Why: Newest events are at the bottom.

What to do: Print audit.log in reverse line order.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Read a big log with numbers

Why: Do not cat a 2 GB file.

What to do: Open /var/log/messages in less with line numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy First 5 lines

Why: Check a CSV header quickly.

What to do: Show the first 5 lines of sales.csv of Maple Leaf Books.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Last 20 lines

Why: Recent errors are at the end.

What to do: Show the last 20 lines of /var/log/secure.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Count lines

Why: How many users exist?

What to do: Count the lines in /etc/group.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Count words

Why: Blog posts have word limits.

What to do: Count the words in post.md.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Quit less

Why: New users get stuck.

What to do: Which single key quits less?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Append operator

Why: Logs must grow, not reset.

What to do: Which operator adds output to the END of a file without deleting what is there?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Overwrite operator

Why: Know what wipes a file.

What to do: Which single operator sends output to a file and empties the file first?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Finish cat input

Why: Users get stuck in cat.

What to do: You typed lines into cat into a file. Which key combo ends the input and saves?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Show hidden characters

Why: Windows line ends break scripts.

What to do: Show tabs and line ends in deploy.sh so you can spot ^M.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Join two files

Why: Monthly exports come in parts.

What to do: Join jan.csv and feb.csv (in that order) into q1.csv.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Number all lines with nl

Why: Blank lines count too in some reviews.

What to do: Use nl to number every line of notes.txt, including empty ones.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Skip the header row

Why: Totals must not include the header.

What to do: Print leads.csv starting from line 2.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Follow a rotating log

Why: -f stops after logrotate.

What to do: Follow /var/log/api/api.log live so it keeps working after the file is rotated.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Count error lines

Why: A quick health number.

What to do: Count lines containing WARN in worker.log using grep and wc in a pipe.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Save and see output

Why: Keep a record of upgrades.

What to do: Run df -h and save its output to disk.txt while still showing it on screen.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Append with tee

Why: Do not lose old records.

What to do: Append the output of date to deploy.log using tee, keeping old lines.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Protect from overwrite

Why: One typo can wipe a file.

What to do: Which bash option name (used with set -o) makes > refuse to overwrite existing files?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Write a root file from a pipe

Why: sudo echo > file does not work.

What to do: Write the line vm.max_map_count=262144 into /etc/sysctl.d/99-maps.conf using echo and sudo tee.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. cat prints and joins; -n numbers, -A shows hidden characters.
  2. less is for big files: /search, n, G, q, F.
  3. head and tail pick the start and end; tail -n +2 skips a header.
  4. tail -F follows logs across rotation.
  5. wc -l counts lines; wc -l < file gives just the number.
  6. tee saves and shows; sudo tee writes root files.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

View files fast. Read files without opening an editor. cat small, less big, head/tail ends. Number lines or show hidden chars. Read the last 20 log lines.
Revision card: View files fast · Download PNG · Open card page
Follow live logs. See errors the moment they happen. tail -F keeps going after rotation. Save and see at once with tee. Follow and filter errors.
Revision card: Follow live logs · Download PNG · Open card page
cat vs less. Prints the whole file. / Shows one screen at a time.
Comparison: cat vs less · Download PNG · Open card page
head vs tail. First lines. / Last lines.
Comparison: head vs tail · Download PNG · Open card page
Why not cat a 5 GB log?. It floods the terminal. less loads only what you see. Open big logs at the end.
Interview card: Why not cat a 5 GB log? · Download PNG · Open card page
tail -f vs tail -F?. -f follows the open file. -F reopens after rotation. Use -F for app logs.
Interview card: tail -f vs tail -F? · Download PNG · Open card page
Why sudo echo > file fails?. Redirection runs as you. Let tee write as root. Write a root file.
Interview card: Why sudo echo > file fails? · Download PNG · Open card page
Count lines without the name?. Scripts need a pure number. Redirect input into wc. Number only.
Interview card: Count lines without the name? · Download PNG · Open card page

More cards: Ravi Sir's Cards.