Ubuntu Input Output
Introduction
Input and output (I/O) operations are fundamental concepts in any computing environment, including Ubuntu. When working with the terminal, understanding how to control the flow of information into programs (input) and how to manage the results produced by those programs (output) is essential for effective command-line usage.
In this guide, we'll explore how Ubuntu handles input and output streams, how to redirect these streams, and how to combine commands using pipes to create powerful command sequences.
Standard I/O Streams
Every command or program running in the Ubuntu terminal has three standard streams:
- Standard Input (stdin) - Stream for data going into a program (by default from keyboard)
- Standard Output (stdout) - Stream for normal output from a program (by default to terminal screen)
- Standard Error (stderr) - Stream for error messages (by default to terminal screen)
Let's look at each of these streams in detail and how to manipulate them.
Standard Input (stdin)
Standard input is the default data stream that feeds into a command. By default, this is your keyboard. When you type a command and provide input, you're using stdin.
Example: Using cat
to Read from stdin
The cat
command without arguments reads from stdin and outputs to stdout:
cat
Try typing the command above, then enter some text, and press Enter. The text will be repeated back to you. To exit, press Ctrl+D
(which sends an EOF - End of File - signal).
Example session:
$ cat
Hello, Ubuntu!
Hello, Ubuntu!
This is stdin in action.
This is stdin in action.
^D
$
Standard Output (stdout)
Standard output is where a command sends its normal output. By default, this is displayed on your terminal screen.
Example: Commands Producing stdout
echo "Hello, Ubuntu!"
ls
date
Output example:
$ echo "Hello, Ubuntu!"
Hello, Ubuntu!
$ ls
Documents Downloads Music Pictures Videos
$ date
Thu Mar 13 14:30:45 UTC 2025
Standard Error (stderr)
Standard error is a separate output stream specifically for error messages. By default, it's also displayed on your terminal screen (which can make it look identical to stdout).
Example: Command Producing stderr
ls /nonexistent
Output example:
$ ls /nonexistent
ls: cannot access '/nonexistent': No such file or directory
I/O Redirection
One of the most powerful features of the Ubuntu terminal is the ability to redirect these standard streams.
Redirecting stdout (>)
To redirect stdout to a file instead of the screen:
ls > file_list.txt
This creates a file named file_list.txt
containing the output of the ls
command. If the file already exists, it will be overwritten.
To append output to an existing file, use >>
:
echo "Additional content" >> file_list.txt
Redirecting stderr (2>)
To redirect stderr to a file:
ls /nonexistent 2> errors.txt
The error message is now in the errors.txt
file instead of being displayed on the screen.
Redirecting Both stdout and stderr
To redirect both outputs to different files:
ls /home /nonexistent > output.txt 2> errors.txt
To redirect both to the same file:
ls /home /nonexistent > all_output.txt 2>&1
Alternatively, in Bash, you can use a more modern syntax:
ls /home /nonexistent &> all_output.txt
Redirecting stdin (<
)
To use a file as input instead of the keyboard:
sort < names.txt
This feeds the contents of names.txt
to the sort
command.
Using Pipes (|)
Pipes allow you to connect the stdout of one command to the stdin of another, creating a pipeline of operations.
ls -l | grep "txt"
This command takes the output of ls -l
and sends it to grep
, which filters for lines containing "txt".
Example: Counting Files in a Directory
ls | wc -l
The output of ls
is piped to wc -l
, which counts the number of lines.
Practical Examples
Example 1: Finding and Sorting Large Files
find /home -type f -size +10M -exec ls -lh {} \; | sort -k5hr | head -n 10
This command:
- Finds all files larger than 10MB in the /home directory
- Lists them with detailed information
- Sorts them by size (largest first)
- Shows only the top 10
Example 2: Creating a Log of System Information
{
echo "System Report: $(date)"
echo "-------------------"
echo "Kernel Version:"
uname -a
echo -e "
Disk Space:"
df -h
echo -e "
Memory Usage:"
free -h
} > system_report.txt
This creates a comprehensive system report in a single file.
Example 3: Processing a CSV File
Assuming you have a CSV file with user data:
cat users.csv | grep "active" | cut -d',' -f1,3 > active_users.csv
This extracts only the active users and saves only the 1st and 3rd fields to a new file.
Example 4: Monitoring Log Files in Real-time
tail -f /var/log/syslog | grep ERROR
This command continuously monitors the syslog file for new entries and displays only lines containing "ERROR".
Advanced I/O Concepts
Here Documents (<<
)
A "here document" is a way to provide multiline input to a command:
cat << EOF > multiline.txt
This is line 1
This is line 2
This is the final line
EOF
This creates a file called multiline.txt
with the content between the <<EOF
and the final EOF
.
/dev/null - The Null Device
When you want to discard output completely, redirect it to /dev/null
:
ls /nonexistent 2>/dev/null
This runs the command but discards any error messages.
Process Substitution
Process substitution allows you to use the output of a command where a file name is expected:
diff <(ls dir1) <(ls dir2)
This compares the outputs of ls dir1
and ls dir2
without creating temporary files.
Summary
Understanding input and output in the Ubuntu terminal provides powerful tools for:
- Redirecting program output to files (
>
,>>
) - Redirecting error messages (
2>
) - Using files as input (
<
) - Connecting commands together with pipes (
|
) - Creating complex command sequences for data processing
These I/O manipulation techniques are fundamental to effective shell scripting and command-line usage in Ubuntu.
Exercises
- Create a script that counts the number of words in all text files in a directory.
- Redirect both stdout and stderr from the
find
command to different files. - Use
grep
with pipes to filter the output of theps aux
command for a specific user. - Create a simple logging system that appends date-stamped entries to a log file.
- Use process substitution to compare the sizes of files in two different directories.
Additional Resources
- The Ubuntu manual pages for Bash:
man bash
- Learn more about stream redirection with:
help redirection
(in Bash) - Practice with grep and pipes:
man grep
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)