LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-28-2013, 05:14 PM   #1
Juc1
Member
 
Registered: Sep 2011
Location: UK
Posts: 78

Rep: Reputation: 0
Symbolic Link (newbie)


Hi all,

I am trying to understand what a symbolic link is / does.

Using this example...

Code:
$ ln -s /usr/local/apache/logs ./logs
...I think that /usr/local/apache/logs is the existing file and ./logs is the new file I am creating which will link to /usr/local/apache/logs.

Can anyone please tell me have I got that right? If so what would I expect to see if I do
Code:
$ cat ./logs
? And what is the purpose of creating a symbolic link?

Thank you...
 
Old 04-28-2013, 05:32 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,275

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
You have it correct ... the syntax is:

Code:
ln -s existingfile newname
In your existing example, you would see the content of "/usr/local/apache/logs" when you cat ./logs (however, /usr/local/apach/logs is probably a directory, so you'll get an error message).

The reason for making a symbolic link is usually for convenience, or if for some reason you have to be able to refer to a file or directory via a different path. This can be convenient if, for example, a program puts its log files in a particular directory, but due to disk space limitations you want them in a different directory on another partition. You might also want to move some files or directories, but leave a link to them at the original location.

Note that if you create a symlink to a file and then delete that file, the symlink will not be deleted, but there will be an error (no such file or directory) if you try to read or write its contents. This can be avoided by having a so-called hard link (use ln without the "-s" flag), which will increment the link count of the inode in question. Note that hard links cannot cross filesystem boundaries and that only root may hard link to directories (since it's easy for the unaware to create infinite loops of hard links).
 
1 members found this post helpful.
Old 04-29-2013, 07:32 PM   #3
Juc1
Member
 
Registered: Sep 2011
Location: UK
Posts: 78

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by btmiller View Post
You have it correct ... the syntax is:

Code:
ln -s existingfile newname
In your existing example, you would see the content of "/usr/local/apache/logs" when you cat ./logs (however, /usr/local/apach/logs is probably a directory, so you'll get an error message).
Ok thanks so can I put it like this - the ln -s command creates a file which is itself empty but which mirrors the content of another file (not a directory) so it is like creating a copy of the original file in a new location. The new file does not need to have any particular extension so you would not know by the file name or by the cat command that it has been created by the ln - s command.


Here is a real case of a symbolic link I have seen for the program 'example' -

Code:
$ ln -s /home/www/example/config/apache.conf /etc/apache2/conf.d/example.conf
So /home/www/example/config/apache.conf is part of the original installation of example and example.conf is the new file that is effectively a copy of /home/www/example/config/apache.conf in the /etc/apache2/conf.d/ directory. Any ideas please what is happening in this case? I guess it is something like this - "when apache 2 is doing stuff it should include the file /home/www/example/config/apache.conf ie as if the file /home/www/example/config/apache.conf existed in the directory /etc/apache2/conf.d, so kind of pulling the /home/www/example/config/apache.conf file into the /etc/apache2/conf.d/ directory."

Have I got that right and if so is this effectively the same as copying the file /home/www/example/config/apache.conf to /etc/apache2/conf.d/ and renaming it example.conf?

Thanks...

Last edited by Juc1; 04-29-2013 at 07:35 PM.
 
Old 04-29-2013, 07:40 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
ln does NOT create a copy, it creates a link (ie a pointer).
See btmiller's comment
Quote:
Note that if you create a symlink to a file and then delete that file, the symlink will not be deleted, but there will be an error (no such file or directory) if you try to read or write its contents.
 
Old 04-29-2013, 08:22 PM   #5
Juc1
Member
 
Registered: Sep 2011
Location: UK
Posts: 78

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
ln does NOT create a copy, it creates a link (ie a pointer).
See btmiller's comment
Yeah thanks I realise it is not a copy because the new file has no content of its own but I meant that it seems that original file + create link file is in effect (as long as the original file is not deleted) similar to original file + create copy of original file.
 
Old 04-29-2013, 08:28 PM   #6
alpo85
Member
 
Registered: Nov 2012
Posts: 30

Rep: Reputation: Disabled
I've had similar questions, if someone could entertain them.

Correct me if I'm wrong, it sounds like Linux sym links are akin to Windows "Shortcuts." In which ways are these two kinds of pointers similar/different?

Also, as I understand, hard links are basically pointers to data (rather than the file with sym links). It sounds like, if I edit the main file, the hard link changes with it. If I delete the file, what happens to the hard link? An error? In which ways are hard links different to sym links?

Thanks in advance!
 
Old 04-29-2013, 09:21 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
symlinks can cross filesystem (partition) boundaries, hard links cannot.
The data is not removed until all hard links have been removed.
Good article here http://linuxcommando.blogspot.com.au...ard-links.html
 
Old 04-29-2013, 11:59 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by btmiller View Post
only root may hard link to directories (since it's easy for the unaware to create infinite loops of hard links).
It's been a long time since even root could make a hard link to a directory. Code in several places makes the tacit assumption that the directory structure is a tree, and making additional hard links to a directory breaks that.
 
Old 04-30-2013, 12:19 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Hate to argue but
Code:
 ln x.x x1.x
 ls -li x*

1311649 -rw-rw-r--. 2 chris chris 31 Apr 30 14:17 x1.x
1311649 -rw-rw-r--. 2 chris chris 31 Apr 30 14:17 x.x
 
Old 04-30-2013, 12:30 AM   #10
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,275

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by rknichols View Post
It's been a long time since even root could make a hard link to a directory. Code in several places makes the tacit assumption that the directory structure is a tree, and making additional hard links to a directory breaks that.
Heh ... you're correct. I haven't tried this for years and years. It used to be that the kernel would let root make a hard link to a directory, but I guess that's fallen by the wayside, at least on Linux.
 
Old 04-30-2013, 10:14 AM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by chrism01 View Post
Hate to argue but
Code:
 ln x.x x1.x
 ls -li x*

1311649 -rw-rw-r--. 2 chris chris 31 Apr 30 14:17 x1.x
1311649 -rw-rw-r--. 2 chris chris 31 Apr 30 14:17 x.x
What point are you trying to make?

Last edited by rknichols; 04-30-2013 at 10:19 AM.
 
Old 04-30-2013, 12:24 PM   #12
normanlinux
Member
 
Registered: Apr 2013
Distribution: Arch and SuSE
Posts: 135

Rep: Reputation: Disabled
Original (hard) links are just another name for the same file. When AT&T created unix everything could be viewed from the perspective of the system or the user. The system didn't care about names it used numbers - the (first) inode for the file. ALL file names were links to that inode.
This was easy to see in early system V where directory entries were stored as name/inode pairs. Hence a single hard link to a file causes the link count to show as 2.

BSD introduced the (very useful) concept of symbolic links. The file actually stored the name of the file it pointed tto, but not inside the file, inside its inode (I believe, memory getting rusty).

Yes, Windows implemented a similar system which they call shortcuts.

BTW as should be clear from above, when a file has a hard link
ln file1 fle2
and you delete either of them the other stays as is. They are just 2 names for the same thing. The file is only removed when link count drops to 0.
 
Old 05-01-2013, 04:08 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
@rknichols:
I was commenting on
Quote:
It's been a long time since even root could make a hard link to a directory
Just realised I need glasses; somehow missed the 'hard' link bit; must have been late at night for me.
(and that was a file, not a dir grrrr) .
Please ignore previous post
 
  


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] Can I make a symbolic link that is dynamic link->$HOME/file? croog Linux - General 4 10-14-2012 08:55 PM
Symbolic link not allowed or link target not accessible rheluser Linux - Server 3 09-04-2012 11:22 AM
symbolic link not allowed or link target not accessible paullaubscher Linux - Newbie 4 03-10-2010 07:47 AM
How to create symbolic link to html file and avoid page link problem? haxpor Linux - General 2 01-29-2008 08:40 AM
Not a symbolic link ?? attockonian Linux - Newbie 4 06-30-2006 09:09 PM


All times are GMT -5. The time now is 08:39 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