Professional Documents
Culture Documents
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
Syntax :
Example :
1. Listing all files and directories and give it as input to
more command.
$ ls -l | more
Output :
$ ls -l -> temp
[contents of temp]
rm temp
Output :
Output of the above two commands is same.
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.
SYNTAX:
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 :
OUTPUT :
3 file1.txt
geek@HP:~$cat file2.txt
OUTPUT:
geeks
for
geeks
3 file1.txt
OUTPUT:
geek@HP:~$15 file1.txt
OUTPUT:
geek@HP:~$15 file1.txt