102

I'd like to generate a file with the name example.file. I could use

touch example.file

but I want the file to be exactly 24MB in size. I already checked the manpage of touch, but there is no parameter like this. Is there an easy way to generate files of a certain size?

132

You can use dd:

dd if=/dev/zero of=output.dat  bs=24M  count=1

or

dd if=/dev/zero of=output.dat  bs=1M  count=24

or, on Mac,

dd if=/dev/zero of=output.dat  bs=1m  count=24
  • 2
    ... or use bs=1M and count=24. Many find it nicer and easier to read. – Bgs Nov 15 '13 at 20:11
  • 3
    Maybe dont use huge block sizes, my system did not like bs=1G count=1 – ThorSummoner Dec 7 '16 at 0:17
  • 10
    On the Mac, use 24m (small m), because the Mac doesn't like the big M. dd if=/dev/zero of=output.dat bs=24m count=1 – SPRBRN Feb 13 '17 at 13:30
  • 1
    On Android (5.1.1, might (not) be version or phone specific) I had to use a lowercase m too. – Erik Apr 12 '18 at 11:56
  • 1
    What is the problem with large block sizes specifically? I had hoped to use a 1G block size for a 1G file. – felwithe Jan 5 at 15:03
42

Under non-embedded Linux or Cygwin (or any system with GNU coreutils) and FreeBSD:

truncate -s 24m example.file

This creates a file full of null bytes. If the file already exists and is smaller, it is extended to the requested size with null bytes. If the file already exists and is larger, is is truncated to the requested size.

The null bytes do not consume any disk space, the file is a sparse file.

On many systems, head -c 24m </dev/zero >example.file creates a non-sparse file full of null bytes. If head doesn't have a -c option on your system (it's common but not in POSIX), you can use dd bs=1024k count=24 </dev/zero >example.file instead (this is POSIX-compliant).

24

If you don't care about the content of the file, this is much faster than using dd:

fallocate -l 24M filename

Obviously, using dd for a 24MB file won't take any time on a modern system, but larger files can be noticeably slow.

13

You can use dd:

dd if=/dev/zero of=outputfile.out bs=1024k count=24

Or in case you happen to be using Solaris

mkfile 24m outputfile.out
  • 3
    mkfile seems to be present on macOS too – Ben Flynn Sep 19 '16 at 20:28
  • You can also pass -n to create a sparse file – russbishop Dec 8 '17 at 17:29
0
FROM_NODE=N01;
echo; cd $MOUNT_PATH; pwd; ls -la; sleep 1; echo;
WHEN="$(date +%Y-%m-%d_%H-%M-%S)";
fallocate -l 10M $MOUNT_PATH/"$FROM_NODE"_"$WHEN".dump
ls -lha; echo;
  • 1
    Doesn't this say 10 megs while the Q asks for 24? – Jeff Schaller Jan 19 '17 at 20:10
  • 2
    fallocate could be a good answer, but why all the other lines? Cut your answer down so that it does just what was asked, and nothing more. – JigglyNaga Jan 19 '17 at 20:41
  • 1
    If you prefer sir: fallocate -l 10M somefile.dump – Pascal Andy Feb 5 '17 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.