what is Input/Output Redirection in Linux ?

Input/output redirection means the text that is showing on your screen while you are running any command(program)in your shell, that information can be redirect elsewhere and even you can store this output in a file or can be print directly.


This function called Redirection, you can also redirect your input of program.

Note : In Linux systems everything consider as a file.

A file descriptor is number that is associated with every file

When you run a program in shell ( i.e when execute a command ) on the back end Linux access 3 special files.

a ) Standard Input               – 0 file descriptor      ( example = mouse, )

b ) Standard Output              – 1 file descriptor      ( example = Screen )

c ) Standard Error Output      – 2 file descriptor      (example = Screen )

So you can redirect these files to other files. If you redirect standard output (Descriptor = 1) to your printer, instead of showing these outputs on your screen you computer start getting print on paper.

1 : Output Redirection

Output Redirection is most commonly used,  when you execute a command it’s normally appears on your terminal . If you extract any tar file you will notice all the output scrolls down rapidly. you can redirect this output in a file for inspecting your output or can send anybody via email. This is called Output Redirection. Using this operator ‘>’ in shell you can redirect your output in a file.

Example :

[root@loadb1 /]# ls > output.txt
[root@loadb1 /]# cat output.txt
[root@loadb1 /]#

Note: If output.txt file is already exit then ‘>’ operator will overwrite the file for append more output into output.txt use ‘>>’ instead of ‘>’.

Linux Commands :

2 : Input Redirection 
You can redirect your input by using ‘<‘ operator. You cannot run input redirection on all programs or commands. you can use only with that programs or commands that accept Input from keyboard.

Example : you are going to send an email and u already have Template of that email. you can put your template in your email body using input redirect.

[root@loadb1 /]# mail ali < mail_template.txt

above command launch email program with mail_template.txt contents.
Now due to advancement in GUI, and also lots of good email clients, method is rarely used.

3 : Error Redirection

Error Redirection is very helpful when u are in trouble. In my case i am trying to open a file that is not readable for my user i will get permission denied errors. i will redirect these error into error.txt file.
Example :

[H.Ali@lx1 /]$ cat ali.txt 2> /home/H.Ali/error.txt
[H.Ali@lx1 /]$ cat /home/H.Ali/error.txt
cat: ali.txt: Permission denied

In the above command 2 is descriptor of error redirection file by typing ‘2>’ you are saying redirect any kind of error to the file error.txt

4 : Pipes ‘ | ‘ in Linux

Linux pipes allow us connect output stream of ‘command a’ to input stream of ‘command b’. here in my case i will pipe cat commands output into less as input.

Example : cat /var/log/messages | less

we can also use pipe for searching strings specially from large text files.

cat /var/log/messages | grep kernal | less

grep is a line searcher it will search lines for specific piece of text.

above command will shearch a keywork ‘kernel’ with grep and then pipe it again to less.

Similar Posts