Ubuntu Tutorials - Herong's Tutorial Examples
∟Shell - The Command-Line Interpreter
∟Command Input/Output Redirection
This section provides a tutorial example on how to use command input and output redirection operators to read input files and save output files.
If you want to save the output of command into a new file,
or take input from an existing file, you need to understand
how Bash handles input and output when executing a command.
When Bash starts a command, it will establish 3 standard
communication channels:
- stdin (Standard Input, file descriptor 0) -
This is the channel for the command to receive input data.
By default, stdin is connected to the keyboard,
allowing you to enter input data.
You can press Ctrl-Z to send an end-of-file signal
in the input channel.
- stdout (Standard Output, file descriptor 1):
This is the channel for the command to write output data.
By default, stdout is connected to the screen,
allowing you to view output data.
- stderr (Standard Error, file descriptor 2):
This is the channel for the command to write error messages.
By default, stderr is also connected to the screen,
allowing you to view error messages.
To help you changing communication channels, Bash provides
the following redirection operators:
- command < file -
The "<" operator redirects stdin to a file,
so the command will read input data from the given file
instead of the keyboard.
- command > file -
The ">" operator redirects stdout to a file,
so the command will write output data to the given file
instead of the screen.
- command 2> file -
The "2>" operator redirects stderr to a file,
so the command will write error messages to the given file
instead of the screen.
- command >> file -
The ">>" operator redirects stdout to a file.
If the file exists already, output will be appended at the end of the file.
- command 2>> file -
The "2>>" operator redirects stderr to a file.
If the file exists already, errors will be appended at the end of the file.
- command &> file -
The "&>" operator redirects both stdout and stderr to a file.
- command &>> file -
The "&>>" operator redirects both stdout and stderr to a file.
If the file exists already, output and errors will be appended at the end of the file.
- command 2>&1 -
The "2>&1" operator redirects stderr to stdout.
- command | command -
The "|" operator redirects stdout of the first command to stdin of the second command.
- command |& command -
The "|&" operator redirects both stdout and stderr of the first command
to stdin of the second command.
Here are some examples of using Bash redirection operators:
# redirect stdout to a file
herong$ grep earlyoom /var/log/syslog > earlyoom.log
# redirect stdin to a file
herong$ grep 'Out of' < earlyoom.log
ubuntu earlyoom[10287]: Out of memory! avail: 22 MiB < min: 362 MiB
ubuntu earlyoom[10287]: Out of memory! avail: 24 MiB < min: 362 MiB
# redirect stdout and stderr to different files
herong$ ping nowhere.com > output.txt 2> error.txt
herong$ cat error.txt
ping: nowhere.com: Name or service not known
# redirect stdout and stderr to the same file
herong$ ping nowhere.com &> output.txt
herong$ cat output.txt
ping: nowhere.com: Name or service not known
# pipe output as input to the next command
herong$ ps -u | grep root | wc -l
185
Table of Contents
About This Book
Introduction to Ubuntu Systems
GNOME - Desktop Interface and Environment
►Shell - The Command-Line Interpreter
What Is Shell
What Is Bash (Bourne Again SHell)
Create and Run Bash Script
Bash Command Line Interpretation Steps
Bash Shell Session Customization
►Command Input/Output Redirection
Shell Session Command History
"tmux" - Terminal Multiplexer
Process Management
Memory Management
Files and Directories
APT (Advanced Package Tool)
Network Connection on Ubuntu
Internet Networking Tools
SSH Protocol and ssh/scp Commands
Administrative Tasks
References
Full Version in PDF/EPUB