Output Redirection and Piping in Practical DevSecOps Labs
Linux provides powerful mechanisms to control where command output goes. You can save output to files using redirection, or send it to another command for further processing using piping. Both are essential skills for every DevSecOps engineer.
Table of Contents
Output Redirection
Redirection uses the > and >> symbols to save command output to a file.
Overwrite (>)
cat /etc/passwd > mypasswd.txt
This reads /etc/passwd and saves its entire content into a new file called mypasswd.txt. If the file already exists, it will be overwritten.
Verify the content:
cat mypasswd.txt
Append (>>)
cat /etc/passwd >> mypasswd.txt
The double-greater-than (>>) appends content to the existing file instead of overwriting it. After running this, mypasswd.txt will contain the /etc/passwd content twice.
Using echo with redirection
echo is a handy command for displaying strings or variable values, and can also create files:
echo "this is a string" # prints to terminal
echo $HOSTNAME # prints the hostname variable
echo "this is a string" > file.txt # writes to a file
Piping Output
A pipe (|) takes the output of one command and sends it as input to another command.
Extracting fields with cut
Given mypasswd.txt containing /etc/passwd data:
cat mypasswd.txt | cut -d ':' -f 1
This pipes the file content to cut, which uses : as a delimiter (-d ':') and extracts the first field (-f 1), giving you just the usernames:
root
daemon
bin
sys
...
Searching output with grep
ps -aux | grep bash
This lists all running processes (ps -aux) and filters for lines containing "bash":
root 1 0.0 0.0 21340 4660 pts/0 Ss 14:29 0:00 bin/bash
root 624 0.0 0.0 11556 1156 pts/0 S+ 14:30 0:00 grep --color=auto bash
Try running ps -aux without grep to see the full, unfiltered output.
Common Questions
| Question | Answer |
|---|---|
What's the difference between > and >>? |
> overwrites the file. >> appends to the file. |
| Can I pipe multiple commands together? | Yes: cmd1 | cmd2 | cmd3. Output flows left to right. |
| Can I redirect AND pipe at the same time? | Redirecting (>) sends output to a file, piping (|) sends it to another command. You typically use one or the other for a given output stream. |
| How do I redirect error output? | Use 2> for errors and &> for both standard output and errors (e.g., command > out.txt 2> err.txt). |
What does cut -d ':' -f 1 mean? |
-d sets the delimiter (:), -f 1 selects the first field. Try -f 2 for the second field. |
Wrap-Up
Output redirection and piping are cornerstones of Linux command-line proficiency. Use > to save output, >> to append, and | to chain commands together for powerful data processing workflows. These skills are used in nearly every subsequent exercise across the Practical DevSecOps courses. Read more at Introduction to Pipes in Linux.