You can use the dd command to create image files for network or file system testing. You need to use the following commands:
- fallocate command – Preallocate space to a file.
- truncate command – Shrink or extend the size of a file to the specified size.
- dd command – Convert and copy a file i.e. clone/create/overwrite images.
- df command – Show free disk space.
- du command – Show disk usage statistics.
- ls command – List file size.
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | No |
Requirements | fallocate or dd |
Time | 5m |
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
🐧 20 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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
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:
it should show the proper size.
It’s even easier, using the format that the commenter Indie suggest:
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
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.
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?
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!
can I supress the output of dd command?
Yes, send it to /dev/null.
dd args >/dev/null
OR
dd args >/dev/null 2>&1
Slightly easier version is to use a suffix for the size instead of trying to do the maths.
to create a 1G file.
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
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.
I have 2 tb harddrive and i want to clone only 1 tb to my other drive?
what command should i do
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
How to check if the created file is empty, is there a way to check that?
ls -l
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.
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.
How to delete that test file that is being created?
Use rm command:
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