Create link between two existing files

14,015

Solution 1

You need to remove file b.txt previously with command rm b.txt, then create symbolic link with your command ln -s a.txt b.txt.

You could use hard link from b.txt to a.txt, then execute ln a.txt b.txt, both a.txt and b.txt would point the same file on hard drive and removing a.txt doesn't remove file, which could be read through b.txt. With symbolic link from b.txt to a.txt removing a.txt remove file and b.txt symbolic link will be broken. More about hard links: https://en.wikipedia.org/wiki/Hard_link

Solution 2

Always check the man pages for help first. It'll save you a lot of time. Or if you're really busy, an ln --help at the shell gives

Usage: ln [OPTION]... [-T] TARGET LINK_NAME   (1st form) ....

Further down the help text, we find

...
-s, --symbolic  make symbolic links instead of hard links
-f, --force     remove existing destination files
...

which in a sense means, if you want to create a link for a file called my_secret.file as my_secret.link, you'll issue a command like

ln -sf my_secret.file my_secret.link

and if you do an ls -l my_secret.link in here. You'll get something like this

lrwxrwxrwx 1 foouser groupbar  7 Jul 13 17:17 my_secret.link -> my_secret.file

The "l" in lrwxrwxrwx tells us that its a link.

Edit : Rahul struck first.

Share:
14,015

Related videos on Youtube

Special Person
Author by

Special Person

Updated on September 18, 2022

Comments

  • Special Person
    Special Person almost 2 years

    For example: I have file a.txt and file b.txt. I want a link from a.txt to b.txt. If I open/read file a.txt, file b.txt should open/read. If I try something like ln -s a.txt b.txt I get an error because file b.txt exist. How can I create a link from a.txt to b.txt?

  • Special Person
    Special Person almost 8 years
    thanks, can you give me an example?
  • Konstantin Morenko
    Konstantin Morenko almost 8 years
    Example of what?