You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Linux excels at text processing. The philosophy of small, focused tools that can be chained together with pipes makes the command line extraordinarily powerful for working with text data, log files, configuration files, and more.
cat file.txt # display entire file
cat -n file.txt # display with line numbers
cat file1.txt file2.txt # display multiple files concatenated
less /var/log/syslog # scroll through a large file
more /var/log/syslog # simpler pager (forward only)
less navigation:
| Key | Action |
|---|---|
| Space / f | Forward one page |
| b | Back one page |
| /pattern | Search forward |
| ?pattern | Search backward |
| n | Next search match |
| N | Previous search match |
| g | Go to start |
| G | Go to end |
| q | Quit |
head -n 20 file.txt # first 20 lines
tail -n 50 /var/log/syslog # last 50 lines
tail -f /var/log/syslog # follow (watch) new lines in real time
tail -f -n 100 app.log # show last 100 lines then follow
Tip:
tail -fis essential for monitoring log files in real time. Press Ctrl+C to stop.
Pipes send the output of one command as input to another:
ls -la | less # page through directory listing
cat /etc/passwd | wc -l # count lines in passwd file
ps aux | grep nginx # find nginx processes
history | grep "git commit" # find past git commit commands
| Operator | Meaning |
|---|---|
> | Redirect stdout to file (overwrite) |
>> | Redirect stdout to file (append) |
2> | Redirect stderr to file |
2>&1 | Redirect stderr to stdout |
&> | Redirect both stdout and stderr |
< | Use file as stdin |
ls /home > users.txt # save output to file
echo "log entry" >> app.log # append to log file
find / -name "*.conf" 2>/dev/null # discard error messages
command &> output.log # capture both stdout and stderr
sort < unsorted.txt > sorted.txt # read from file, write to file
grep searches for patterns in files or piped input:
grep "error" /var/log/syslog # find lines containing "error"
grep -i "warning" app.log # case-insensitive search
grep -r "TODO" src/ # search recursively in a directory
grep -n "function" script.js # show line numbers
grep -c "404" access.log # count matching lines
grep -v "debug" app.log # invert — show lines NOT matching
grep -l "password" /etc/* # list files containing "password"
grep -w "error" log.txt # match whole word only
grep -A 3 "FATAL" app.log # show 3 lines after match
grep -B 2 "FATAL" app.log # show 2 lines before match
grep -E "error|warning|fatal" log.txt # extended regex (multiple patterns)
| Flag | Meaning |
|---|---|
-i | Case-insensitive |
-r | Recursive |
-n | Show line numbers |
-c | Count matches |
-v | Invert match |
-l | List filenames only |
-w | Whole word match |
-E | Extended regex (egrep) |
-A N | N lines after match |
-B N | N lines before match |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.