Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Creating sparse files on Windows/ Linux

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Sparse file is a type of computer file, which generally has file size (logical) higher than allocated size (clusters allocated to data of the file). Most File systems support this type of file, but Operating system suppresses them underneath command interpreters’ command or API calls.

Therefore, these could not be accessed using generic copy/paste/create methods, rather they are exposed via specialized commands in command processor of operating system or API calls. In this article, we will take look at commands offered by command processor of OS’es to create sparse files.

Windows :
Windows provides command line program named fsutilI (File system utility) which allows for creation/allocation/query of sparse files. To create sparse file on windows, execute the following command on command interpreter (cmd.

fsutil File CreateNew applese 1000

The above command creates new file (filled with zeros) under name of applese. But file isn’t sparse as of now. To make this file sparse file execute following commands, after previous one.

fsutil Sparse SetFlag applese
fsutil Sparse SetRange applese 0 1000

The first command enables sparse flag on our newly created file. This allows for execution of second command which sets first 1000 bytes of our file (from 0 – 1000) to sparse range. Therefore, we get a completely sparse file. The confirmation of which is provided by layout command.

   Stream              : 0x080  ::$DATA
   Attributes          : 0x00000200: Sparse
   Flags               : 0x00000018: No clusters allocated | Has Parsed Information
   Size                : 1, 000 (1.0 KB)
   Allocated Size      :     0 (0.0 KB)
   Vdl                 :     0 (0.0 KB)
   Extents             : 1 Extents   

Clear from above section of layout command, our file has 0 allocated sizes but 1000 bytes of virtual size. This could also be seen in properties of the file.

Linux :
On Linux, the process of creating sparse file is really trivial, as there exist several commands for creation of sparse files. We would be using truncate command for creation of sparse files. The command is as follows.

truncate -s 5M temp

The above command creates sparse file named temp, which is 5 megabytes (~5 million bytes) in size.

Linux OS variants provides extensive support for using sparse files, such as sparse files modification, copy, archiving, resizing, file growth etc. Most of which could be found here.

My Personal Notes arrow_drop_up
Last Updated : 04 Sep, 2020
Like Article
Save Article
Similar Reads