LinuxCommandLibrary

mkfile

Create a file of a specified size

TLDR

Create an empty file of 15 kilobytes

$ mkfile -n [15k] [path/to/file]
copy

Create a file of a given size and unit (bytes, KB, MB, GB)
$ mkfile -n [size][b|k|m|g] [path/to/file]
copy

Create two files of 4 megabytes each
$ mkfile -n [4m] [first_filename] [second_filename]
copy

SYNOPSIS

mkfile [-nv] size filename

PARAMETERS

-n
    Do not pre-allocate blocks. This creates a sparse file where disk blocks are not allocated until data is actually written to them, though the file size is reported as specified.

-v
    Verbose mode. Displays the filename and its size as it is created.

size
    The desired size of the file. This can be an integer followed by a unit suffix (e.g., 'k' for kilobyte, 'm' for megabyte, 'g' for gigabyte, 't' for terabyte). If no suffix is provided, bytes are assumed.

filename
    The name of the file to create.

DESCRIPTION

The mkfile command is used to create a file of a specified size. Unlike commands like touch, which only updates access/modification times or creates an empty file, mkfile pre-allocates disk space for the file and typically fills it with zeros, effectively creating a placeholder file of a specific size. This functionality is often useful for creating swap files, testing disk space allocation, or preparing large files for later content.

While commonly found on Solaris and macOS (Darwin) systems, mkfile is not a standard utility on most GNU/Linux distributions. Linux users typically rely on commands like dd, fallocate, or truncate to achieve similar results. If mkfile is present on a Linux system, it's usually via a compatibility package or a specific distribution's tooling, rather than being part of the standard GNU Coreutils.

CAVEATS

mkfile is primarily a Solaris and macOS command. It is not part of standard GNU Coreutils found on most Linux distributions. Attempting to use mkfile on a typical Linux system will often result in a 'command not found' error. Linux users should use alternatives like dd, fallocate, or truncate for creating fixed-size files.

LINUX ALTERNATIVES

On Linux, there are several robust alternatives to mkfile:

  • dd: For creating a zero-filled file: dd if=/dev/zero of=filename bs=1M count=100 (creates a 100MB file).
  • fallocate: For fast pre-allocation of space: fallocate -l 100M filename (creates and allocates 100MB, often faster than dd).
  • truncate: For changing file size, can extend or shrink: truncate -s 100M filename (can create sparse files if extending).
These commands offer similar or enhanced capabilities to mkfile and are universally available on Linux.

HISTORY

The mkfile utility originated in the Solaris operating system. It was designed to provide a straightforward way to create files of a specific size, particularly useful for tasks like setting up swap space. Its presence on macOS (Darwin) reflects its Unix heritage. However, it was never adopted as a standard utility within the GNU Coreutils suite that forms the basis of most Linux distributions, leading to its relative obscurity and the use of different commands for similar functionality in the Linux ecosystem.

SEE ALSO

dd(1), fallocate(1), truncate(1), touch(1)

Copied to clipboard