Is there a directory equivalent of /dev/null in Linux?

Solution 1:

The FHS provides no "standard" empty directory.

It is common for Linux systems to provide a directory /var/empty, but this directory is not defined in FHS and may not actually be empty. Instead, certain daemons will create their own empty directories in here. For instance, openssh uses the empty directory /var/empty/sshd for privilege separation.

If your need for an empty directory is transient, you can create an empty directory yourself, as a subdirectory of /run or /tmp. If you're doing this outside the program, you can use mktemp -d for this, or use the mkdtemp(3) C function inside your program. Though if you always need the empty directory to be present, consider creating one under /var/empty as openssh does.

For this use case, creating a directory under /tmp is probably the best fit, though in practice it doesn't matter very much where you put it.

Solution 2:

You can use mktemp -d to create a new empty temporary directory with secure permissions, by default in /tmp/. The utility will output the new directory's path on STDOUT, so it is useful in the shell.

It's more portable than a systemd unit file anyway.


Solution 3:

This Unix question has some suggestions for creating a "blackhole" directory, including a nullfs FUSE filesystem.


Solution 4:

For services systemd provides the option PrivateTmp to create private /tmp and /var/tmp directories that are not shared by processes outside of the namespace for that service and which should be empty (initially).

[Service]
ExecStart=...
PrivateTmp=yes 

Tags:

Linux

Fhs