LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2013, 01:22 AM   #1
myzoneajay
LQ Newbie
 
Registered: Jan 2013
Posts: 7

Rep: Reputation: Disabled
Symbolic links


I am learning linux from the book
The Linux Command Line - A Complete Introduction By William E. Shotts

I was learning how to create symbolic links, then I got to this page where I am facing this problem:
We have created a directory called Playground which contains 2 directories: dir1 & dir2 ; and a file called fun ; and a hard link to that file called fun-hard.
Now it is written in the book that to create a symbolic link we use
ln -s ../fun dir1/fun-sym
& this command is being run when present working directory is playground directory.
.. (2 periods) means parent directory. In this case it would be parent directory of playground, that is home directory. But ../fun means a directory named fun inside the home directory, which is wrong becauz there is no such directory.
Shouldn't it be ./fun instead of ../fun
Plz see this image for more clarity:
http://s18.postimage.org/p8k8i3wa1/image.png
Attached Thumbnails
Click image for larger version

Name:	1.PNG
Views:	13
Size:	113.4 KB
ID:	11856  
 
Old 02-16-2013, 01:47 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Welcome to LQ!

Before any explaination, I'd suggest you to once thoroughly go through the manual of ln command (see here).

Take a note:-
- A symbolic link is nothing but a shortcut to another file or directory.
- A . in Unix means current directory, whereas .. stand for parant directory of your current working directory i.e. one level up from your current directory in file system hierarchy.

In the screenshot you shared:-
User me is in playground directory. fun directory is inside playground directory. So:-

Code:
[me@linuxbox playground]$ ln -s fun fun-sym  _______________ Means you've created a symbolic link of fun directory named fun-sym
[me@linuxbox playground]$ ln -s ../fun dir1/fun-sym ________ Means there's one more directory outside of playground with same name i.e. fun and you've create a symbolic link of that fun directory in dir1. Here target directory is fun and it's link is residing in dir1 with name fun-sym.
[me@linuxbox playground]$ ln -s ../fun dir2/fun-sym ________ Means one more directory outside of playground with same name i.e. fun and you've created a symbolic link of that fun directory in dir2. Here target directory is fun and it's link is residing in dir2 with name fun-sym.
Later, when you're checking links, as:
Code:
[me@linuxbox playground]$ ls -l dir1
...... fun-hard          _____________ Means a hard link of fun directory
...... fun-sym -> ../fun _____________ Means a symbolic link named fun-sym of directory playground/fun
I suppose, that directory hierarchy is confusing you. So you can try the same on your test machine with different directories for clear understanding.

Just remember the syntax of ln command:
Code:
ln [OPTION] TARGET LINK_NAME

Last edited by shivaa; 02-16-2013 at 09:33 AM.
 
Old 02-16-2013, 09:26 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,246

Rep: Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404Reputation: 1404
Quote:
Originally Posted by myzoneajay View Post
this command is being run when present working directory is playground directory.
.. (2 periods) means parent directory. In this case it would be parent directory of playground,
No. A relative symbolic link (one that does not begin with '/') is relative to the directory where the symbolic link resides. It has nothing to do with the CWD of the process that is following the link.
 
1 members found this post helpful.
Old 02-16-2013, 09:35 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,398

Rep: Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017Reputation: 2017
Quote:
Originally Posted by rknichols View Post
No. A relative symbolic link (one that does not begin with '/') is relative to the directory where the symbolic link resides. It has nothing to do with the CWD of the process that is following the link.
^ This

Which can be quite confusing sometimes. The thing to remember is when you're making a symbolic link, you write the target of the link as if you're in the directory that contains the link itself, even if you aren't. So in fact

Code:
ln -s ../fun dir1/fun-sym
Will create a link called fun-sym in dir1, that points to a file called fun in your CWD (since you are currently in dir1's parent). In fact all of these examples will give the same result:
Code:
$ cd $HOME/playground
$ ln -s ../fun dir1/fun-sym
Code:
$ cd $HOME
$ ln -s ../fun playground/dir1/fun-sym
Code:
$ cd /whatever/directory/you/want
$ ln -s ../fun $HOME/playground/dir1/fun-sym
The way you write the path to the link itself depends on your CWD (unless you give the full path), but the link target is always the same.

If you were to write
Code:
ln -s fun dir1/fun-sym
it would result in a broken link, since fun-sym would be pointing to a file called "fun" in ITS CWD, which doesn't exist

Last edited by suicidaleggroll; 02-16-2013 at 09:42 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Symbolic links Matx Linux - Newbie 4 01-02-2013 09:57 AM
Symbolic links Vs Hard links sulekha Linux - General 2 10-02-2008 07:03 AM
Symbolic Links petercool Linux - General 2 08-20-2003 11:15 AM
Symbolic links pdstatha Programming 1 04-08-2002 09:50 PM
symbolic links winger Linux - General 5 04-06-2002 05:16 PM


All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration