Skip to content

mkfifo command in Linux

The mkfifo command in Linux is used to create named pipes, which are special types of files that allow inter-process communication. With mkfifo, you can establish communication channels between different processes for seamless data transfer. This command is particularly useful in shell scripting and when working with multiple programs that need to pass data to each other efficiently. Named pipes created with mkfifo can help streamline your workflow and improve the overall efficiency of your system.

mkfifo Syntax:

Terminal window
mkfifo [option] [parameter]

Linux mkfifo Options:

OptionDescription
-mSet the permission mode of the fifo
-ZSet the SELinux security context

mkfifo Parameters:

ParameterDescription
pathSpecify the path for the fifo to be created

How to use mkfifo command:

Create a named pipe

Terminal window
mkfifo mypipe

Creates a named pipe called “mypipe”.

Check file type

Terminal window
file mypipe

Displays the file type of “mypipe” which should show “fifo”.

Read from a named pipe

Terminal window
cat mypipe

Reads data from the named pipe “mypipe”.

Write to a named pipe

Terminal window
echo "Hello, named pipe!" > mypipe

Writes the text “Hello, named pipe!” to the named pipe.

Pipe data between commands

Terminal window
mkfifo pipe

Creates a named pipe called “pipe” for piping data between commands.

Use named pipe for inter-process communication

Terminal window
mkfifo communication_pipe

Creates a named pipe “communication_pipe” for inter-process communication.

Redirect output to a named pipe

Terminal window
ls -l > my_data_pipe

Redirects the output of the “ls -l” command to a named pipe called “my_data_pipe”.

Remove a named pipe

Terminal window
rm mypipe

Removes the named pipe “mypipe” from the filesystem.

How do I use mkfifo in Linux?

To use the mkfifo command in Linux, execute the following command:

Terminal window
mkfifo /path/to/named_pipe

How can I create multiple named pipes with mkfifo?

You can create multiple named pipes at once using the mkfifo command by providing the names of the pipes as arguments. For example:

Terminal window
mkfifo pipe1 pipe2 pipe3

How can I check the permissions of a named pipe created with mkfifo?

You can use the ls command with the -l option to view the permissions of a named pipe created with mkfifo. For example:

Terminal window
ls -l /path/to/named_pipe

Can mkfifo be used to create FIFOs with specific permissions?

Yes, you can specify the permissions for the named pipe created with mkfifo using the chmod command. For example:

Terminal window
mkfifo /path/to/named_pipe
chmod 777 /path/to/named_pipe

How can I remove a named pipe created with mkfifo?

To remove a named pipe created with mkfifo, you can use the rm command. For example:

Terminal window
rm /path/to/named_pipe

Can I use mkfifo to create named pipes in a specific directory?

Yes, you can specify the directory where you want to create the named pipe using the mkfifo command. For example:

Terminal window
mkfifo /path/to/directory/named_pipe

How can I read from a named pipe created with mkfifo?

You can read from a named pipe created with mkfifo using standard file I/O operations in bash scripts or other programming languages. For example:

Terminal window
cat /path/to/named_pipe

Is it possible to write data to a named pipe using mkfifo?

Yes, you can write data to a named pipe created with mkfifo using standard file I/O operations in bash scripts or other programming languages. For example:

Terminal window
echo "Data to write" > /path/to/named_pipe

Can a named pipe created with mkfifo be used for inter-process communication?

Yes, named pipes created with mkfifo can be used for inter-process communication by enabling communication between multiple processes through reading and writing to the named pipe.

Applications of the mkfifo command

  • Creating named pipes for inter-process communication
  • Setting up communication channels between processes
  • Implementing communication mechanisms in shell scripts
  • Providing a method for one-way communication between processes