Another special device we can use is /dev/zero. When we redirect output to /dev/zero, it does exactly the same as /dev/null: the data disappears. However, in practice, /dev/null is most often used for this purpose.
So, why have this special device then? Because /dev/zero can also be used to read null bytes. Out of all possible 256 bytes, the null byte is the first: the hexadecimal 00. A null byte is often used to signify the termination of a command, for example.
Now, we can also use these empty bytes to allocate bytes to the disk:
reader@ubuntu:/tmp$ ls -l
-rw-rw-r-- 1 reader reader 48 Nov 6 19:26 output
reader@ubuntu:/tmp$ head -c 1024 /dev/zero > allocated-file
reader@ubuntu:/tmp$ ls -l
-rw-rw-r-- 1 reader reader 1024 Nov 6 20:09 allocated-file
-rw-rw-r-- 1 reader reader 48 Nov 6 19:26 output
reader@ubuntu:/tmp$ cat allocated-file
reader@ubuntu:/tmp$
By using head -c 1024, we specify we want the first 1024 characters from /dev/zero. Because /dev/zero only supplies null bytes, these will all be the same, but we know for sure that there will be 1024 of them.
We redirect those to a file using stdout redirection, and we then see a file with a size of 1024 bytes (how surprising). Now, if we cat this file, we see nothing! Again, this should not be a surprise, because null bytes are exactly that: empty, void, null. The Terminal has no way of representing them, so it does not.
Should you ever need to do this in a script, there is another option for you: fallocate:
reader@ubuntu:/tmp$ fallocate --length 1024 fallocated-file
reader@ubuntu:/tmp$ ls -l
-rw-rw-r-- 1 reader reader 1024 Nov 6 20:09 allocated-file
-rw-rw-r-- 1 reader reader 1024 Nov 6 20:13 fallocated-file
-rw-rw-r-- 1 reader reader 48 Nov 6 19:26 output
reader@ubuntu:/tmp$ cat fallocated-file
reader@ubuntu:/tmp$
As you can see from the preceding output, this command does exactly what we already accomplished with our /dev/zero read and redirection (we wouldn't be surprised if fallocate was actually a fancy wrapper around reading from /dev/zero, but we can't say this for sure).