Create a file filled with zeros
Tested with truncate |
Debian (Squeeze) |
Ubuntu (Lucid, Precise, Trusty) |
Tested with dd |
Debian (Lenny, Squeeze) |
Ubuntu (Lucid, Precise, Trusty) |
Objective
Create a file of a given size in which all bytes are set to zero
Scenario
Suppose you wish to create a regular file called vdrive.img
for use by a virtual machine as a storage device. It is required to be 4 gigabytes in size and the preferred initial content is all bytes set to zero.
Method
One way to create the file is with the truncate
command provided by GNU Coreutils:
truncate -s 4G vdrive.img
Any existing content up to the specified length would be kept, so you may wish to issue a precautionary rm
command if it is important that the file be zeroed:
rm -f vdrive.img
truncate -s 4G vdrive.img
Be aware that truncate
is not a standard POSIX command. It was added to GNU Coreutils as of version 7.0, and was first distributed in:
- CentOS 6
- Debian 5.0 (Squeeze)
- Fedora 11
- OpenSUSE 11.2
- Ubuntu 9.10 (Karmic Koala)
A file created using this method will be a ‘sparse file’ if the filesystem supports it, meaning that storage for any given part of its content will not be reserved until it is first written to. Advantages are that the file can be created very quickly and does not occupy unnecessary disc space. Drawbacks are that the reported amount of free disc space can be misleading (because it does not take account of space that the file may need to occupy in the future), and an ENOSPC
(no space left on device) error can occur when writing to the file even if it is not being extended.
Testing
You can check that the file has the required size and content using the hexdump
command.
hexdump vdrive.img
Repeated lines are omitted from the output by default, so for a file of zeros the output should be very brief:
0000000 0000 0000 0000 0000 0000 0000 0000 0000 * 100000000
Alternatives
Using dd
A more portable solution is to use the dd
command. This can be used to create a non-sparse file:
dd if=/dev/zero of=vdrive.img bs=1024 count=4194304
or an (almost) sparse file if the filesystem supports it:
dd if=/dev/zero of=vdrive.img bs=1024 seek=4194303 count=1
The bs
argument specifies the block size, which has been set to 1 kilobyte in this case. The
count
argument is the number of blocks to be copied, and seek
is the number of blocks to pass over before starting to write. Both count
and seek
are measured in blocks. As previously, you may wish to issue an rm
command before creating a sparse file.
Some sources recommend setting count
to zero. This appears to work using the GNU implementation of
dd
, but the specification for the dd
command does not clearly specify whether the file must be
extended under these circumstances (only that it should seek to the given location, which may or may not cause the file size to change).
The dd
command dates from very early versions of UNIX, and the features used here have been standardised by POSIX.
The existence of /dev/zero
is not required by POSIX, but it is required by the Filesystem Hierarchy Standard and it is provided by most UNIX-like operating systems.
Tags: shell