Script Valley
Developer Environment Setup (WSL, Terminal, VS Code)
Linux Terminal FundamentalsLesson 2.3

How to read and search file contents with cat, grep, and less

cat command, less pager, grep basic usage, grep -r recursive, grep -i case insensitive, grep -n line numbers, pipe operator, combining commands

Reading and Searching Files

cat pipe grep pipeline diagram

Reading files and searching through them are daily tasks. These three tools cover the vast majority of cases.

cat — print file contents

cat package.json
cat file1.txt file2.txt

For large files, cat floods the terminal. Use less instead.

less — scrollable pager

less largefile.log

Navigate with arrow keys or j/k. Press / to search within the file, n for next match, q to quit.

grep — search for patterns

grep error app.log
grep -i error app.log
grep -n error app.log
grep -r TODO src/
grep -rn console.log .

Combining with pipes

The pipe | sends the output of one command as input to the next:

cat app.log | grep error
ls -la | grep .js
ps aux | grep node

Pipes are one of the most powerful patterns in shell scripting. Any command that writes to stdout can feed any command that reads from stdin.

Up next

How to use environment variables and the PATH in Linux

Sign in to track progress