Doubts in 'The Linux Command Line'

Carbide

New Member
Credits
30
Screenshot.png

In the first line, the fun shows the original file and fun-sym shows the symbolic link file.
While in the second and third line why is it necessary to add "../" before fun and not only just fun.
Typing only fun gives a broken link.
 


JasKinasis

Well-Known Member
Credits
7,678
View attachment 9769
In the first line, the fun shows the original file and fun-sym shows the symbolic link file.
While in the second and third line why is it necessary to add "../" before fun and not only just fun.
Typing only fun gives a broken link.
Because the sym-links must be either:
A: A relative path to the target file/directory (based on where the new sym-link will actually be).
Or
B: An absolute path to the target file/directory.

From the code you’ve shared, it looks like you’re in your home directory, which can be signified in the shell as $HOME, or by the shell shortcut ~/, or by the absolute path /home/yourusername/, or by the relative path ./ (because it’s the directory we’re currently in.

If you want to put a link in the current directory, that points to a file/directory/other file-system object in the current directory you can just use:
Bash:
ln -s fun fun.sym
Because the target file and the symbolic link are both in the same directory, we don’t need to do anything special.

But if you’re in one directory and you want to create a symbolic link in another directory, the path to the target file/directory/other file-system object must either be:
1. an absolute path to the sym-links target, or
2. a relative path to the sym-links target, specified from the perspective of the sym-link.

So in your second example:
Bash:
ln -s ../fun dir1/fun-sym
You are in your home directory, your target for your sym-link is also in your home directory.

You want the sym-link called fun-sym to be put into a sub-directory called dir1.

So if the sym-link is to be put in dir1/ (which is the same as the relative path ./dir1, or the absolute paths $Home/dir1, or ~/dir1, or /home/yourusername/dir1)
The path to the target must be specified as an absolute path, or as a relative path from where the sym-link will be (and not from where we currently are)

So, going back to your second example:
You are in your home directory. Your target for your sym-link is in your home directory.
You want the sym-link itself to be put into a sub-directory called dir1.
So, if the sym-link is going to be in dir1, then as a relative path, it’s target will be up one directory. So if you are going to use a relative path to the target, the path must be in relation to where the sym-link WILL be. NOT in relation to where we currently are. Therefore, you must use the relative path ../fun for the sym-links target.

Or you could use an absolute path for the target:
e.g.
Bash:
ln -s $HOME/fun dir1/fun-sym
or
Bash:
ln -s ~/fun ./dir1/fun-sym
or
Bash:
ln -s /home/yourusername/fun ./dir1/fun-sym
All three of the above examples specify the target path as an absolute path. And the paths for the sym-links are relative paths.
Note: dir1 and ./dir1 are both exactly the same thing.

Does that make sense now?!
 

Carbide

New Member
Credits
30
Thanks Man

"Because the sym-links must be either:
A: A relative path to the target file/directory (based on where the new sym-link will actually be)."
I didn't know this.
I thought based on where the actual file is.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Latest posts

Top