Linux/UNIX: Use the dd command to create a 1 GB binary

Source: Internet
Author: User

Linux/UNIX: Use the dd command to create a 1 GB binary

How can I use Shell commands to create 1 GB or 10 Gb image files on UNIX, Linux, and BSD systems using dd commands?

You can use the dd command to generate an image file to test the network or file system.

Run the following command:

  1. Fallocate-Pre-allocate blank files;
  2. Truncate-reduce or expand the file size;
  3. Dd-convert and copy files, such as cloning, creating, and overwriting images;
  4. Df-view disk space;
  5. Du-Statistics on disk usage;
  6. Ls-list the file size;

Fallocate command syntax

The basic syntax is:

Fallocate-l Image_Size_Here/path/to/image. img

Use the fallocate command to create a large file in Linux

Run the following command to create a 1 GB file:
Fallocate-l 1G test. img

Run the ls command to view the file size:

$ Ls-lh test. img

Sample output:
-Rw-r --. 1 root 1.0G Nov 27 0:42 test. img

You can use the stat or du command to view the block allocation information:
Stat test. img
File: 'test. img'
Size: 1073741824 Blocks: 2097160 IO Block: 4096 regular file
Device: 805 h/2053d Inode: 9043983 Links: 1
Access: (0644/-rw-r --) Uid: (0/root) Gid: (0/root)
Access: 03:42:27. 575841698-0600
Modify: 03:42:27. 575841698-0600
Change: 03:51:06. 108962462-0600
Du-h test. img
1048580 test. img

Create a 100 M file and press Enter:

Fallocate-l 100 M file. out

The-l parameter is used to specify the file size in bytes, such as k, m, g, t, p, e, indicating KiB, MiB, GiB, TiB, PiB, eiB.

Dd command syntax

Note: The following information only exists in earlier versions of Linux or when the fallocate command is unavailable. If possible, use the fallocate command to create a binary image file. The dd command is regarded as old and is not recommended, but it is only for the historical reasons.

Basic Syntax:

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

Use the dd command to create an image:

First, make sure that the disk has enough space to store the files created by dd:

$ Df-H

Create a 1 MB file and press Enter:

$ Dd if =/dev/zero of = test. img bs = 1024 count = 0 seek = 1024

You will get an empty file (also known as "sparse file "). Create a 10 M file and press Enter:

$ Dd if =/dev/zero of = test. img bs = 1024 count = 0 seek = $ [1024*10]

Create a 100 M file and press Enter:

$ Dd if =/dev/zero of = test. img bs = 1024 count = 0 seek = $ [1024*100]
$ Ls-lh test. img

Create a 1 GB 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

Confirm the file size (pay attention to the role of bs parameters ):

$ Ls-lh 1G. img
$ Stat 1g. img
$ Du-h 1g. im

Dd prompt: Create a specified size file in Unix or Linux

If the truncate command is unavailable, 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 1 GB file (use 1 GB in * BSD/OS X ):

$ Dd if =/dev/zero of = 1G. bin bs = 1G count = 1
Example output: 1 + 0 records in
1 + 0 records out
1073741824 bytes (1.1 GB) copied, 8.12307 s, 132 MB/s

Confirm file size:

$ Stat 1g. bin

Sample output: File: '1G. bin'
Size: 1073741824 Blocks: 2097152 IO Block: 4096 regular file
Device: 805 h/2053d Inode: 9043988 Links: 1
Access: (0644/-rw-r --) Uid: (0/root) Gid: (0/root)
Access: 04:15:02. 962662890-0600
Modify: 04:15:03. 769673943-0600
Change: 04:15:03. 769673943-0600

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

Translator: Daniel

This article is launched by the help translation team's original translation help house.

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.