Table of contents
No headings in the article.
Let's take a scenario where you gave the command and you want to send the given command's output to another command. How would you do that?
Here comes Pipe which means you had given the command that will connect the output of a given command to another command. The symbol for pipe is ( | ).
Syntax :
Command 1 (argument) | Command 2 (argument) | Command 3 (argument) | ...
Let's take an example for better understanding :
Example 1 :
You want to see the output of the last line of "ls -ltr" (this command lists all the directories and files).
$ ls -ltr | tail -1
The "tail -1" command will print the last line of the "ls -ltr" command.
This is the beauty of piping the command with another command.
Example 2 :
I want to sort the data from the file by removing the duplicates. For sorting the data we use "sort" command and for removing the duplicates we use "uniq" command.
$ sort amit | uniq
And here is the output you can see, in one line we had given two commands with the help of pipe (|).
Example 3 :
Let's combine the 4 commands with the help of pipe (|)
1) cat - is used to see the output of any file.
2) grep - is used for finding the particular keyword. I had discussed in "File/Text Processor Commands" blog.
3) tee - is used to see and store the output in the file (both at the same time).
4) wc - used to count the word from the file.
$ cat amit | grep 'DevOps Enthusiast' | tee output | wc
Here the file "output" is created while using tee command. I hadn't created before and written any contents in this file. Let's break this :
Here you can see in first command "amit" there are 6 lines. In second command internally it will print the output of amit file then from the amit file it find the keyword "DevOps Enthusiast". After this tee command will store all the output of previous command in "output" file. Then in final it counts the word,lines and characters from the previous "output" file and prints the counted lines,words,characters. Remember one thing the given last command will print the output and all previous commands in pipe will work internally.
Now if you see the output of "output" file it will show the output of second command.(tee)
I hope you"all get this. Don't worry about "tee" command I'll be covering in next blog. Be with me and follow me for next amazing Linux and DevOps blogs.
THANK YOU :)