How to Create Named Pipes Using the mkfifo Command in Linux

When you need two independent programs to communicate with each other on a Linux server, you typically use a standard, unnamed pipe (the | character). For example, ls -l | grep txt takes the output of the first command and feeds it directly into the second command. However, unnamed pipes only work for commands executed simultaneously in the same terminal session. If you have a background database backup script running in Terminal A, and you want a compression script in Terminal B to read its output in real-time, you must use a “named pipe” created by the mkfifo command.

What is a Named Pipe (FIFO)?

FIFO stands for “First In, First Out.” When you use mkfifo, you create a special type of file that exists physically on the hard drive (you can see it with the ls command). However, this file does not store data on the disk. Instead, it acts as a permanent, physical portal in the filesystem. Any data poured into the portal by Program A is held in the system’s RAM until it is instantly sucked out the other side by Program B.

How to Create and Use a Named Pipe

Creating the pipe requires no special privileges.

  1. Open your terminal and create the pipe by running:
    mkfifo /tmp/my_data_pipe
  2. If you run ls -l /tmp/my_data_pipe, you will see a ‘p’ at the very beginning of the permission string (e.g., prw-rw-r--), confirming it is a pipe, not a text file.

Now, let’s use it for interprocess communication across two different terminal windows.

  1. In Terminal 1 (The Writer): Execute a command and redirect its output into the pipe.
    echo "This is secure data traveling through the pipe." > /tmp/my_data_pipe
    Note: Terminal 1 will freeze and hang. The pipe blocks the writer until a reader arrives.
  2. In Terminal 2 (The Reader): Execute a command that reads from the pipe.
    cat /tmp/my_data_pipe

The exact millisecond you hit Enter in Terminal 2, the data will flow out of the pipe onto the screen, and Terminal 1 will instantly unfreeze. The data was transferred seamlessly between two completely isolated processes without ever touching the physical hard drive.

When you are finished using the pipe, you can delete it exactly like a normal file using the rm command: rm /tmp/my_data_pipe.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.