Create link between two existing files
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.
Related videos on Youtube
Special Person
Updated on September 18, 2022Comments
-
Special Person almost 2 years
For example: I have file
a.txt
and fileb.txt
. I want a link froma.txt
tob.txt
. If I open/read filea.txt
, fileb.txt
should open/read. If I try something likeln -s a.txt b.txt
I get an error because fileb.txt
exist. How can I create a link froma.txt
tob.txt
? -
Special Person almost 8 yearsthanks, can you give me an example?
-
Konstantin Morenko almost 8 yearsExample of what?