I/O redirection and piping Commands in Linux
I/O Redirection : Redirection can be defined as changing the way from where commands read input to where commands sends output. You can redirect input and output of a command. For redirection, meta characters are used. Redirection can be into a file (shell meta characters are angle brackets '<', '>') or a program ( shell meta characters are pipesymbol '|').
Redirection Into A File :
Each stream uses redirection commands. Single bracket '>' or double bracket '>>' can be used to redirect standard output. If the target file doesn't exist, a new file with the same name will be created.
Overwrite
Commands with a single bracket '>' overwrite existing file content.
- > : standard output
- < : standard input
- 2> : standard error
Writing '1>' or '>' and '0<' or '<' is same thing. But for stderr you have to write '2>'.
Syntax: cat > <fileName>
Example:
cat > v.txt
command "cat > v.txt" has created 'v.txt' with content 'a, b, c'. Same file 'v.txt' is created again with command "cat > v.txt" and this time it overwrites earlier file content and only displays 'd, e, f ,g,h,i'.
Output :

Append :
Commands with a double bracket '>>' do not overwrite the existing file content.
- >> - standard output
- << - standard input
- 2>> - standard error
Syntax: cat >> <fileName>
Example:
cat >> v.txt
Output :
we have created two files with the same name using '>>' in command "cat >> v.txt". But this time, content doesn't overwrite and everything is displayed.


Redirection Into A Program : Pipe redirects a stream from one program to another. Although the functionality of pipe may look similar to that of '>' and '>>' but has a significance difference. Pipe redirects data from one program to another while brackets are only used in redirection of files.
Example:
ls *.txt | cat > textFile
Output : command "ls *.txt | cat > txtFile" has put all the '.txt' files into a newly created file 'textFile'.

Linux Input Redirection
- < stdin:The bash shell uses stdin to take input. In input redirection, a file is made input to the command and this redirection is done with the help of '<' sign.
Syntax: cat < <fileName>
Example:cat < v.txt
Output : command "cat < v.txt" has taken 'v.txt' as input and displayed its content.

-
<< here document :
Syntax: command << any_delimiter
Example:
wc <<hi
Output : wc is for word count and it gives number of lines It is used to find out number of lines, word count, byte and characters count in the arguments and it will be ended when we type "hi" here . After writing "hi" it will end the commands and will given the word count ....

Lets take another example :
ls <<who
It will list all the files and directories after we will enter "who " here who is used as delimiter which act as end of arguments ....

Another example : here we are using cat command .....here we want to display the text of a.txt file .
cat <<Khatam
In the above figure it will not give contents of "a.txt" until we enter "Khatam " here .
- <<< here string :The here string is used to directly pass strings to a command.
Example:
base64 <<< a.txt
Output : Command "base64 <<< a.txt" has decoded file 'a.txt' and then by using command 'base64 -d' we got back our file 'a.txt'.

Linux Output Redirection :Output redirection is used to put output of one command into a file or into another command.
- > stdout : The stdout is redirected with a '>' greater than sign. When shell meets the '>' sign, it will clear the file (as you already know).
Example:
echo Goeduhub_Technologies . > a.txt
Output : greater than sign '>' redirects the command 'echo' output into a file 'a.txt'.

- Output File Is Erased: In output redirection, during scanning of a command line, shell will encounter through '>' sign and will clear the file.
Example:
zcho Welcome!! > a.txt
Output : command "zcho Welcome > a.txt" is wrong but still file 'a.txt' is cleared.

- noclobber :We can prevent file deletion while using '>' sign with the help of noclobber option.
Syntax:
set -o noclobber (To prevent overwrite)
set +o noclobber (To overwrite)
Example:
echo Learn Linux. > a.txt
Output : command "set -o noclobber" prevents file from getting overwrite. But command "set +o noclobber" allows you to overwrite the existing file.

- Overruling noclobber: Overruling noclobber means you can overwrite an existing file while noclobber is set by using '>|' sign.
Syntax: command >| <fileName>
Example:
echo Welcome to goeduhub. >| a.txt
Output : greater than '>' sign, bash doesn't allow to overwrite the file 'a.txt'. But with '>|' sign file is overwritten.

- >>append : Append '>>' sign doesn't let the file content to be overwritten and hence, displays new as well as old file content.
Syntax: command >> <fileName>
Example:
echo Welcome to goeduhub. >> a.txt
Output : file 'a.txt' is not overwritten with append command. New content is displayed with the old one.

For more Rajasthan Technical University CSE-IV Sem Linux Programming Lab Experiments Click Here