You are on page 1of 16

How to do input/output redirection in Linux

Khizar Hayat Saani


Input/Output (I/O) redirection in Linux refers to the
ability of the Linux operating system that allows us to
change the standard input (stdin) and standard output
(stdout) when executing a command on the terminal.
By default, the standard input device is your keyboard
and the standard output device is your screen.
I/O Redirection

Output redirection
We can overwrite the standard output using the ‘>’
symbol.
The right operand is set as the standard output.
In the above example, we first run the ls command to
list the files and directories in the root directory onto
the standard output.
Then, the same command is run again. However, this
time we use the ‘>’ symbol to redirect the standard
output to file.txt.
Using the cat command, we output the content
of file.txt. Notice that the file contains the output of the
previously executed ls command.

Input redirection
We can overwrite the standard input using the ‘<’
symbol.
The right operand is set as the standard input.
In this example, we create a file (file.txt) using
the touch command.
The echo command echoes strings onto the standard
output. Using output redirection, we redirect
the echo command’s output to file.txt. We can see
that file.txt now contains a single row with the
data hello world.
The wc -l command returns the number of rows in a file
followed by the name of the file. By default, the
command takes the name of the file from the standard
input.
Using the ‘<’ symbol, we redirect the standard input
to file.txt.
We get 1 as our output. However, the name of the file is
not printed in this case. This happens because the
command assumes that it is taking its input
from stdin rather than a file. Hence, the name of the file
is not printed.
Piping in Unix or Linux

A pipe is a form of redirection (transfer of standard


output to some other destination) that is used in Linux
and other Unix-like operating systems to send the
output of one command/program/process to another
command/program/process for further processing. The
Unix/Linux systems allow stdout of a command to be
connected to stdin of another command. You can make
it do so by using the pipe character ‘|’. 
Pipe is used to combine two or more commands, and in
this, the output of one command acts as input to
another command, and this command’s output may act
as input to the next command and so on. It can also be
visualized as a temporary connection between two or
more commands/ programs/ processes. The command
line programs that do the further processing are
referred to as filters. 

This direct connection between commands/ programs/


processes allows them to operate simultaneously and
permits data to be transferred between them
continuously rather than having to pass it through
temporary text files or through the display screen. 
Pipes are unidirectional i.e data flows from left to right
through the pipeline. 

Syntax : 
 

command_1 | command_2 | command_3 | .... |


command_N

Example : 
1. Listing all files and directories and give it as input to
more command. 

 
$ ls -l | more

Output : 
 

The more command takes the output of $ ls -l as its


input. The net effect of this command is that the output
of ls -l is displayed one screen at a time. The pipe acts as
a container which takes the output of ls -l and gives it to
more as input. This command does not use a disk to
connect standard output of ls -l to the standard input of
more because pipe is implemented in the main
memory. 
In terms of I/O redirection operators, the above
command is equivalent to the following command
sequence. 
 

$ ls -l -> temp

more -> temp (or more temp)

[contents of temp]

rm temp

Output : 
 
Output of the above two commands is same. 

2. Use sort and uniq command to sort a file and print


unique values. 

$ sort record.txt | uniq

This will sort the given file and print the unique values
only. 
Output : 
 
3. Use head and tail to print lines in a particular range
in a file. 

$ cat sample2.txt | head -7 | tail -5

This command select first 7 lines through (head -7)


command and that will be input to (tail -5) command
which will finally print last 5 lines from that 7 lines. 
Output : 
 
4. Use ls and find to list and print all lines matching a
particular pattern in matching files. 

$ ls -l | find ./ -type f -name "*.txt" -exec grep "program"


{} \;

This command select files with .txt extension in the


given directory and search for pattern like “program” in
the above example and print those in which have
program in them. 
Output : 
 
5. Use cat, grep, tee and wc command to read the
particular entry from user and store in a file and print
line count. 
tee command in Linux with examples

tee command reads the standard input and writes it to


both the standard output and one or more files. The
command is named after the T-splitter used in plumbing.
It basically breaks the output of a program so that it can
be both displayed and saved in a file. It does both the
tasks simultaneously, copies the result into the specified
files or variables and also display the result.

SYNTAX:

tee [OPTION]... [FILE]...

Options :
1.-a Option : It basically do not overwrite the file but
append to the given file.
Suppose we have file1.txt

Input: geek
for

geeks

and file2.txt

Input:geeks

for

geeks

SYNTAX :

geek@HP:~$ wc -l file1.txt|tee -a file2.txt

OUTPUT :

3 file1.txt

geek@HP:~$cat file2.txt

OUTPUT:

geeks
for

geeks

3 file1.txt

2.–help Option : It gives the help message and exit.


SYNTAX :

geek@HP:~$ tee --help


3.–version Option : It gives the version information and
exit.
SYNTAX :

geek@HP:~$ tee --version


Application

Suppose we want to count number of characters in our


file and also want to save the output to new text file so
to do both activities at same time, we use tee command.

geek@HP:~$ wc -l file1.txt| tee file2.txt

OUTPUT:

geek@HP:~$15 file1.txt

Here we have file1 with 15 characters, so the output will


be 15 and the output will be stored to file2. In order to
check the output we use :

geek@HP:~$ cat file2.txt

OUTPUT:

geek@HP:~$15 file1.txt

You might also like