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

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

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]
dd if=/dev/zero of=/path/to/output.img [options]
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

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: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts