Ubuntu Redirections
Introduction
When working with the Ubuntu Terminal, understanding how to control the flow of data is essential for becoming a proficient command-line user. Redirections are powerful features that allow you to manipulate where input comes from and where output goes. Instead of just seeing command output on the screen, you can redirect it to files, use files as input, or connect commands together.
In this guide, we'll explore how Ubuntu's shell (Bash) handles three standard data streams and how you can redirect them to suit your needs:
- Standard Input (stdin) - Data fed into commands (keyboard by default)
- Standard Output (stdout) - Normal command output (terminal screen by default)
- Standard Error (stderr) - Error messages (terminal screen by default)
Basic Output Redirection
Redirecting Standard Output (stdout)
To redirect the standard output of a command to a file instead of displaying it on the screen, use the >
operator:
ls -la > file_list.txt
This command lists all files in the current directory (including hidden files) and saves the output to a file named file_list.txt
instead of displaying it on screen.
Important: The >
operator will create a new file or overwrite an existing file's content without warning. If file_list.txt
already existed, its previous content would be completely replaced.
Appending Output to Files
If you want to add output to the end of an existing file without overwriting it, use the >>
operator:
echo "New line of text" >> notes.txt
This command appends the text "New line of text" to the end of notes.txt
(or creates the file if it doesn't exist).
Example: Building a log file
echo "Log started on $(date)" > logfile.txt
echo "First operation completed" >> logfile.txt
echo "Second operation completed" >> logfile.txt
cat logfile.txt
Output:
Log started on Thu Mar 13 10:30:45 EDT 2025
First operation completed
Second operation completed
Redirecting Standard Error (stderr)
Error messages are sent to a separate output stream called stderr (standard error). To redirect error messages to a file, use 2>
:
find /etc -name "*.conf" 2> errors.txt
This command searches for configuration files in /etc
and redirects any error messages (like "permission denied") to errors.txt
while still showing successful results on the screen.
The number 2
refers to the file descriptor for standard error.
Redirecting Both stdout and stderr
Separate Files
To redirect standard output and standard error to different files:
find /etc -name "*.conf" > results.txt 2> errors.txt
Same File
To redirect both standard output and error messages to the same file:
find /etc -name "*.conf" > all_output.txt 2>&1
In modern Bash, you can also use a simpler syntax:
find /etc -name "*.conf" &> all_output.txt
Input Redirection
Using Files as Input
To use a file as input instead of typing at the keyboard, use the <
operator:
sort < unsorted_list.txt
This command takes the content of unsorted_list.txt
, sorts it, and displays the sorted result on screen.
Practical example: Processing a list of names
Create a file with unsorted names:
echo -e "Zack
Amy
John
Lisa" > names.txt
Then sort it:
sort < names.txt
Output:
Amy
John
Lisa
Zack
Here Documents (<<
)
A "here document" allows you to include multiple lines of input text directly in your command using the <<
operator followed by a delimiter word:
cat << EOF > poem.txt
Roses are red
Violets are blue
Linux is awesome
And so are you
EOF
This creates a file called poem.txt
containing the four lines of text.
Here Strings (<<<
)
For shorter input strings, you can use the <<<
operator:
grep "hello" <<< "hello world"
This searches for "hello" in the provided string "hello world".
Pipeline Redirection
Pipelines (|
) allow you to connect the output of one command to the input of another:
ls -la | grep ".txt"
This lists all files, then filters the results to show only those containing ".txt".
Practical example: Counting files by type
find . -type f | grep -o "\.[^\.]*$" | sort | uniq -c | sort -nr
This one-liner:
- Finds all regular files in the current directory and subdirectories
- Extracts just the file extension
- Sorts them
- Counts unique occurrences
- Sorts by frequency (most common first)
Advanced Redirection Techniques
Discarding Output
To discard output completely (send it to "nowhere"), redirect to /dev/null
:
find /etc -name "*.conf" 2>/dev/null
This command will show only successful results and silently discard any error messages.
Swapping stdout and stderr
Sometimes you might want to capture only error messages and ignore successful output:
find /etc -name "*.conf" 3>&1 1>&2 2>&3 | grep "Permission denied"
This complex redirection effectively swaps stdout and stderr, allowing you to pipe only the error messages to grep
.
Using tee for Dual Output
The tee
command allows you to send output to a file while still displaying it on screen:
ls -la | tee file_list.txt
This command lists all files, saves the output to file_list.txt
, and displays it on the terminal.
Real-World Examples
Example 1: Creating a System Backup Log
#!/bin/bash
echo "Backup started: $(date)" > backup.log
tar -czf backup.tar.gz /home/user/important_data >> backup.log 2>&1
echo "Backup completed: $(date)" >> backup.log
cat backup.log
This script creates a compressed backup and logs both normal messages and any errors to the same log file.
Example 2: Extracting Specific Data from Logs
grep "ERROR" /var/log/syslog | cut -d':' -f3- | sort | uniq -c > error_summary.txt
This command extracts error messages from the system log, processes them to remove timestamps, sorts them, counts occurrences of each unique error, and saves the summary to a file.
Example 3: Processing Data in a Pipeline
cat data.csv | grep -v "^#" | cut -d',' -f2,5 | sort | uniq > processed_data.txt
This pipeline:
- Reads a CSV file
- Removes comment lines (starting with #)
- Extracts only the 2nd and 5th columns
- Sorts and removes duplicates
- Saves the result to a new file
Redirection Operators Summary
Here's a quick reference guide to the redirection operators we've covered:
Operator | Description |
---|---|
> | Redirect stdout to a file (overwrite) |
>> | Redirect stdout to a file (append) |
2> | Redirect stderr to a file (overwrite) |
2>> | Redirect stderr to a file (append) |
&> | Redirect both stdout and stderr to a file (overwrite) |
&>> | Redirect both stdout and stderr to a file (append) |
< | Take input from a file |
<< | Here document (multi-line input) |
<<< | Here string (single-line input) |
| | Pipe output from one command to another |
/dev/null | Discard output |
Understanding File Descriptors
In Linux, everything is treated as a file, including input and output streams. Each stream is assigned a file descriptor number:
- 0: stdin (standard input)
- 1: stdout (standard output)
- 2: stderr (standard error)
This explains notation like 2>
(redirect file descriptor 2) and 2>&1
(redirect file descriptor 2 to where file descriptor 1 is going).
Summary
Redirections are powerful tools in the Ubuntu Terminal that let you control where data comes from and where it goes. By mastering these techniques, you can:
- Save command output to files for later reference
- Handle errors effectively
- Process data from files without manual input
- Chain commands together to create powerful data processing pipelines
- Create more maintainable scripts and automation
With practice, these redirection operators will become second nature and an essential part of your command-line toolkit.
Exercises
-
Create a script that searches your home directory for large files (over 100MB) and saves the results to a file called
large_files.txt
. -
Write a command that counts the number of warning and error messages in your system log and saves a summary to a file.
-
Create a pipeline that extracts all unique IP addresses from your Apache access log.
-
Write a command that silently removes all empty directories in your current folder (hint: use
/dev/null
to hide error messages). -
Create a here document that generates an HTML file with a simple webpage structure.
Additional Resources
- The Bash manual: Access with
man bash
and look for the "REDIRECTION" section - GNU Bash manual online
- Linux Documentation Project: Advanced Bash-Scripting Guide
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)