Text Editing, Redirection & Pipes

25 minLesson 4 of 16

Learning Objectives

  • Write content to files using echo and redirection operators
  • Read files with cat, head, and tail
  • Edit files with nano and vim
  • Chain commands using pipes and logical operators
  • Understand stdin, stdout, and stderr

Writing to Files

The echo Command

echo prints text to the terminal (standard output):

echo "Hello, DevOps!"

Output Redirection

Redirect output to a file instead of the terminal:

# Write to file (overwrites existing content)
echo "First line" > myfile.txt
 
# Append to file (adds to end)
echo "Second line" >> myfile.txt
OperatorBehavior
>Overwrite file contents
>>Append to file
# Overwrite demonstration
echo "Line A" > demo.txt
echo "Line B" > demo.txt
cat demo.txt
# Output: Line B (Line A was overwritten)
 
# Append demonstration
echo "Line A" >> demo2.txt
echo "Line B" >> demo2.txt
cat demo2.txt
# Output: Line A
#         Line B

Redirect Any Command Output

Any command that prints to the terminal can be redirected:

# Save directory listing to a file
ls / > root_contents.txt
 
# Save system info
uname -a > system_info.txt
 
# Append date to a log
date >> activity.log

Reading Files

# Display entire file
cat myfile.txt
 
# Display first N lines
head -n 5 myfile.txt
 
# Display last N lines
tail -n 3 myfile.txt
 
# Follow a file in real-time (great for logs)
tail -f /var/log/syslog

Text Editors

Nano (Beginner-Friendly)

nano myfile.txt

Essential nano shortcuts:

ShortcutAction
Ctrl+OSave file
Ctrl+XExit
Ctrl+KCut line
Ctrl+UPaste line
Ctrl+WSearch
Ctrl+Y/VPage up/down

Vim (Power User)

vim myfile.txt

Vim has modes:

  • Normal mode (default) — navigate and execute commands
  • Insert mode — type text (press i to enter)
  • Command mode — save, quit (press :)

Essential vim commands:

CommandAction
iEnter insert mode
EscReturn to normal mode
:wSave
:qQuit
:wqSave and quit
:q!Quit without saving
ddDelete line
yyCopy line
pPaste
💡

For DevOps work, nano is fine for quick edits. Learn vim basics too — it's available on virtually every Linux server.

I/O Streams

Linux has three standard streams:

StreamDescriptorDefault
stdin (input)0Keyboard
stdout (output)1Terminal
stderr (error)2Terminal

Redirecting Errors

# Redirect errors to a file
cat nonexistent_file 2> errors.log
 
# Redirect both output and errors
ls /root > output.txt 2> errors.txt
 
# Redirect both to the same file
ls /root > all_output.txt 2>&1
 
# Discard errors completely
command_that_might_fail 2> /dev/null

Input Redirection

# Feed file contents as input
sort < unsorted_list.txt
 
# Combine input and output redirection
sort < unsorted.txt > sorted.txt

Pipes

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

# Find files containing "bin" in root listing
ls / | grep bin
 
# Count number of files in a directory
ls | wc -l
 
# Find a specific process
ps aux | grep nginx
 
# Chain multiple pipes
ls / | grep bin | head -n 1

Common Pipe Patterns

# Sort and remove duplicates
cat names.txt | sort | uniq
 
# Search logs for errors
cat /var/log/syslog | grep -i error | tail -n 20
 
# Count lines matching a pattern
grep -c "404" access.log

Chaining Commands

Sequential Execution (&&)

Run the next command only if the previous one succeeds:

# Update and install (second runs only if first succeeds)
sudo apt update && sudo apt install nginx
 
# Create directory and enter it
mkdir project && cd project

Run Regardless (;)

# Run both commands regardless of success/failure
echo "Starting..." ; ls /nonexistent ; echo "Done"

Run on Failure (||)

# Run second command only if first fails
ping -c 1 google.com || echo "Network is down"

The grep Command

grep searches for patterns in text — one of the most useful Linux commands:

# Search for a word in a file
grep "error" logfile.txt
 
# Case-insensitive search
grep -i "warning" logfile.txt
 
# Search recursively in directories
grep -r "TODO" ./src/
 
# Show line numbers
grep -n "function" script.sh
 
# Invert match (show lines NOT matching)
grep -v "debug" logfile.txt

Summary

  • > overwrites, >> appends output to files
  • cat, head, tail read file contents
  • nano for quick edits, vim for power users
  • 2> redirects errors, 2>&1 combines stdout and stderr
  • | pipes output between commands
  • && chains commands (run next only on success)
  • grep searches text with powerful pattern matching

Next Steps

In the next lesson, we'll cover users, groups, permissions, and environment variables — essential for securing Linux systems.