3

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?

3

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

  • thanks, can you give me an example? – Sam Jul 13 '16 at 11:32
  • Example of what? – Konstantin Morenko Jul 13 '16 at 11:33
1

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.

0

you can use -f option, it removes existing destination files, if any, before creating the link.

ln -sf a.txt b.txt

found in man ln :

[OPTION] 
-f, --force
          remove existing destination files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.