Symlink to part of the file

filesystemssymlink

Is there possible to create file that is just a sub-sequence of bytes from another file, like a symlink, but referencing only part of the file?

Best Answer

Yes, it is (somewhat) possible at least on Linux with some limitations.

The method is to create a read-write loop device that maps to a subset of the file.

eg:

#!/bin/bash
for ((i=0;i<10000;i++)); do
    printf "%7d\n" $i
done >/var/tmp/file
losetup -v -f -o 512 --sizelimit 512 /var/tmp/file
losetup -a
head -2 /var/tmp/file
echo ...
tail -2 /var/tmp/file
echo ===
head -2 /dev/loop0
echo ...
tail -2 /dev/loop0 
printf "I was here" > /dev/loop0
grep here /var/tmp/file
losetup -d /dev/loop0

output:

loop device: /dev/loop0
/dev/loop0: [0808]:136392 (/var/tmp/file), offset 512, size 512
      0
      1
...
   9998
   9999
===
     64
     65
...
    126
    127
I was here   65

I believe both the offset and the size must be multiple of a block size (512 bytes).

You probably need to be root to create and access the loop device.

If you need a symlink, you can create one that points to the loop device.

Related Question