Input/output Redirection in Linux
• The
standard input (stdin) device is the keyboard.
• The
standard output (stdout) device is the screen.
Linux, is a very flexible system
and we can change the standard input / output devices. Let's learn how this re-direction works
Output Redirection
The
'>' symbol is used for output (STDOUT) redirection.
Example:
ls -al > listings
Here the output of command ls -al
is re-directed to file "listings" instead of our screen.
Note: Use the correct file name
while redirecting command output to a file. If there is an existing file with
the same name, the redirected command will delete the contents of that file and
then it may be overwritten."
If we do not want a file to be
overwritten but want to add more content to an existing file, then we should
use '>>' operator.
We can re-direct standard output,
to not just files, but also devices!
$ cat music.mp3 > /dev/audio
The cat command reads the file
music.mp3 and sends the output to /dev/audio which is the audio device. If the
sound configurations in our PC are correct, this command will play the file
music.mp3.
Input redirection
The '<' symbol is used for
input(STDIN) redirection
Example: The mail program in Linux
can help us to send e-mails from the Terminal.
we can type the contents of the
email using the standard device keyboard. But if we want to attach a File to
email we can use the input re-direction operator in following format -
Mail -s "Subject"
to-address < Filename
No comments:
Post a Comment