Solved Is it possible to create a symbolic link to a non-existent file?

Hi,
for various reasons I'd like to create a symlink that points to a non-existent file.
Does anyone know if this is possible and if so, how?
Thanks
p.s. the reasons involve read-only NFS mounts...
 
Does anyone know if this is possible and if so, how?
Yes, the exact same way as a symlink to an existing file. For obvious reasons you can only create a soft link as a hard link requires an inode (which won't exist because the file doesn't exist).

Code:
dice@pibsd:~/test % ln -s /some/nonexistant/file myfile
dice@pibsd:~/test % ls -al
total 8
drwxr-xr-x  2 dice  dice  512 Nov  5 11:03 .
drwxr-xr-x  7 dice  dice  512 Nov  5 11:01 ..
lrwxr-xr-x  1 dice  dice   22 Nov  5 11:03 myfile -> /some/nonexistant/file
 
Yes, the exact same way as a symlink to an existing file. For obvious reasons you can only create a soft link as a hard link requires an inode (which won't exist because the file doesn't exist).

Code:
dice@pibsd:~/test % ln -s /some/nonexistant/file myfile
dice@pibsd:~/test % ls -al
total 8
drwxr-xr-x  2 dice  dice  512 Nov  5 11:03 .
drwxr-xr-x  7 dice  dice  512 Nov  5 11:01 ..
lrwxr-xr-x  1 dice  dice   22 Nov  5 11:03 myfile -> /some/nonexistant/file

Huh. How's that for confirmation bias?! I'm sure I tried that before I posted. I'm sure I saw an error. So I went back through my logs and nope: no error when I tried it.

Well, sorry for wasting your time, and thanks for the answer.
 
Here is a funny observation about Unix-style file systems: Soft links are actually arbitrary text strings. There is nothing that prevents you from creating a soft link that says "The quick brown fox jumps over the lazy dog." And in Unix, a text string is any sequence of bytes not including the nul byte, so feel free to put weird binary digits in there to amuse yourself. If you were so inclined, you could just use soft links (instead of files) to store any short files.

The only "magic" about soft links is: those file system functions (some in the user-space run time library) that manage files try to interpret the content of a soft link as a file name and substitute it for a file. But you don't have to do that; you can just create them and read them back.

Another fun fact: like file names, the encoding (utf-8 versus iso8859 versus...) of soft links is not defined. You have to rely on convention and tradition to know what it means.
 
Back
Top