LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Symbolic Link (newbie) (https://www.linuxquestions.org/questions/linux-newbie-8/symbolic-link-newbie-4175459907/)

Juc1 04-28-2013 04:14 PM

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...

btmiller 04-28-2013 04:32 PM

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).

Juc1 04-29-2013 06:32 PM

Quote:

Originally Posted by btmiller (Post 4940716)
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...

chrism01 04-29-2013 06:40 PM

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.

Juc1 04-29-2013 07:22 PM

Quote:

Originally Posted by chrism01 (Post 4941482)
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.

alpo85 04-29-2013 07:28 PM

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!

chrism01 04-29-2013 08:21 PM

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

rknichols 04-29-2013 10:59 PM

Quote:

Originally Posted by btmiller (Post 4940716)
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.

chrism01 04-29-2013 11:19 PM

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

:)

btmiller 04-29-2013 11:30 PM

Quote:

Originally Posted by rknichols (Post 4941570)
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.

rknichols 04-30-2013 09:14 AM

Quote:

Originally Posted by chrism01 (Post 4941580)
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?

normanlinux 04-30-2013 11:24 AM

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.

chrism01 05-01-2013 03:08 AM

@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 :( :(


All times are GMT -5. The time now is 07:58 AM.