Linux / Bash
Beginner
1 min read
Advanced Redirection and tee
Example
# tee: display output AND save to file
ls -la | tee filelist.txt
# tee: append to file instead of overwrite
command_output | tee -a /var/log/deploy.log
# tee: write to multiple files
echo "Status: OK" | tee status.txt backup_status.txt
# tee in the middle of a pipeline
cat /var/log/syslog | grep "error" | tee errors_raw.txt | wc -l
# Capture output with sudo (tee workaround)
echo "new_config=value" | sudo tee /etc/app/config.conf > /dev/null
# ---- Process substitution ----
# Compare directory listings without temp files
diff <(ls /etc/nginx/) <(ls /etc/apache2/)
# Compare sorted versions of two files
diff <(sort file1.txt) <(sort file2.txt)
# Use command output as a file argument
wc -l <(find / -name "*.log" 2>/dev/null)
# Feed command output into a while loop (avoids subshell)
while IFS= read -r line; do
echo "Processing: $line"
done < <(find /var/log -name "*.log" -mtime -1)
# ---- Named pipes (FIFOs) ----
# Create a named pipe
mkfifo /tmp/mypipe
# Writer process (runs in background)
ls -la > /tmp/mypipe &
# Reader process (consumes from pipe)
cat /tmp/mypipe
# Clean up
rm /tmp/mypipe
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash