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.

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?

share|improve this question
4  
Perhaps if you explain the purpose of what you are trying to do, we could suggest a solution. –  JRT Oct 20 '11 at 8:22

3 Answers 3

No. You must either reference the whole file, or you must copy the part of the file you're interested in.

share|improve this 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.

share|improve this answer

It could be possible (to create a normal symlink pointing to a part of a file) on a filesystem which implements the "file as directory" idea, which appeared--besides other authors--in the plans for reiserfs, but the reiserfs's implementation wasn't liked for the problems the implementation caused.

share|improve this answer

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.