165

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?

2

6 Answers 6

198

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
8
  • 2
    ... or use bs=1M and count=24. Many find it nicer and easier to read.
    – Bgs
    Nov 15, 2013 at 20:11
  • 6
    Maybe dont use huge block sizes, my system did not like bs=1G count=1 Dec 7, 2016 at 0:17
  • 14
    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, 2017 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, 2018 at 11:56
  • 3
    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, 2019 at 15:03
63

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).

5
  • BusyBox also has it, so most embedded systems will too :-) Oct 3, 2018 at 1:46
  • Note that this doesn’t create a 24 MB file, but a 24 MiB one (=~ 25,2 MB) – for a 24 MB file use -s 24MB instead.
    – dessert
    Nov 14, 2019 at 12:52
  • @dessert The asker probably wanted 24MiB. Most people don't use the binary prefixes. Nov 14, 2019 at 13:00
  • You really think so? It’s exactly the other way around in my experience – still, don’t you agree that on a question about a certain file size an answer should at least mention the difference? Maybe I know the wrong type of people, but they constantly get confused about it and wonder why their 500 GiB of data doesn’t fit on their 500 GB hard drive.
    – dessert
    Nov 14, 2019 at 13:30
  • My intuitition is that this is the fastest alternative, especially as the file size becomes large.
    – Jim L.
    Nov 16, 2021 at 21:19
43

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.

4
  • Note that this doesn’t create a 24 MB file, but a 24 MiB one (=~ 25,2 MB) – for a 24 MB file use -l 24MB instead.
    – dessert
    Nov 14, 2019 at 12:54
  • 3
    This should be the top answer. Imagine you needed to allocate a 10 GB file, totally unnecessary to fill it with zeros as suggested in the other replies. Jun 8, 2020 at 7:44
  • 2
    Note that fallocate is not supported on multiple filesystems including ext3
    – Azatik1000
    Oct 8, 2020 at 20:47
  • thx @Azatik1000, seems to not work on zfs. Although it sounds like the best answer :-/
    – MoRe
    Apr 16, 2021 at 8:21
14

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
  • 3
    mkfile seems to be present on macOS too
    – Ben Flynn
    Sep 19, 2016 at 20:28
  • You can also pass -n to create a sparse file
    – russbishop
    Dec 8, 2017 at 17:29
  • 1
    Note that this doesn’t create a 24 MB file, but a 24 MiB one (=~ 25,2 MB).
    – dessert
    Nov 14, 2019 at 12:55
0

refer other summary:

  • truncate
    • truncate -s 24M example.file
  • fallocate
    • fallocate -l $((24*1024*1024)) example.file
  • head
    • random data
      • head -c 24MB /dev/urandom > example.file
    • zero data
      • head -c 24MB /dev/zero > example.file
  • dd
    • dd if=/dev/urandom of=example.file bs=24MB count=1
      • dd if=/dev/urandom of=example.file bs=4MB count=6
-1
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;
3
  • 1
    Doesn't this say 10 megs while the Q asks for 24?
    – Jeff Schaller
    Jan 19, 2017 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, 2017 at 20:41
  • 1
    If you prefer sir: fallocate -l 10M somefile.dump Feb 5, 2017 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.