I know how to create an empty file:
but how can I create a 1MB file that only contains nullbytes on the commandline with bash?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
truncate -s 1M nullbytes
nullbytes didn't exist beforehand) would create a 1 mebibyte sparse file. That is a file that appears filled with zeros but that doesn't take any space on disk.
truncate, you can use
dd bs=1048576 seek=1 of=nullbytes count=0
dd implementations, you can replace 1048576 with
If you'd rather the disk space be allocated, on Linux and some filesystems, you could do:
fallocate -l 1M nullbytes
That allocates the space without actually writing data to the disk (the space is reserved but marked as uninitialised).
dd < /dev/zero bs=1048576 count=1 > nullbytes
Will actually write the zeros to disk. That is the least efficient, but if you need your drives to spin when accessing that file, that's the one you'll want to go for.
Or @mikeserv's way to trick
dd into generating the NUL bytes:
dd bs=1048576 count=1 conv=sync,noerror 0> /dev/null > nullbytes
An alternative with GNU
head that doesn't involve having to specify a block size (1M is OK, but 10G for instance wouldn't):
head -c 1M < /dev/zero > nullbytes
Or to get a progress bar:
pv -Ss 1M < /dev/zero > nullbytes