← Back to course

Reading and Searching Files

Reading and Searching Files

Welcome back.

In the previous lesson, you learned how to create, copy, move, rename, and delete files.

Now it is time to look inside them.

Because files are not decorations. They are containers of information.

Sometimes useful information.

Sometimes logs.

Sometimes a configuration file that looks like it was written by a wizard during a thunderstorm.

What You’ll Learn

In this lesson, you’ll learn how to:

The Mission

Your mission is simple:

Create a few text files, read them, inspect them, and search inside them.

We are turning the terminal into a detective tool.

No magnifying glass required.

A dramatic hat is optional.

Create a Practice File

First, go to your practice folder:

cd ~/terminal-practice

If the folder does not exist, create it:

mkdir -p ~/terminal-practice
cd ~/terminal-practice

Now create a file called story.txt:

touch story.txt

Open it with a text editor if you want, or create it quickly from the terminal:

echo "Linux is powerful." > story.txt
echo "The terminal is direct." >> story.txt
echo "Practice makes everything easier." >> story.txt
echo "Errors are messages, not monsters." >> story.txt

Now you have a file with a few lines of text.

Read a File with cat

To print the whole file in the terminal, use:

cat story.txt

You should see:

Linux is powerful.
The terminal is direct.
Practice makes everything easier.
Errors are messages, not monsters.

cat is useful for small files.

For big files, it can flood your terminal like someone opened a pasta factory inside your screen.

Use it wisely.

Read Long Files with less

For longer files, use:

less story.txt

Inside less, you can move around:

Yes, q means quit.

This is important.

Many people have been trapped inside less longer than they want to admit.

View the Beginning with head

To see the first lines of a file, use:

head story.txt

By default, head shows the first 10 lines.

To show only the first 2 lines:

head -n 2 story.txt

This is useful when you just want to quickly understand what a file is about.

Like reading the first page of a manual before deciding to ignore it completely.

View the End with tail

To see the last lines of a file, use:

tail story.txt

To show only the last 2 lines:

tail -n 2 story.txt

tail is very useful for log files, because new information usually appears at the end.

For example:

tail -f app.log

The -f option follows the file live.

It keeps watching new lines as they appear.

Very useful.

Also slightly hypnotic.

Search Inside a File with grep

To search for text inside a file, use grep.

Search for the word terminal:

grep "terminal" story.txt

You should see:

The terminal is direct.

Search for Errors:

grep "Errors" story.txt

You should see:

Errors are messages, not monsters.

grep is one of the most useful terminal commands.

It finds text fast.

Like Ctrl+F, but with stronger coffee.

Case-Insensitive Search

By default, grep is case-sensitive.

This means errors and Errors are different.

Try:

grep "errors" story.txt

You may see nothing.

Now try:

grep -i "errors" story.txt

The -i option ignores case.

Much better.

Linux is strict, but sometimes we can ask it to relax.

Show Line Numbers

To show line numbers, use:

grep -n "terminal" story.txt

You may see:

2:The terminal is direct.

This is useful when you want to find where something appears.

Especially in configuration files, logs, or code.

Search in Multiple Files

Create another file:

echo "The terminal can search many files." > notes.txt
echo "Learning Linux step by step is smart." >> notes.txt

Now search in all .txt files:

grep "terminal" *.txt

You may see results from more than one file.

This is where grep starts feeling like a superpower.

Not a flashy superpower.

More like a quiet librarian who can find anything in three seconds.

Common Mistakes

Using cat on huge files

This can be annoying:

cat very-large-file.log

If the file is huge, your terminal may fill with endless text.

Use:

less very-large-file.log

or:

tail very-large-file.log

Be smart. Do not fight the log dragon with a spoon.

Forgetting quotes

This is okay:

grep "terminal" story.txt

For simple words, this may also work:

grep terminal story.txt

But if your search has spaces, use quotes:

grep "Practice makes" story.txt

Quotes keep the words together.

Without quotes, the shell may misunderstand you.

The shell is powerful, not psychic.

Searching with the wrong case

If this does not find anything:

grep "errors" story.txt

try:

grep -i "errors" story.txt

Case matters unless you tell grep to ignore it.

Practice

Try this:

cd ~/terminal-practice
cat story.txt
less story.txt
head -n 2 story.txt
tail -n 2 story.txt
grep "terminal" story.txt
grep -i "errors" story.txt
grep -n "Practice" story.txt

Then answer:

  1. Which command prints the whole file?
  2. Which command is better for long files?
  3. Which command shows the first lines?
  4. Which command shows the last lines?
  5. Which command searches text inside files?

Mini Challenge

Create a file called linux-notes.txt with these lines:

Linux is stable.
The terminal is useful.
Commands can be combined.
Practice every day.
Do not fear errors.

Then:

  1. Show the whole file.
  2. Show only the first 2 lines.
  3. Show only the last 2 lines.
  4. Search for the word terminal.
  5. Search for the word errors ignoring case.
  6. Show the line number where Practice appears.

Use only terminal commands.

No mouse.

The mouse is starting to feel unemployed.

Summary

Today you learned:

Now you can read and search files from the terminal.

This is a big step.

Because once you can search, you stop being lost and start being dangerous.

In a good way.

Mostly.

Next Lesson

In the next lesson, we’ll learn about permissions and sudo.

That is where Linux starts saying:

“No.”

And we learn why.