10

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

But I havent got answer to my question.

I want to create a file of 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

Is there any other methods?

Thanks a lot!

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

Community
  • 1
  • 1
DocWiki
  • 3,488
  • 10
  • 39
  • 49

4 Answers4

18

Sparse file

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

This has the added benefit of creating the file sparse if the underlying filesystem supports that. This means, no space is wasted if some of the pages (_blocks) ever get written to and the file creation is extremely quick.


Non-sparse (opaque) file:

Edit since people have, rightly pointed out that sparse files have characteristics that could be disadvantageous in some scenarios, here is the sweet point:

You could use fallocate (in Debian present due to util-linux) instead:

fallocate -l 390143672 output.dat

This still has the benefit of not needing to actually write the blocks, so it is pretty much as quick as creating the sparse file, but it is not sparse. Best Of Both Worlds.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • "extremely quick" but subsequent writes to the file are less predictable, and can fail if your partition filled up in the mean time. (Also careful with how backup tools handle your sparse files.) (But +1, it is a nice option.) – Mat Nov 01 '11 at 11:18
  • @sehe: I tried your command, but there seems to be something wrong. `-rw-r--r-- 1 root root 199753560064 Nov 1 19:19 output.dat` and `ls -hl output.dat` gives `-rw-r--r-- 1 root root 187G Nov 1 19:19 output.dat` 187G???? Crazy! – DocWiki Nov 01 '11 at 11:22
  • @DocWiki: **oops** fixed that. I was missing `bs=1`, sorry – sehe Nov 01 '11 at 11:25
  • @sehe I remember. Someone told me "Remember that dd's default block size is 512 bytes" – DocWiki Nov 01 '11 at 11:27
  • @Mat: fair enough. Showing a 'best of worlds' asnwer based on `fallocate` now (tested on debian) – sehe Nov 01 '11 at 11:30
  • @sehe: `fallocate -l 390143672 output.dat` gives `fallocate: output.dat: fallocate failed: Operation not supported` on my Ubuntu 10.08. I am Googling for this error. – DocWiki Nov 01 '11 at 11:39
  • @DocWiki: You're apparently not using a filesystem that supports fast file allocation IOCTLs (like e.g. ext4)... That's unfortunate. – sehe Nov 01 '11 at 12:07
7

Change your parameters:

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

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

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

Ex:

dd if=/dev/zero of=./output.dat bs=1M count=1
dd if=/dev/zero of=./output.dat seek=1M bs=1 count=42
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    ... I wonder why people prefer this version. It works, true, but it sure isn't optimal – sehe Nov 01 '11 at 11:12
  • Depends on what you goal is. Spares files are fun, but if you want to actually reserve space on the filesystem (maybe to have a better chance of getting large continuous chunks on disk), actually writing the file is helpful. – Mat Nov 01 '11 at 11:15
  • @Mat This solution takes too long on my laptop. So I have to kill it before it finishes. – DocWiki Nov 01 '11 at 11:18
  • @Matt Any example on using `seek` to add 0.07M to a file? – DocWiki Nov 01 '11 at 11:24
  • @Matt Thanks a lot. How to view a file's size on Linux command line with decimal. I mean, the command line `ls -hl` just says: '373M' but the file is actually "372.07M". – DocWiki Nov 01 '11 at 11:31
  • @DocWiki: just use `ls -l` to see the actual file size. – Mat Nov 01 '11 at 11:34
  • @Mat You could simply optimize like this: `dd if=/dev/zero bs=1 count=390143672 | dd of=output.dat bs=1M` instead. Much simpler – sehe Nov 01 '11 at 11:34
  • @sehe: we appear to have different concepts of what is "simple" :) – Mat Nov 01 '11 at 11:36
  • @Mat: simple is when the user has only **one** number to enter and no manual calculation involved. In your version, `count=42` is non-obvious and /hard/ to generalize in shell scripting. – sehe Nov 01 '11 at 12:04
1

truncate - shrink or extend the size of a file 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
Hardy Feng
  • 459
  • 4
  • 13
0

Swap count and bs. bs bytes will be in memory, so it can't be that big.

thejh
  • 44,854
  • 16
  • 96
  • 107