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| Operator | Behavior |
|---|---|
> | 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 BRedirect 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.logReading 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/syslogText Editors
Nano (Beginner-Friendly)
nano myfile.txtEssential nano shortcuts:
| Shortcut | Action |
|---|---|
Ctrl+O | Save file |
Ctrl+X | Exit |
Ctrl+K | Cut line |
Ctrl+U | Paste line |
Ctrl+W | Search |
Ctrl+Y/V | Page up/down |
Vim (Power User)
vim myfile.txtVim has modes:
- Normal mode (default) — navigate and execute commands
- Insert mode — type text (press
ito enter) - Command mode — save, quit (press
:)
Essential vim commands:
| Command | Action |
|---|---|
i | Enter insert mode |
Esc | Return to normal mode |
:w | Save |
:q | Quit |
:wq | Save and quit |
:q! | Quit without saving |
dd | Delete line |
yy | Copy line |
p | Paste |
💡
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:
| Stream | Descriptor | Default |
|---|---|---|
| stdin (input) | 0 | Keyboard |
| stdout (output) | 1 | Terminal |
| stderr (error) | 2 | Terminal |
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/nullInput Redirection
# Feed file contents as input
sort < unsorted_list.txt
# Combine input and output redirection
sort < unsorted.txt > sorted.txtPipes
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 1Common 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.logChaining 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 projectRun 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.txtSummary
>overwrites,>>appends output to filescat,head,tailread file contentsnanofor quick edits,vimfor power users2>redirects errors,2>&1combines stdout and stderr|pipes output between commands&&chains commands (run next only on success)grepsearches 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.