M07 - File Operations: View and Read

Read files safely from the terminal, inspect the beginning or end of long output, and search inside text without opening a GUI editor.

File System

File Operations: View and Read

Read files safely from the terminal, inspect the beginning or end of long output, and search inside text without opening a GUI editor.

35 min BEGINNER BOTH
What you should learn
  • Read files safely from the terminal, inspect the beginning or end of long output, and search inside text without opening a GUI editor.
Visual model

Concept to practice path

Each lesson is meant to move from an idea, to an example, to a safe practice step.

Open the full visual

Why This Matters

Once you can move around the file system, the next skill is reading what is there without damaging anything.

This is real operating-system work:

  • checking a config file before changing it
  • reading the end of a log after an error
  • searching a large text file for one useful line

For this stage, stay in safe practice files or read-only system files. We are learning observation first, not editing.

Real scenario

Reading a log without making things worse

Situation

A service fails and you only need the last few lines of the log to see what happened.

Common wrong move

Open the whole file in a full editor or dump thousands of lines into the terminal.

Better move

Read only the end of the file first, then page or search if you need more context.

Good file work starts with the smallest safe view that answers the question.


1. Read a Small File Directly

If a file is short, you can print it straight to the terminal.

PowerShell uses Get-Content. The alias cat works too, but it is still PowerShell behind the scenes.

Read a Small File

Get-Content .\notes.txt

Alias form

cat .\notes.txt

Linux commonly uses cat for small text files.

Read a Small File

cat ./notes.txt

Show line numbers when that helps

cat -n ./notes.txt

Use this when the file is short enough to understand in one screen or two.

Good Habit

If you are not sure how large a file is, do not dump it blindly. Start with a safer tool like less, more, or the last few lines.


2. Read a Large File One Screen at a Time

Large logs and long configs are easier to inspect with a pager.

Page Through Output

Get-Content .\server.log | more

Press Space for the next page. Press q or Ctrl+C to stop when needed.

Page Through a File

less /var/log/syslog

Inside less, use the arrow keys or Space to move, /text to search, and q to quit.

For beginners, this is often the safest first move on a large file because you stay in read-only mode.


3. Inspect Only the Beginning or End

Very often you do not need the whole file. You just need the first few lines or the most recent lines.

Read the Start or End

First 5 lines

Get-Content .\app.log -TotalCount 5

Last 10 lines

Get-Content .\app.log -Tail 10

Follow new lines as they appear

Get-Content .\app.log -Tail 10 -Wait

Read the Start or End

First 5 lines

head -n 5 ./app.log

Last 10 lines

tail -n 10 ./app.log

Follow new lines as they appear

tail -f ./app.log

This is especially useful for logs because the newest information is usually at the end.


4. Search Inside Text

Reading line by line is slow when you already know the word you want.

Search for Matching Lines

Select-String -Path .\server.log -Pattern "ERROR"

Include line numbers

Select-String -Path .\server.log -Pattern "timeout"

Search for Matching Lines

grep "ERROR" ./server.log

Case-insensitive with line numbers

grep -in "timeout" ./server.log

For now, keep the search simple. Exact words and simple flags are enough. Advanced regular expressions can wait.


What to Ignore for Now

  • full regular-expression syntax
  • binary log formats
  • editing inside pagers or advanced terminal editors

The goal here is simple: read safely, inspect quickly, and search with intention.


Before You Move On

You are ready for the next lesson when you can do all three of these without guessing:

  1. Print a short text file.
  2. Read only the last few lines of a longer file.
  3. Search a file for one keyword.

The next module adds file creation and small, controlled edits.

Learning loop

Recall it, check it, then practice it.

Do this before moving on. It turns reading into memory and safer action.

Recall
  • When would you use cat or Get-Content, and when would that be the wrong tool?
  • How would you inspect only the end of a log file without opening the whole file in an editor?
Check
  • Read a small file, inspect the end of a larger file, and search for one keyword without notes.
  • Explain the difference between dumping a file, paging through it, and searching inside it.

What to do next

Best next step Practice

Use the matching lab or keep building skill in practice.

Open now