You will be intrigued to know that we can fake the size of the file, even the disk. This article mainly focuses on “Sparse file” and will give you complete details about it. Do not miss any part of this article and read it completely to know more about the sparse file and how to create one.
What is a Sparse File?
Commonly, a file is made up of bits of data stored in blocks on the data disk. Now, this file is using some space in your disk and that amount of space can be checked easily.
Similarly, the Sparse file is like any other file, but instead of containing the data on the blocks, it only contains zeros. For instance, you can have 1GB of sparse file without actually using 1GB of space in the file system.
According to Wikipedia,
In computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when the file itself is partially empty. This is achieved by writing brief information (metadata) representing the empty blocks to disk instead of the actual “empty” space which makes up the block, using less disk space. The full block size is written to disk as the actual size only when the block contains “real” (non-empty) data.
As the Sparse file contains zeros, below is the image showing how it looks like. This is a Sparse file created in Windows operating system.
Advantages of Sparse File
- Storage is only allocated when actually needed.
- Large files can be created even if there is insufficient free space on the file system.
- Can easily be used for testing environments.
Disadvantages of Sparse File
- Sparse files may become fragmented.
- File system free space reports could be misleading while checking free or used space.
- File systems having sparse files can have unexpected effects.
How to Create a Sparse File in Unix/Linux
There are various commands by which you can create a sparse file in a file system. Below are few of the common ways of creating a sparse file along with the example.
1. Using ‘truncate’ command:
Syntax: truncate -s size file_name
[root@client1 ~]# truncate -s 5M test.txt [root@client1 ~]# [root@client1 ~]# cat test.txt [root@client1 ~]# ls -lh test.txt -rw-r--r--. 1 root root 5.0M Jun 21 12:39 test.txt [root@client1 ~]#
2. Using ‘dd’ command:
[root@client1 ~]# dd of=test-sf bs=1k seek=5120 count=0 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000330922 s, 0.0 kB/s [root@client1 ~]# [root@client1 ~]# cat test-sf [root@client1 ~]# ls -lh test-sf -rw-r--r--. 1 root root 5.0M Jun 21 13:09 test-sf [root@client1 ~]#
How to Create a Sparse File in Windows
In Windows Operating system, we don’t have utilities to create a sparse file and also it takes few commands to run in your OS. You can use “FSUTIL” command to create a sparse file in Windows.
Below is the example.
Syntax: fsutil file createnew filename length
C:\>fsutil file createnew test 0x10000 File C:\test is created C:\>fsutil sparse setflag test C:\>fsutil sparse setrange test 0 0x10000 C:\>fsutil sparse queryflag test This file is set as sparse
If you will try to set range without setting the sparse flag, it will give the error “The specified file is NOT sparse”.
Please also refer MAN page of fsutil.
In this article, you read about sparse file and how to create them in Unix and Windows operating system. Yes, we know it is easy to create sparse file in Unix machine.
Hope you like this article about “Sparse File” and know what it is. If you have any doubts about sparse file or how to create it, do let us know through your comments on this article. You can also tell us what other topics we should cover in this blog.
If you like this article, do not forget to subscribe to Storage Tutorials by Email and do share it with your colleagues and in social media.