Linux / UNIX: Create Large 1GB Binary Image File With dd Command

Posted on in Categories , , last updated November 27, 2013

How do I create 1 GB or 10 GB image file instantly with dd command under UNIX / Linux / BSD operating systems using a shell prompt?

You can use the dd command to create image files for network or file system testing. You need to use the following commands:

  1. fallocate command – Preallocate space to a file.
  2. truncate command – Shrink or extend the size of a file to the specified size.
  3. dd command – Convert and copy a file i.e. clone/create/overwrite images.
  4. df command – Show free disk space.
  5. du command – Show disk usage statistics.
  6. ls command – List file size.

fallocate command syntax

The basic syntax is:
fallocate -l Image_Size_Here /path/to/image.img

Creating a large file on a Linux using fallocate command

The following command will create 1G file:

fallocate -l 1G test.img

Verify new disk image with the ls command:
$ ls -lh test.img
Sample outputs:

-rw-r--r--. 1 root root 1.0G Nov 27 03:42 test.img

You can use stat or du command to verify block allocation:

stat test.img
  File: `test.img'
  Size: 1073741824	Blocks: 2097160    IO Block: 4096   regular file
Device: 805h/2053d	Inode: 9043983     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-11-27 03:42:27.575841698 -0600
Modify: 2013-11-27 03:42:27.575841698 -0600
Change: 2013-11-27 03:51:06.108962462 -0600

du -h test.img
1048580	test.img

To create 100M size, enter:
fallocate -l 100M file.out
The -l option specifies the length of the allocation, in bytes. Suffixes of k, m, g, t, p, e may be specified to denote KiB, MiB, GiB, etc.

dd command syntax

Note: The following information only exists for older version of Linux and/or when fallocate command is not available. If possible use fallocate command only for creating binary images. dd command based method is considered as old and not recommended, but presented here for historical reasons only.

The basic syntax is:
dd if=/path/to/input of=/path/to/output [options]
OR
dd if=/dev/zero of=/path/to/output.img [options]
OR
dd if=/dev/zero of=YOUR-IMAGE-FILE-NAME-HERE bs=1 count=0 seek=Size-HERE

Creating an image file with dd command

First, make sure you've sufficient disk space to create a image file using dd:
$ df -H

To create 1MB file (1024kb), enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=1024

You will get an empty files (also known as "sparse file") of arbitrary size using above syntax. To create 10MB file , enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*10]

To create 100MB file , enter:
$ dd if=/dev/zero of=test.img bs=1024 count=0 seek=$[1024*100]
$ ls -lh test.img

To create 1GB, file:
$ dd if=/dev/zero of=1g.img bs=1 count=0 seek=1G
Sample output:

0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000235686 s, 0.0 kB/s

Verify file size (note bs factor in original dd command):
$ ls -lh 1g.img
$ stat 1g.img
$ du -h 1g.im

dd tip: Create a file with a given size in Unix or Linux

If truncate command not avilable, try the following syntax:
dd if=/dev/zero of=/path/to/fiie.img bs=YOUR-FILE-SIZE-HERE count=1
In this example, create a file with 1G (on *BSD/OS X use 1g) size:
$ dd if=/dev/zero of=1g.bin bs=1G count=1
Sample outputs:

1+0 records in
1+0 records out
1073741824 bytes (1.1 GB) copied, 8.12307 s, 132 MB/s

Verify file size/blocks:
$ stat 1g.bin
Sample outputs:

  File: `1g.bin'
  Size: 1073741824	Blocks: 2097152    IO Block: 4096   regular file
Device: 805h/2053d	Inode: 9043988     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2013-11-27 04:15:02.962662890 -0600
Modify: 2013-11-27 04:15:03.769673943 -0600
Change: 2013-11-27 04:15:03.769673943 -0600

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

