Linux Bash Shell Tutorial : How to use I/O redirection.5 tips and tricks


  Understanding Input Output or I/O redirection works in Linux helps us streamline our job better when working with the terminal. In this tutorial,I will share quick tips and tricks on how to use I/O redirection in our system.

A few terminology that will help you understand this tutorial is as below:



Standard Input -what is being type in terminal from keyboard.

Standard output -text that display in terminal on command execution
Standard  error – text that display in terminal when a command runs and exit with an error

Content List

1)Redirecting standard output to a file.
2)Redirecting a file to standard input.
3) Redirecting standard error to a file.
4)Redirecting Standard output and error to one file
5) Disposing unwanted output -/dev/null (bit bucket)


1)Redirecting standard output to a file.


Use “>” operator sign to redirect and save output file in current directory .




To append/update existing file use “>>”




2)Redirecting a file to standard input.

Use  “<” operator to redirect an input to the terminal


In above example, I write new content to file stdin and  redirect stdin file to the terminal  using "<" sign operator.

3) Redirecting standard error to a file.

Use “2>” operator sign to redirect standard error to a new file in current directory.


To append/update existing file use “2>>”



4)Redirecting Standard output and error to one file

Use “&>” operator sign to redirect standard output and standard error to a  new file in current directory .


To append/update existing file use “&>>”


file stdouterr
5) Disposing unwanted output -/dev/null (bit bucket)

Use “2> /dev/null” operator sign to redirect unwanted  output to bit bucket. Data  that are redirect to bit bucket will be deleted without a trace. As such some people also call bit bucket as a black hole.



*NOTE:If you execute the same command using  ">" ,"<" ,"2>" ,"&>"  to same file name but with different content, the file will be overwritten with new file content .

Previous
Next Post »