Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic link to a non existing file

Tags:

unix

symlink

I tried to create a symbolic link to a non existing file

ln -s non_existing_file.txt   $HOME/dir1/dir2/my_symbolic_link

Then I tried to write something in the non existing file using the symbolic link

vi $HOME/dir1/dir2/my_symbolic_link

Now after saving and exiting non_existing_file.txt is created under dir2.

Can someone explain why?

like image 272
buvi Avatar asked May 29 '13 09:05

buvi


People also ask

Can symbolic links point to files that don't exist?

A symbolic link points to a file. In case, the original file is deleted, the symbolic link would be pointing to non-existing file. You can create a symbolic link to a directory too.

What happens to symbolic link when file is deleted?

If a symbolic link is deleted, its target remains unaffected. If a symbolic link points to a target, and sometime later that target is moved, renamed or deleted, the symbolic link is not automatically updated or deleted, but continues to exist and still points to the old target, now a non-existing location or file.

Can you symlink a file?

Symlinks can take two forms: Soft links are similar to shortcuts, and can point to another file or directory in any file system. Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.

Can make relative symbolic links only in current directory?

The reason you get "xyz-file: can make relative symbolic links only in current directory" is because for the source directory, you specified a relative path. It'll work as you want it if you specify an absolute path for the source, like so: "cp -sR /root/absolute/path/name dest". Does not work on all systems.


1 Answers

ln -s target linkpath

creates a symlink at linkpath which holds the name target. Operations on the symlink interpret the name target relative to the directory where the symlink resides, not the present working directory.

So, if you have a symlink holding, say, ../usr in /tmp/link-to-usr, then ls /tmp/link-to-usr will list the contents of /usr (which is /tmp/../usr) regardless of where the ls command is executed.

like image 51
Fred Foo Avatar answered Sep 20 '22 12:09

Fred Foo