First
First
Back
Back
Down
Down
Home
Home
Up
Up
Next
Next
Last
Last
Close
Close
wadhavankar.org

Page No. 26/54
 
Home  >  Tech Notes  >  Operating System  >  Linux  >   Linux Basic (User Level)
 1 Linux Basic
  Understanding Linux Basic
   Shell Commands
    Standard I/O, Redirection, Pipe & tee
    

Linux commands communicate via the CLI. It can print information out to the terminal, and accept input from the keyboard. It has 3 streams (connections) to the outside world viz. input, output & error.

Standard Input : Mainly Keyboard Input, but redirection of one command output to another command; reading from file can also be called Standard I/O

Standard Output : Mainly Terminal Window, but redirection to file can also be called Standard I/O

Standard Error : Initially attached to the command window, but it has separate channel intended for printing error messages.

You can attach one or more of these three streams to a file, a device, or even to another program.

    
Functioncshsh
Send stdout to fileprog > fileprog > file
Send stderr to file prog 2> file
Send stdout and stderr to fileprog >& fileprog > file 2>&1
Take stdin from fileprog < fileprog < file
Send stdout to end of fileprog >> fileprog >> file
Send stderr to end of fileprog 2>> file 
Send stdout and stderr to end of fileprog >>& fileprog >> file 2>&1
Read stdin from keyboard until cprog <prog <
Pipe stdout to prog2prog | prog2prog | prog2
Pipe stdout and stderr to prog2prog |& prog2prog 2>&1 | prog2
 
    The wc program counts lines, characters and words in data sent to its standard input. You can use it interactively like this.
e.g. This is a perfect example of Standard Input
    [gauri@localhost linux]$ wc
This is test for standard I/O
wc means word count.
wc will count lines, words & characters too.

Press Ctrl + d ( ^D) to complete writing
This is last line
^D
      6      31     157
[gauri@localhost linux]$
 
    prog < input > output
e.g. Here you are reading myFile.txt. Input to cat command is give by < & output is stored to newFile.txt with redirection ">".
    [gauri@localhost linux]$ cat < myFile.txt > newFile.txt
 
    prog < input > output 2> errors
e.g. Here you are reading myFile.txt. Input to cat command is give by <, output is stored to newFile.txt with redirection ">" & error (if there is any) will be redirected to new file. This is applicable on bash shell.
    [gauri@localhost linux]$ cat < myFile.txt > newFile.txt 2> error.txt
 
    prog < input > output 2> errors
e.g. Here you are reading myFile.txt. Input to cat command is give by <, output is stored to newFile.txt with redirection ">" & error (if there is any) will be redirected to new file. This is applicable on c shell.
    [gauri@localhost linux]% cat < myFile.txt > newFile.txt >& error.txt
 
First
First
Back
Back
Down
Down
Home
Home
Up
Up
Next
Next
Last
Last
Close
Close
wadhavankar.org

Page No. 26/54