How to create a file with ANY given size in Linux?

I read this question: How to create a file with a given size in Linux?

But I have no answer to my question.

I want to create a file of size 372.07 MB,

I tried the following commands in Ubuntu 10.08:

dd if=/dev/zero of=output.dat bs=390143672 count=1 dd: memory exhausted 

390143672 = 372.07 * 1024 * 1024

Are there any other methods?

Thank you so much!

Edit: How to view the file size on a Linux command line with a decimal point. I mean, the ls -hl command line simply says: "373M", but the file is actually "372.07M".

+7
source share
4 answers

Sparse file

 dd of=output.dat bs=1 seek=390143672 count=0 

This adds the advantage of creating a sparse file if this file system supports it. This means that it is not wasted if some of the pages (_blocks) are ever written, and file creation is very fast .


Opaque (opaque) file:

Edit , since people, correctly indicating that sparse files have characteristics that may be disadvantageous in some scenarios, here's a sweet point:

You can use fallocate (on Debian due to util-linux ) instead:

 fallocate -l 390143672 output.dat 

This still has the advantage of not having to actually write blocks, so it is pretty fast, like creating a sparse file , but it is not sparse. The best of two worlds.

+15
source

Change your settings:

 dd if=/dev/zero of=output.dat bs=1 count=390143672 

otherwise dd tries to create a buffer of 370 MB in memory.

If you want to do this more efficiently, first write the 372MB part using large blocks (say 1M), then write the tail part with 1 byte blocks using the seek option to jump to the end of the file first.

Example:

 dd if=/dev/zero of=./output.dat bs=1M count=1 dd if=/dev/zero of=./output.dat seek=1M bs=1 count=42 
+7
source

truncate - reduce or increase the file size to the specified size

The following example truncates putty.log from 298 bytes to 235 bytes.

 root@ubuntu :~# ls -l putty.log -rw-r--r-- 1 root root 298 2013-10-11 03:01 putty.log root@ubuntu :~# truncate putty.log -s 235 root@ubuntu :~# ls -l putty.log -rw-r--r-- 1 root root 235 2013-10-14 19:07 putty.log 
+1
source

Swap amount and bs. bs bytes will be in memory, so it cannot be so large.

0
source

All Articles