Create empty file

Empty files can be used as a mean to pass information between programs in a persistent manner, or a form of configuration. Empty files can be used on web servers to declutter 404 logs of endless bot scans.

For the examples below, we assume relative filename “path/to/empty”, where directories “path/to” already exist. We assume that a file does NOT exist at the filename, and it may be overwritten if it exists.

NOTE: For simplicity, some of these examples overwrite existing files, but some do not overwrite. Extra code is required to give a consistent behavior. All examples at least create an empty file if one doesn’t exist.

Windows

Windows users often use Command Prompt or PowerShell.

Command Prompt

Create empty file with Command Prompt:

copy NUL path\to\empty

PowerShell

Create empty file with PowerShell (on any operating system):

New-Item path/to/empty

Linux / MacOS / Unix

Most shells create empty file like:

touch path/to/empty

Python

from pathlib import Path

Path('path/to/empty').touch()

Fortran

program empty

implicit none

integer :: u

open(newunit=u, file='path/to/empty')
close(u)

end program

C++

Note: you should add code to check that the file was created. This simple example doesn’t fail even if file not created.

#include <fstream>

int main(){

std::ofstream output("path/to/empty");

return EXIT_SUCCESS;
}