create empty file

>file.txt, :>file.txt, :>>file.txt

cat /dev/null > file1.ext
cp /dev/null file.txt

echo -n >file.txt
echo -n ” >file.txt
echo “” >file.txt #creates a file containing a newline

printf ” > file.txt

dd if=/dev/zero of=file.txt count=0 bs=1

If you like to create a text file with random data, then you can use something like this

dd status=progress if=/dev/zero of=/tmp/file.zer0 bs=10M count=10
dd status=progress if=/dev/urandom of=/tmp/file1.rand bs=10M count=10

head -c 10M /dev/urandom > myfile

/dev/null produces no output.
/dev/zero produces a continuous stream of NULL (zero value) bytes.

You can see the difference by executing cat /dev/null and cat /dev/zero.
Try cat /dev/null > file and you will find an empty file.
Now try cat /dev/zero > file, while watching the size of the file (watch -n 1 du -h file) continuously increase. This is because reading from /dev/zero gives an endless stream of \0 (null) characters.

Use dd to visualize the difference more appropriately:

$ dd if=/dev/null of=file count=10
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000276193 s, 0.0 kB/s

$ dd if=/dev/zero of=file count=10
10+0 records in
10+0 records out
5120 bytes (5.1 kB) copied, 0.00090775 s, 5.6 MB/s

/dev/zero is used to create dummy files or swap.

Posted in: net