Files and folders

The mkfifo command

Creates a named pipe (FIFO): a special file through which two programs pass data without touching the disk. What one process writes, another reads.

Syntax

mkfifo [-m modo] nombre...Try it in the terminal →

Most useful options

-m <modo>Permissions of the pipe, in octal or symbolic form. Without -m, the umask applies (666 minus the umask)

Copy-ready examples

$ mkfifo canalcreates the pipe; ls -l shows a p at the start of the permissions.

Example output

prw-r--r-- 1 miguel miguel 0 nov  3 11:30 canal
$ mkfifo -m 600 secretocreates the pipe for your user only, useful when it will carry sensitive data.
$ cat canalin one terminal, waits for someone to write to the pipe and prints whatever arrives.
$ echo 'hola' > canalin another terminal, writes to the pipe: the text shows up in the first one. Until there is a reader, it waits.
$ mkfifo tuberia; gzip < tuberia > salida.gz & cat datos.txt > tuberiacompresses datos.txt through a named pipe, with no intermediate file.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
mkfifo: cannot create fifo 'existing': File existsThe name already exists, whether or not it is a pipe. mkfifo never overwrites.Check what it is with ls -l. If it is not yours, pick another name; if it is yours and no longer needed, remove it with rm.
mkfifo: cannot create fifo 'noexiste/canal': No such file or directoryThe folder where you want to create the pipe does not exist.Create the folder first with mkdir -p, or fix the path.
mkfifo: missing operandYou gave no name for the pipe.Give the name: mkfifo canal.

Typical mistakes

  • A FIFO stores no data: if nobody reads, the writer stays blocked. That is why the reader has to be running first.
  • rm removes the pipe, and no data is lost with it, because it was never on disk.

Differences from similar commands

  • ln: ln -s creates a link to something that already exists; mkfifo creates a new, empty object.
  • mknod: can also create pipes (mknod name p), with more options and more risk when used for devices.
  • |: anonymous pipe: joins two commands with no name on disk. mkfifo creates a named, visible pipe.

Security

A pipe with open permissions, such as with umask 000, can be read or written by other users: someone could read your data or inject theirs. Create your own with -m 600.

Related commands

FAQ

Where is the data of a FIFO stored?

Nowhere on disk: it passes from one process to another in memory. The file only serves as a meeting point.

Why does my program hang when writing to the pipe?

Because no reader is open at the other end. A FIFO blocks the writer until someone reads. Start the reader first.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course