103 β€” Viewing & Searching Files

Beginner

Master commands for reading file contents and searching through files and directories. Learn cat, less, head, tail, grep, and find to efficiently locate and inspect data.

Learning Objectives

1
View file contents with cat, less, head, and tail
2
Search file contents with grep
3
Find files and directories with find
4
Monitor log files in real-time
Step 1

Set up practice files with content

Create files with sample content for viewing and searching.

Commands to Run

cd ~
mkdir search-practice && cd search-practice
echo 'Hello World' > hello.txt
echo -e 'Line 1\nLine 2\nLine 3\nERROR: Something failed\nLine 5' > log.txt
echo -e 'apple\nbanana\ncherry\ndate\nelderberry' > fruits.txt

What This Does

echo writes text to files. > creates new file. -e enables escape sequences like \n (newline). We're creating test files with known content to practice viewing and searching.

Expected Outcome

Three files created: hello.txt (1 line), log.txt (5 lines with ERROR), fruits.txt (5 lines of fruit names).

Pro Tips

  • 1
    echo writes to stdout (screen) by default
  • 2
    > redirects output to file (creates or overwrites)
  • 3
    >> appends to file instead of overwriting
  • 4
    -e flag interprets \n as newline

Common Mistakes to Avoid

  • ⚠️Using > instead of >> and accidentally overwriting files
  • ⚠️Forgetting quotes around text with spaces
Was this step helpful?

All Steps (0 / 11 completed)