Understanding Linux Input and Outputs

Linux OS Topics
Post Reply
Ageek
Sergeant
Sergeant
Posts: 22
Joined: Sat Aug 01, 2009 10:54 am

Understanding Linux Input and Outputs

Post by Ageek » Tue Jun 21, 2011 3:19 pm

Understanding Input and Output

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
STDIN -> Standard Input
STDOUT -> Standard terminal Output
STDERR -> Standard terminal Error
Command Output

>
# 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
Redirect the output to a file

Code: Select all

$ find /etc -name passwd >find.out
$ cat find.out
Redirect STDERR to a file

Code: Select all

$ find /etc -name passwd 2>find.err
$ cat find.err
Redirect all input and output to a file

Code: Select all

$ find /etc -name passwd &>find.all
$ cat find.all
Printing the 2012 calender to a file

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
Exersise :

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
Create a file having the following detail in it. Use append to file method
  • 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
Observe the file

Code: Select all

$ less host.stat
Combine output of multiple programs

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
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: Understanding Linux Input and Outputs

Post by Saman » Tue Jun 21, 2011 3:53 pm

UdithaM, Very useful information dude.
It seems that time has come to all ROBOT.LK members to give a start to Linux.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Understanding Linux Input and Outputs

Post by SemiconductorCat » Sun Oct 09, 2011 7:24 pm

This is really a good post.Helped with my problem.
rep added.
Post Reply

Return to “Linux”