Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've created a sparse file with dd. How do I copy contents of another file there, leaving all the zero blocks unallocated?

share|improve this question
    
possible duplicate of Can a file that was originally sparse and expanded be made re-sparse –  Gilles Jan 9 '13 at 23:39
1  
That question and answer are a bit unclear, but I think they show a fundamental misunderstanding. If you overwrite a file using cp, the overwritten file disappears. It doesn't matter if that file used to be sparse; these things are not contagious. –  Tobu Jul 20 '13 at 13:40
    
Interesting, but I assume that the source file still exists. The question is if I can preserve this feature. –  d33tah Jul 20 '13 at 14:01
add comment

1 Answer

The solution is apparently to use cp --sparse=always. My first attempt was with writing some Python code, but unfortunately the MD5 sums didn't match (could anyone tell me why? the code's in edit history).

share|improve this answer
    
The filesystem block size usually isn't 512B - more often it is 4KB. –  peterph Jan 9 '13 at 22:05
    
You say that might be the reason that MD5 sums didn't match? –  d33tah Jan 9 '13 at 22:10
    
No, filesystem gives you zeros where it finds an unallocated block, so the error had to be in your script. The block size is important for other reason: if you seek by 6 empty 512B sectors, then one with some data an then again seek say 4 empty sectors, you save nothing, because the data in the one sector causes the fs to allocate whole 4K block. –  peterph Jan 9 '13 at 22:35
2  
GNU tar also has the -S, --sparse option to handle sparse files effectively, btw. –  peterph Jan 9 '13 at 22:36
    
@d33tah your python code is losing a byte every time it seeks because of the -1 in the seek target, which isn't necessary. You should seek ahead a full 512 bytes each time. –  Wumpus Q. Wumbley Jul 20 '13 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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