# Create a 2Mb file

**URL:** <https://community.unix.com/t/create-a-2mb-file/389400>\
**Category:** AIX\
**Created:** [January 2, 2023, 4:47pm UTC](https://community.unix.com/t/create-a-2mb-file/389400 "2023-01-02T16:47:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [January 2, 2023, 4:47pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/1 "2023-01-02T16:47:47Z")

</div>

Hi,  
Happy new year.

Thanks for your answers to my questions during 2022.

How can I create a 2Mb file?

Thanks

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [January 2, 2023, 5:31pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/2 "2023-01-02T17:31:34Z")

</div>

With  
`mkfile ...`  
or  
`dd if=/dev/zero ...`

Consult the respective man pages!

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [January 2, 2023, 5:56pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/3 "2023-01-02T17:56:37Z")

</div>

Thank you,  
dd is to convert:  
dd Command

Purpose

```
   Converts and copies a file.

```

$ man mkfile  
Manual entry for mkfile not found or not installed.

Thanks.

---

<div class="post-metadata">

**Author:** ![hicksd8](https://community.unix.com/letter_avatar/hicksd8/32/6_22734026db12803ebb3e059622ac3534.png) [@hicksd8](https://community.unix.com/u/hicksd8)\
**Post date:** [January 2, 2023, 7:31pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/4 "2023-01-02T19:31:06Z")

</div>

> [@big123456](#):
>
> Purpose
> 
> ```plaintext
> Converts and copies a file.
> 
> ```

This is true since 'dd' has an 'if' (input file) argument and an 'of' (output file) argument.

So as already suggested, using /dev/zero is a special 'file' of just zeros.

So you can use that as input file,

```no-highlight
if=/dev/zero

```

Output file will be the file that you want to write,

```no-highlight
of=<path to output file>

```

You control the output file size with two argments, bs= and count=

'bs' (blocksize) is the blocksize that you want to read and write, and would typically be specified as 512 bytes since that is typical disk sector size,

'count' specifies how many blocks you want to write. NOTE: Without 'count' the command will continue forever thereby filling your filesystem eventually.

So,

```no-highlight
bs=512 count=4000

```

will give you exactly 2MB. (512 \* 4000)

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [January 2, 2023, 9:18pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/5 "2023-01-02T21:18:46Z")

</div>

> [@hicksd8](#):
>
> `bs=512 count=4000`

Or  
`bs=1024 count=2000`  
Or  
`bs=1024 count=2048`  
?

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_22734026db12803ebb3e059622ac3534.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [January 2, 2023, 10:59pm UTC](https://community.unix.com/t/create-a-2mb-file/389400/6 "2023-01-02T22:59:34Z")

</div>

This `truncate` command creates a file of 2MB instantly.

It is a "sparse file". If you read it, it contains 2097152 zero bytes. But it occupies no space on the disk. So if you want a large file for e.g. performance checking, the results will be misleading.

I can create a 2 terabyte sparse file on my 300 megabyte disk, and that takes zero blocks too.

```plaintext
$ truncate -s $(( 2 * 1024 * 1024 )) EmptyFile
$ ls -l EmptyFile
-rw-rw-r-- 1 paul paul 2097152 Jan 2 22:46 EmptyFile
$ wc EmptyFile
      0 0 2097152 EmptyFile
$ file EmptyFile
EmptyFile: data
$ stat EmptyFile
  File: EmptyFile
  Size: 2097152 Blocks: 0 IO Block: 4096 regular file
Device: 809h/2057d	Inode: 6165481 Links: 1
Access: (0664/-rw-rw-r--) Uid: (1000/paul) Gid: (1000/paul)
Access: 2023-01-02 22:46:45.953861059 +0000
Modify: 2023-01-02 22:46:27.786605625 +0000
Change: 2023-01-02 22:46:27.786605625 +0000
 Birth: -
$ 

```

---

<div class="post-metadata">

**Author:** ![bendingrodriguez](https://community.unix.com/user_avatar/community.unix.com/bendingrodriguez/32/21971_2.png) [@bendingrodriguez](https://community.unix.com/u/bendingrodriguez)\
**Post date:** [January 3, 2023, 6:29am UTC](https://community.unix.com/t/create-a-2mb-file/389400/7 "2023-01-03T06:29:34Z")

</div>

instead of calculating the number of bytes, you could simply write

```plaintext
$ truncate -s 2m file

```

---

<div class="post-metadata">

**Author:** ![big123456](https://community.unix.com/letter_avatar/big123456/32/6_22734026db12803ebb3e059622ac3534.png) [@big123456](https://community.unix.com/u/big123456)\
**Post date:** [January 3, 2023, 9:21am UTC](https://community.unix.com/t/create-a-2mb-file/389400/8 "2023-01-03T09:21:27Z")

</div>

> [@hicksd8](#):
>
> `bs=512 count=4000`

Yes. Thank you. It worked.

---

<div class="post-metadata">

**Author:** ![chatgpt](https://community.unix.com/user_avatar/community.unix.com/chatgpt/32/19766_2.png) [@chatgpt](https://community.unix.com/u/chatgpt)\
**Post date:** [January 4, 2023, 2:55am UTC](https://community.unix.com/t/create-a-2mb-file/389400/9 "2023-01-04T02:55:08Z")

</div>

Here is the answer by ChatGPT after I copied-and-pasted the original question to ChatGPT:

> To create a 2 MB file, you can use a text editor such as Notepad on Windows or TextEdit on Mac to create a large file by adding text or by repeating a smaller file multiple times. Here is an example of how to do this using the `cat` command in a terminal or command prompt on a Unix-like system such as Linux or macOS:
> 
> 1. Open a terminal window.
> 2. Use the `dd` command to create a 1 MB file filled with random data: `dd if=/dev/urandom of=1MB.txt bs=1M count=1`
> 3. Use the `cat` command to create a new file by concatenating the 1 MB file twice: `cat 1MB.txt 1MB.txt > 2MB.txt`
> 
> This will create a new file called `2MB.txt` that is 2 MB in size. Note that the actual content of the file will be random data.
> 
> You can also use a file compression tool to create a 2 MB file by compressing a larger file. For example, you could use the `gzip` command to compress a text file and create a smaller `.gz` file.
> 
> I hope this helps! Let me know if you have any other questions.

[![Screenshot 2023-01-04 at 9.55.14 AM](https://community.unix.com/uploads/default/original/3X/5/c/5c16efbb570e1d9fa24281070989e9bfefaa1b14.png)](https://chat.openai.com/chat)

> [@Replying to Questions Using ChatGPT](https://community.unix.com/t/replying-to-questions-using-chatgpt/389421):
>
> I have just provided over 10 examples of using [ChatGPT](https://chat.openai.com/chat) to answer questions. All members can do the same, but please state / reference that you are using ChatGPT if you do so. We may create a (future) rule for this, I think (have not discussed with the team yet). In the meantime, anyone who posts a reply using ChatGPT should say so. FWIW, some of the replies I have read in 2022 seem to have originated from ChatGPT, TBH... Thanks: Examples Using ChatGPT ....

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [January 11, 2023, 2:55am UTC](https://community.unix.com/t/create-a-2mb-file/389400/10 "2023-01-11T02:55:46Z")

</div>

Closed on January 11, 2023, 2:55am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
