Tuesday, February 23, 2010

How to detect sparse files?

Sparse files are the files with lots of holes :) and can become painful while copying those to other location/filesystem/tape for that matter. In this example, I tried to create a sparse file. The question is how to detect a sparse file. Well, the sparse files, in most of the cases, shows different size in different commands output. Here is shown how:


$ dd if=/dev/zero of=FILE1 bs=8k count=1 seek=1000
1+0 records in
1+0 records out

$ ls -lh
-rw-r--r-- 1 vikass htt 7.9M Feb 23 09:59 FILE1

$ du -sh FILE1
12K FILE1

$ stat FILE1
File: `FILE1'
Size: 8200192 Blocks: 24 IO Block: 32768 regular file
Device: 16h/22d Inode: 316723 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1020/ vikass) Gid: ( 103/ htt)
Access: 2010-02-23 09:59:53.109548000 +0800
Modify: 2010-02-23 09:59:53.111551000 +0800
Change: 2010-02-23 09:59:53.111551000 +0800



Now, check the size reported in "ls -lh" command and "du -h" command on the same file. If it reports different, then it is nothing but a sparse file.

This example was from a Linux system (RHEL 4U7) but will work for Solaris also, apart from stat command. Enjoy! :)

No comments: