Date: 2026apr7
Update: 2026apr10
OS: Linux
Language: bash
Q. Linux: Make a fixed size file of random bytes
A. You might want to do this to reserve some disk space case of an emergency.
There are several ways, here they are ordered by my preference:
1. With fallocate and shred
# Make it
fallocate -l 1G /home/myrandomfile
# Randomize it
shred --iterations=1 /home/myrandomfile
The `fallocate` command uses the fallocate() system call which seems like
the best way of conveying your intentions to the filesystem.
https://man7.org/linux/man-pages/man2/fallocate.2.html
By default `shred` does 3 iterations so we add `--iterations=1` to reduce
the number. This is random enough for our purposes and faster.
2. With truncate and shred
# Make it
truncate --size 1G /home/myrandomfile
# Randomize it (same as above)
shred --iterations=1 /home/myrandomfile
Its safe to bet that the `truncate` command uses the ftruncate() system call.
https://man7.org/linux/man-pages/man2/truncate.2.html
Its been around since 2007 so any distribution will have it by now.
3. With head
head -c 1G /dev/urandom > /home/myrandomfile
Modern `head` copes with binary data.
4. With dd
Use `dd` to read random bytes into a file:
dd if=/dev/urandom of=/home/myrandomfile bs=1M count=(size in megabytes)
For example, to make a 1GiB file:
dd if=/dev/urandom of=/home/myrandomfile bs=1M count=1024
This way is fine, but the syntax isn't so pretty.