20 comment

  1. I don’t think this works correctly unless the “count” argument is changed to something greater than 0.

    Try something like this:

    dd if=/dev/zero of=10Gtest bs=1M count=10000

    1. I don’t think you have read and tested this yourself.

      If using “count=0”, as the article suggests, you will still have the file of the specified size.

      However, the output will show something like “0+0 records in” and “0+0 records out”.

      Then if you check the file size:

      ls -al /path/to/test.img

      it should show the proper size.

      It’s even easier, using the format that the commenter Indie suggest:

      dd if=/dev/zero of=/path/to/disk.img bs=1 count=0 seek=1G
      1. JW is correct. Without a count setting – its generating a 0 byte file.

        ls -alh will show a file size; but compare that to df -h – and you’ll see it doesn’t actually consume any space on disk

        1. I think the commenter “Indie” has already clarified this in a previous comment (in agreement with the author).

          @JW, @sonassi, @stfu_and_die
          If you think you’re right, why don’t tell the author of the article to either remove or edit the article? or write your own article?

          Indie August 26, 2011 at 3:02 pm

          The ls command will tell you the size of the file while du will tell you the Disk Usage. When you create the empty disk image with the above command it doesn’t write any data so it doesn’t actually allocate any blocks to the file.

          If you type “stat disk.img” you’ll see that it’s using 0 blocks and thus no disk space is being consumed by the file, it’s only when you then write data to it that the blocks will then be allocated.

  2. Yes this is work but there is concern !!!

    When do ls -lh disk.img file its showing as 1G size but when u run the command

    du -h disk.img it shows 0 bytes of size…..

    Which one we can believe…i dont know…please clarify if you know about this…

    Jai

  3. The ls command will tell you the size of the file while du will tell you the Disk Usage. When you create the empty disk image with the above command it doesn’t write any data so it doesn’t actually allocate any blocks to the file.

    If you type “stat disk.img” you’ll see that it’s using 0 blocks and thus no disk space is being consumed by the file, it’s only when you then write data to it that the blocks will then be allocated.

    1. Hi, the question is old – but I’m happy to provide a nice answer – maybe this basic advice will help other people as well.
      This isn’t exactly possible with dd. If you’d only dd 1tb of the 2tb drive and you’re lucky to be able to mount it, you’ll end up seeing correct filenames – but {all,most,many} files will be corrupted entirely.
      I’d rather suggest you:
      ? format your second hard-drive
      ? look which files/directories are using how much space, recommended tool is ncdu
      ? create a list of files you want to copy and use rsync to copy the files

  4. Using /dev/zero will create a file for you, but the free space seems to remain the same on the disk. If you’re looking to make a filler file as I am in /var/lib/mysql so when the database fills up, I still have some space to work with.. you’ll want to substitute /dev/zero to /dev/random or something like that.

    Here’s my attempt at creating a 1G filler file.
    df output before:
    /dev/mapper/VG01-LV_mysql
    30959612 27519608 3439984 89% /var/lib/mysql
    command:
    dd if=/dev/zero of=1gig-filler.img bs=1000 count=0 seek=$[1000*1000*1]
    0+0 records in
    0+0 records out
    0 bytes (0 B) copied, 3e-05 seconds, 0.0 kB/s

    file:
    -rw——- 1 root root 1000000000 Feb 13 12:26 1gig-filler.img

    df output after (space is slightly different because the db is being used):
    /dev/mapper/VG01-LV_mysql
    30959612 27521072 3438520 89% /var/lib/mysql

    I don’t see any use for using /dev/zero in this scenario. I’ve used it to wipe a disk clean… dd if=/dev/zero of=/dev/sda will clear a disk, but this file doesn’t take up real space.

  5. Actually it might be the count=0 part that is making it not use any real space.. I can’t seem to get df to show me the space actively being used even with /dev/random.. I know I’ve done this successfully before.

  6. What if I wanted to create a large file but with random content as I need to perform some verification on the file and have a file full of NULLs doesn’t really help.

    Thanks in advance

Leave a Comment