2

Linux supports sparse files aka 'file with holes'

Note following commands

alias mystat='stat -c "%n: %B*%b blocks %s bytes"'

dd if=/dev/zero bs=1024k seek=4096 count=0 of=file-with-holes

ls -l file-with-holes
-rw-r--r-- 1 root root 4294967296 Feb 25 18:33 file-with-holes

mystat file-with-holes
file-with-holes: 512*0 blocks 4294967296 bytes

cp file-with-holes other-file

mystat other-file
other-file: 512*0 blocks 4294967296 bytes

cat file-with-holes > file-without-holes

mystat file-without-holes
file-without-holes: 512*8388608 blocks 4294967296 bytes

The cat command removed all holes from the file.

Is there a way to get the holes back?

  • 1
    I see you deleted you answer but check out bsdtar -S - this will create sparse files on extraction (unlike GNU tar). Not really the answer for a full fs, but maybe good if restoring a backup. Also see cpio --sparse. – Graeme Feb 25 '14 at 18:21
  • Thanks, there is also a -S for rsync. I already saved a nuber of hundreds gigs. –  Feb 25 '14 at 19:14

1 Answers1

4
cp --sparse=always file-without-holes another-file-with-holes

Example:

$ cp --sparse=always file-without-holes another-file-with-holes
$ du --apparent-size another
16384   another-file-with-holes
$ du another-file-with-holes
0   another-file-with-holes
Graeme
  • 34,027