Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
in RTU/BTU B.Tech (CSE-IV Sem) Linux Programming Lab by Goeduhub's Expert (7.6k points)

I/O redirection and piping Commands in Linux

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-


Course Name



Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon


Natural Language Processing-NLP with Deep Learning in Python Apply Coupon


Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
Best answer

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.


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>  


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>  


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.


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 :
    • A here document is used to redirect input into an interactive shell script or program. We can run an interactive program within a shell script without user action by supplying the required input for the interactive program, or interactive shell script.

    Syntax: command << any_delimiter 


    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 ....

    here doc

    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 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.


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).


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.


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.


set -o noclobber    (To prevent overwrite)  

set +o noclobber    (To overwrite)  


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>  


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>  


 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

3.3k questions

7.1k answers


4.6k users


About Us | Contact Us || Terms & Conditions | Privacy Policy || Youtube Channel || Telegram Channel © Social::   |  |