I know how to create an empty file:
touch /var/tmp/nullbytes
but how can I create a 1MB file that only contains nullbytes on the commandline with bash?
command linefiles
I know how to create an empty file:
touch /var/tmp/nullbytes
but how can I create a 1MB file that only contains nullbytes on the commandline with bash?
Best Answer
With GNU
truncate
:(assuming
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.Without
truncate
, you can usedd
instead:(with some
dd
implementations, you can replace 1048576 with1M
)If you'd rather the disk space be allocated, on Linux and some filesystems, you could do:
That allocates the space without actually writing data to the disk (the space is reserved but marked as uninitialised).
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:An alternative with GNU
head
that doesn't involve having to specify a block size (1M is OK, but 10G for instance wouldn't):Or to get a progress bar: