What is the use of pipes and named pipes in Linux
Xiaobian to share with you what is the use of pipes and named pipes in Linux, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
In Linux, pipe lets you send the output of one command to another. Pipelines, as their name suggests, redirect standard outputs, inputs, and errors from one process to another for further processing.
The syntax for the Pipe (or Unnamed Pipe) command is to place a line between two commands| Character:
Command-1 | Command-2 | ...| Command-N
Here, the pipe cannot be accessed through another session; it is temporarily created to receive execution of Command-1 and redirect standard output. It is deleted after successful execution.
In the example above, contents.txt contains a list of all files in a particular directory--specifically, the output of the ls -al command. We first grep the file name from contents.txt using the "file" keyword through the pipeline (as shown), so the output of the cat command is provided as input to the grep command. Next, we add a pipe to execute awk, which displays column 9 in the filtered output of grep. We can also count the number of rows in contents.txt using wc -l.
Named pipes can continue to be used as long as the system is up and running or until it is deleted. It is a special file that follows FIFO (first in, first out) mechanism. It can be used just like a regular file. That is, you can write to it, read from it, and turn it on or off. To create a named pipe, the command is:
mkfifo
This creates a named pipe file that can even be used in multiple shell sessions.
Another way to create FIFO named pipes is to use this command:
mknod p
To redirect the standard output of any command to another command, use the> symbol. To redirect standard input for any command, use