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

by on June 2, 2008 · 18 comments· LAST UPDATED November 27, 2013

in , ,

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.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Requirementsfallocate or dd
Estimated completion time5m

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
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 18 comments… read them below or add one }

1 JW June 17, 2009 at 5:57 am

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

Reply

2 zzzplayer March 5, 2013 at 4:42 am

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

Reply

3 sonassi March 27, 2013 at 2:11 pm

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

Reply

4 stfu and die May 1, 2013 at 2:15 pm

Bullshit the 10g one above doesnt create the proper file size at all. Check with the du command it replys the file size is 0.

Reply

5 zzzplayer November 27, 2013 at 8:17 am

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.

Reply

6 nixCraft November 27, 2013 at 10:07 am

The FAQ has been updated with new and accurate information. However, I kept dd command based method for historical reasons only. I recommend using fallocate command on Linux.

Hope this helps!

Reply

7 Diane Girard June 24, 2009 at 9:48 pm

can I supress the output of dd command?

Reply

8 nixCraft June 25, 2009 at 8:05 am

Yes, send it to /dev/null.

dd args >/dev/null
OR
dd args >/dev/null 2>&1

Reply

9 Indie December 26, 2010 at 12:23 am

Slightly easier version is to use a suffix for the size instead of trying to do the maths.

dd if=/dev/zero of=disk.img bs=1 count=0 seek=1G

to create a 1G file.

Reply

10 jayaram Prasad August 26, 2011 at 5:09 am

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

Reply

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

Reply

12 Jigar June 1, 2012 at 7:56 am

I have 2 tb harddrive and i want to clone only 1 tb to my other drive?

what command should i do

Reply

13 vsama June 2, 2012 at 11:52 pm

How to check if the created file is empty, is there a way to check that?

Reply

14 Bhanu November 6, 2012 at 12:01 pm

ls -l

Reply

15 sonic February 13, 2013 at 6:37 pm

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.

Reply

16 sonic February 13, 2013 at 6:54 pm

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.

Reply

17 Mike October 24, 2013 at 3:01 pm

How to delete that test file that is being created?

Reply

18 nixCraft October 24, 2013 at 4:07 pm

Use rm command:

rm fileName-here

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: