Operating System - HP-UX
1838576 Members
4117 Online
110128 Solutions
New Discussion

Large file with NULL characters

 
Michael D'Aulerio
Regular Advisor

Large file with NULL characters

I'm puzzled by file sizes of files with NULL characters. I have a file with 2264512 bytes, most of which are NULL characters (\000). When I run ls -l, it shows 2264512 for the file size. When I run du -s, it only shows 64 blocks. If I copy the file or tar the file, the new file has 4448 blocks. How can a file with that many bytes only take up 64 blocks?
Email: michael.n.daulerio@lmco.com
3 REPLIES 3
Sundar_7
Honored Contributor

Re: Large file with NULL characters

Hi Michael,

You are basically looking at something called "Sparse" files.

Try this

# echo | dd bs=1024 seek=100000 of=/tmp/large

# ll /tmp/large

# du â sk /tmp/large

ls will tell you the size is 102 MB but du will tell you the size is only 8 KB.

I dont believe tar can even begin to understand a sparse file. use fbackup to backup/restore sparse files.

A typical sparse file content will look like something like this

data data NULL NULL ......... NULL data data

Now what you see from the ls -l output is the size of the file including the NULLs.

-- Sund
Learn What to do ,How to do and more importantly When to do ?
Michael D'Aulerio
Regular Advisor

Re: Large file with NULL characters

Thanks Sund,
You explain the sparse files very well. I should have given you a 10. I only gave you 7 because I couldn't get the command you gave me to create a sparse file. I figured out 2 things:
1) I couldn't create the sparse file from csh. I had to be in sh or ksh
2) It wouldn't work on an NFS mounted file system.
It took me a while to realize that my home directory is mounted from another host. I didn't use the /tmp directory as in your example. Once I got into ksh and wrote to a local file system, it worked exactly as you said.
Mike
Email: michael.n.daulerio@lmco.com
Michael D'Aulerio
Regular Advisor

Re: Large file with NULL characters

The pax command can also be used to archive & restore sparse files. I had been using cp or tar to copy files from one directory to another and was baffled because the target directory took up much more space than the source.
Email: michael.n.daulerio@lmco.com