EDIT: The simplest way is probably the truncate
of Ashutosh Vishwa Bandhu's answer, but as pointed out by @offby1 that creates sparse files which may not be what you want. The solutions below create normal, full files.
The following commands create a 14MB file called foo
:
fallocate
(thanks to @Breakthrough who suggested it in the comments and vote up Ahmed Masud's answer below which also mentions it.)
fallocate -l 14000000 foo
This command is particularly impressive since it is as fast as truncate
(instantaneous) irrespective of the desired file size (unlike the other solutions which will be slow for large files) and yet creates normal files, not sparse ones :
$ truncate -s 14MB foo1.txt
$ fallocate -l 14000000 foo2.txt
$ ls -ls foo?.txt
0 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:54 foo1.txt
13672 -rw-r--r-- 1 terdon terdon 14000000 Jun 21 03:55 foo2.txt
Create a file filled with random data
dd if=/dev/urandom of=foo bs=14MB count=1
or
head -c 14MB /dev/urandom > foo
Create a file filled with \0
s:
dd if=/dev/zero of=foo.txt bs=14MB count=1
or
head -c 14MB /dev/zero > foo
Create a file filled with the first 14MB of data of another file:
head -c 14MB bar.txt > foo
Create a file filled with the last 14MB of data of another file:
tail -c 14MB bar.txt > foo
In all of the above examples, the file will be 14*1000*1000
if you want 14*1024*1024
, replace MB
with M
. For example:
dd if=/dev/urandom of=foo bs=14M count=1
head -c 14M /dev/zero > foo
fallocate
only deals in bytes, so you'd have to do (14*1024*1024=14680064)
fallocate -l 14680064 foo