Ubuntu – What happens to a symbolic link when the original file is replaced

command linedirectoryfilessymbolic-link

I have update software in /opt/minergate-cli. I've renamed the directory minergate-cli to minergate-old with an mv command, then installed a newer version of the software giving the same directory name.

Assuming I had an old program and now a new program by the same name, that being "minergate"
what happens to any symbolic links pointing to the program minergate?

Are they pointing to the original program living in minergate-old, or did the link move to the new program minergate.cli?

Best Answer


  • Symlinks do not follow the files to which they point. If you have link_file -> original_file and you do mv original_file original_file.backup the link_file will be broken

  • If you recreate original filename ( which is what OP did ) the symlink will work again

  • If you have to make link_file -> original_file.backup, you have to delete link_file and create it again, pointing to new filename


What happens to symlink if we rename a file ?

Once you move a file to which symlink points, symlink is broken aka dangling symlink. You have to delete it and create new one if you want to point to the new filename.

For example, let's create a symlink to file:

$ ln -s testfile.txt mysymlink
$ ls -l mysymlink
lrwxrwxrwx 1 adm adm 12 Dec  8 13:28 mysymlink -> testfile.txt

Now let's rename the file. You will see symlink still points to the pathname that no longer exists (which is important, file exists, pathname - does not):

$ mv testfile.txt testfile.txt.bak
$ ls -l mysymlink
lrwxrwxrwx 1 xie xie 12 Dec  8 13:28 mysymlink -> testfile.txt
$ cat mysymlink
cat: mysymlink: No such file or directory

How to fix a broken symlink ?

If you can rename file to original pathname, the symlink will start working.

$ mv testfile.txt.bak testfile.txt $ cat mysymlink one two three

If renaming is not an option and you may not rename the file, create a new symlink and delete the old one.

# break the symlink
$ mv  testfile.txt testfile.txt.bak
$ cat mysymlink
cat: mysymlink: No such file or directory
# remove the old symlink
$ rm mysymlink
# create symlink with same filename but pointing to new pathname
$ ln -s testfile.txt.bak mysymlink
$ cat mysymlink 
one two three

OP question: Did the link move to the new program minergate.cli?

If the symlink target /opt/minergate-cli has been re-created when new version of application was installed, the symlink will point to new file. If the new file has different filename, then symlink will be broken. Symlinks do not follow filename if filename was moved, as in OP's example when they did mv /opt/minergate-cli /opt/minergate.old , so symlink will still keep pointing to /opt/minergate-cli regardless if that file exists or not.

See also

Related Question