1

I'm trying to make a sparse file on android. For this I'm using Android Terminal Emulator. I have installed Busybox so I can use the dd command. Other commands like truncate aren't installed. My question: does anyone know how to make a sparse file with android terminal?

I've tried some commands I found on internet:

dd if=/dev/zero of=/sdcard/file.img bs=1 count=0 seek="wanted size"    

But no file would be made. I also tried:

dd if=/dev/zero of=/sdcard/file.img bs="wanted size" count=1.   

But I would get the following error:

dd "path of if" invalid argument    

Does anyone know how to get it right?

1
  • Try to be more specific about "some kind of error".
    – cYrus
    Jul 14 '15 at 11:14
1

I've already found the answer myself.

dd if=/dev/zero of=/sdcard/file.img bs=1 count=0 seek="wanted size"    

Would not make a file since the size on the drive is determined by the block size and count. By setting the count to 0 the file would be 0 bytes and being so, it wouldn't exist.

dd if=/dev/zero of=/sdcard/file.img bs="wanted size" count=1    

Would give the following error:

dd "path of if" invalid argument    

The problem here is that there's a limit to the block size. It can't be set that big. I needed to calculate the count for the wanted block size. I chose 64k. This is the working command for making a 512 MB sparse file:

dd if=/dev/zero of=/sdcard/file.img bs=64k count=8192    

Using seek is optional.

1
  • 1
    Using seek is not optional. What you created is not a sparse file at all.
    – Daniel B
    Jul 16 '15 at 11:13
0

The following command will create a 5 gigabyte sparse file on my device
dd of=sparse.img bs=1 count=0 seek=5G

dd is a program for copying blocks of bytes.
if= is used to specify the input file. Not needed here.
of= is needed to specify the output file.
bs= is needed to specify block size, aka bytes per read or write.
seek= is needed to specify the number of output blocks to skip.
count= is used to specify the number of blocks to copy.

The default behaviour when count isn't specified is to copy the entire input file (which defaults to stdin). bs should normally be set to the optimal read/write size of the devices, but in this case setting it to 1 allows us to use human readable numbers for seek. Both seek and count take their values in blocks as defined by bs such that bytes=bs*(count+seek).

A sparse file is created using count=0 and instead using seek to set the file size. The accepted answer doesn't create a sparse file at all, instead filling the file with zeros.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.