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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With