M07 - File Operations: View and Read
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.
- Open small text files from the terminal.
- Inspect long files without flooding the screen.
- Search inside text for useful lines.
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.
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.
Get-Content .\notes.txt
Alias form
cat .\notes.txt
Linux commonly uses cat for small text files.
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.
Get-Content .\server.log | more
Press Space for the next page. Press q or Ctrl+C to stop when needed.
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.
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
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.
Select-String -Path .\server.log -Pattern “ERROR”
Include line numbers
Select-String -Path .\server.log -Pattern “timeout”
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:
- Print a short text file.
- Read only the last few lines of a longer file.
- Search a file for one keyword.
The next module adds file creation and small, controlled edits.