Pipes and Redirection in Linux


Introduction

Pipes and redirection are two different kinds of mechanisms used in Linux. Sometimes, we need the output of a command to be passed as input of another command and do some operation. There we use the pipe operator. The operator is “|”. It’s found on top of the “Enter” key. Sometimes, we redirect or pass all the output of a command to a file for storing purposes. Also, we take a file’s content as input for a command. This is called redirection and operators are used like “>”, “>>” and “<”.

In this article, we will learn how to use pipe and redirection in Linux.

Example 1: Redirecting to a New File

When we run any command in a Linux terminal, the output will be printed on the screen. If we want to send the output of a command, we can use “>” to do this operation.

In the first example, we will the see output in the terminal.

$ ls -lhrt
total 16K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Now, after using “>”, there will not be any output in the terminal and output will be stored inside “redirection.txt”.

$ ls -lhrt > redirection.txt

$ cat redirection.txt
total 16K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
-rw-rw-r-- 1 rian rian    0 Feb 11 22:43 redirection.txt
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Example 2: Redirecting to an Existing File

In last example we have redirected the output in new file. We can redirect the output to an existing file also. But there is one problem. If we use “>” to redirect to an existing file then old content of the existing file will be gone.

$ cat 123.txt
123

$ ls -lhrt > 123.txt

$ cat 123.txt
total 20K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
-rw-rw-r-- 1 rian rian  269 Feb 11 22:43 redirection.txt
-rw-rw-r-- 1 rian rian    0 Feb 11 22:50 123.txt
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Now we can see old content of file 123.txt is gone. In that case, we have to use “>>” to keep the old content and append the redirected content.

$ cat 123.txt
123

$ ls -lhrt >> 123.txt

$ cat 123.txt
123
total 24K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
-rw-rw-r-- 1 rian rian  269 Feb 11 22:43 redirection.txt
-rw-rw-r-- 1 rian rian    4 Feb 11 22:53 123.txt
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Example 3: Redirecting From an Existing

We can also redirect from an existing file and give input to any command. Let us see the below example. Here, we are searching for “123” using the “grep” command from the content of dile “123.txt”

$ grep -i "123" < 123.txt
123
-rw-rw-r-- 1 rian rian    4 Feb 11 22:53 123.txt

Example 4: Redirecting STDERR

We can redirect only the error to the file using “2” before “>”

$ ls nofile.txt
ls: cannot access 'nofile.txt': No such file or directory

$ ls nofile.txt 2> error.txt

$ cat error.txt
ls: cannot access 'nofile.txt': No such file or directory

One limitation is, “error.txt” will have only STDERR. If we want to get STDOUT and STDERR in one file then we have to use the below command. Let us see the steps.

$ ls nofile.txt bash/
ls: cannot access 'nofile.txt': No such file or directory
bash/:
bash_function.sh  bash_pass_arg.sh  bash_return.sh  bash_return_usingglobal.sh  return3.sh

$ ls nofile.txt bash/ 2> error.txt
bash/:
bash_function.sh  bash_pass_arg.sh  bash_return.sh  bash_return_usingglobal.sh  return3.sh

$ cat error.txt
ls: cannot access 'nofile.txt': No such file or directory

$ ls nofile.txt bash > error.txt 2>&1

$ cat error.txt
ls: cannot access 'nofile.txt': No such file or directory
bash:
bash_function.sh
bash_pass_arg.sh
bash_return.sh
bash_return_usingglobal.sh
return3.sh

Example 1: Use of Single and Multiple Pipe

We can pass the output of one command to the following command to do some operation and get the desired output. Let us see below a simple example.

$ ls
123.txt  bash  cat-more-less  cmd-bash  error.txt  redirection.txt  zip-unzip

$ ls | grep -i "bash"
bash
cmd-bash

As we can see the command flows from left to right.

Multiple pipes can be used in one line to get the required output.

$ ls | grep -i "bash" | wc -l
2

Conclusion

From this article, we have learned how and where to use PIPE and REDIRECTION in Linux.

We have learned below operators.

“>” , “>>”, “<”, “|”

These commands help us to do faster operation and get the expected output. So, based on the need we should use the appropriate command in Linux.

Updated on: 29-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements