Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your standard input (stdin) device, and the screen or a particular terminal window is the standard output (stdout) device.
However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.
What are I/O channels available in Linux
Command OutputSTDIN -> Standard Input
STDOUT -> Standard terminal Output
STDERR -> Standard terminal Error
>
# Redirect stdout to a file.
# Creates the file if not present, otherwise overwrites it.
ls -lR > dir-tree.list
# Creates a file containing a listing of the directory tree.
: > filename
# The > truncates file "filename" to zero length.
# If file not present, creates zero-length file (same effect as 'touch').
# The : serves as a dummy placeholder, producing no output.
> filename
# The > truncates file "filename" to zero length.
# If file not present, creates zero-length file (same effect as 'touch').
# (Same result as ": >", above, but this does not work with some shells.)
>>
# Redirect stdout to a file.
# Creates the file if not present, otherwise appends to it.
1>filename
# Redirect stdout to file "filename."
1>>filename
# Redirect and append stdout to file "filename."
2>filename
# Redirect stderr to file "filename."
2>>filename
# Redirect and append stderr to file "filename."
&>filename
# Redirect both stdout and stderr to file "filename."
Examples:
Observe Errors after executing
Code: Select all
$ find /etc -name passwd
Code: Select all
$ find /etc -name passwd >find.out
$ cat find.out
Code: Select all
$ find /etc -name passwd 2>find.err
$ cat find.err
Code: Select all
$ find /etc -name passwd &>find.all
$ cat find.all
Code: Select all
$ cal 2012 > 2012.cal
$ cat 2012.cal
Taking input from a file sending output to a program
<
# get input from a file
|
# sent output to a program
Examples:
Send an email of 2012 calender to the user root.
Code: Select all
$ mail < 2012.cal -s “2012 calender” root@localhot
# mail -u root
Send an email to root saying “Welcome to the course Linux Essential at UCSC” with the subject “welcome”
Code: Select all
$ echo “Welcome to the course Linux Essential at UCSC” | mail \
-s “Welcome” root@localhost
- Date and time
Disk usage
Memory Usage
Uptime of the machine
Code: Select all
$ echo $(date) >> host.stat
$ echo “######### Disk Usage ##########” >> host.stat
$ df -ah >> host.stat
$ echo “######### Memory Usage ########” >> host.stat
$ free >> host.stat
$ echo “######### Uptime ##########” >> host.stat
$ uptime >> host.stat
Code: Select all
$ less host.stat
Code: Select all
$ (cal 2011 ; cal 2012) | more
Sending multiple line to STDIN
Code: Select all
$ mail -s “Multiple Input lines” root@localhost << END
>Hello root
>This is multiple
>line input to a
>Linux command
>END