17

In linux:

$ dd if=/dev/null of=mysparse seek=1G count=0
$ du -b mysparse
549755813888    mysparse
$ du -B1 mysparse
0       mysparse

In windows I have been playing with fsutil but I can not get the size on disk to be smaller than the size in the file properties.

Tarnay Kálmán
  • 3,597
  • 5
  • 27
  • 32
sabgenton
  • 674
  • 1
  • 8
  • 18

3 Answers3

22
Type NUL > temp
FSUtil Sparse SetFlag temp
FSUtil Sparse SetRange temp 0 0x40000000
FSUtil File SetEOF temp 0x40000000

It Creates a New File, then Sets the Sparseness Flag for it, then Sets that specific Range to Sparse. The size is in bytes.

You can also refer to the documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/fsutil-sparse

user541686
  • 23,663
  • 46
  • 140
  • 214
  • 1
    Not very useful without an explanation of what each line does. So that I would know how to adapt it for my case. – ivan_pozdeev Jan 02 '23 at 23:40
  • @ivan_pozdeev: Sorry, I thought it was fairly self-explanatory. It Creates a New File, then Sets the Sparseness Flag for it, then Sets that specific Range to Sparse. You can also refer to the documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/fsutil-sparse – user541686 Jan 03 '23 at 00:39
  • 1
    What's 0x100000 ? Size in kB? blocks? clusters? The OP asks for 1GB. `temp` looks like a name. And `fsutil` is probably case-insensitive. Finally, you first create the file and then mark a region in it sparse. So I have to first use up the full space, no way around it? The purpose of sparse files is specifically to not have to do that -- so that kinda defeats the purpose. – ivan_pozdeev Jan 03 '23 at 15:18
  • @ivan_pozdeev: The size is in bytes as the documentation says. I forget why I put a smaller amount but it was probably to mitigate someone pasting one line and then forgetting about a huge amount of space usage. As for allocating the full space, it doesn't defeat the purpose for most people since people usually just need speed & steady state space usage here, not a momentary spike in space allocation exceeding their free space. Btw I *believe* the commands they gave for Linux with dd do the same thing, though I'd have to check. If you want sparsity from birth, I'd have to find another way. – user541686 Jan 03 '23 at 17:37
  • @ivan_pozdeev: I updated the script to avoid the preallocation. – user541686 Jan 03 '23 at 19:28
5

May be this will help you: "SparseChecker" - manages sparse files on Windows NTFS It allows to operate with files, not with individual blocks of files as "fsutil" does.

benok
  • 115
  • 7
opal
  • 51
  • 1
  • 1
  • You must have [32-bit MSVC redist pkg](https://www.microsoft.com/en-us/download/details.aspx?id=26999) installed for this to work. – gronostaj Jan 24 '23 at 12:19
2

What @Mehrdad said is correct.

However, whatever you write to the file (even ranges of zero) would be considered data to the operating system (and thus space allocated) and only FSCTL_SET_ZERO_DATA() would allow non-space occupying zeros to be written.

fsutil sparse setflag [file]

would allow a file to be set as sparse file.

bubu
  • 9,863
  • 2
  • 29
  • 44