199

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?

R_User
  • 2,093
  • 2
  • 13
  • 5

6 Answers6

239

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
Clay
  • 103
Paul92
  • 2,590
  • 1
  • 14
  • 11
  • 2
    ... or use bs=1M and count=24. Many find it nicer and easier to read. – Bgs Nov 15 '13 at 20:11
  • Thank you for your observations. I edited the answer accordingly. – Paul92 Nov 15 '13 at 20:22
  • 7
    Maybe dont use huge block sizes, my system did not like bs=1G count=1 – ThorSummoner Dec 07 '16 at 00: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 '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
  • 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 05 '19 at 15:03
  • 1
    Note that this doesn’t create a 24 MB file, but a 24 MiB one (=~ 25,2 MB) – for a 24 MB file use bs=24MB or bs=1MB respectively. I suppose it’s mb on a Mac. – dessert Nov 14 '19 at 12:46
  • In my case, dd only created a file of 34MB. max. even if I used a bigger value in bs. Using bs=1M count=500 worked well. – Juan Calero Feb 06 '20 at 12:00
71

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

  • 1
    BusyBox also has it, so most embedded systems will too :-) – Ciro Santilli OurBigBook.com Oct 03 '18 at 01: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 '19 at 12:52
  • 1
    @dessert The asker probably wanted 24MiB. Most people don't use the binary prefixes. – Gilles 'SO- stop being evil' Nov 14 '19 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 '19 at 13:30
  • My intuitition is that this is the fastest alternative, especially as the file size becomes large. – Jim L. Nov 16 '21 at 21:19
  • As "@Gilles'SO-stopbeingevil'" expressed in answer, Created file doesn't consume actual space in storage. But fallocate command does it. Another answer https://unix.stackexchange.com/a/381339/235261 – EsmaeelE Feb 14 '24 at 09:08
56

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.

Socowi
  • 625
SArcher
  • 743
  • 1
    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 '19 at 12:54
  • 4
    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. – Martin Hansen Jun 08 '20 at 07:44
  • 2
    Note that fallocate is not supported on multiple filesystems including ext3 – reflex0810 Oct 08 '20 at 20:47
  • thx @Azatik1000, seems to not work on zfs. Although it sounds like the best answer :-/ – MoRe Apr 16 '21 at 08:21
17

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
BitsOfNix
  • 5,117
4

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
crifan
  • 149
-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;