LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-18-2008, 12:53 AM   #1
JosephS
Member
 
Registered: Jun 2007
Distribution: Debian Squeeze, CrunchBang Waldorf
Posts: 514

Rep: Reputation: 38
When to use hard or softlinks


I would like to know the advantages of hard and symbolic links,
and when to use each.

If I make a backup of a directory, with more than 1 hard link to a file, will the contents of the file be copied more than once?
I use mostly rsync for backing up to another drive; sometimes K3b and cp to backup up some things to a cd.

Thanks for advice.

Last edited by JosephS; 06-18-2008 at 12:20 PM.
 
Old 06-18-2008, 11:22 AM   #2
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Someone may correct me here, but a hardlink points directly to a file and is indistinguishable from any other directory listing for that file. Hardlinks can only exist on the same filesystem (for most purposes equivalent to a disk partition) Say you have a file named "example" and create a hard link to it:

$ ln example examplelink

If example is, say a text file, the "file" command will report something like:

$ file example
example: ASCII text

$ file examplelink
example: ASCII text

If you "rm example", "examplelink" will still exist and refer to exactly the same file; The same data within the file is still all there and can be accessed using the name "examplelink" It can even be renamed back to the original name "example" or a new hardlink named "example" can be made.

A symbolic link (symlink) is more like an indirect pointer. It can be thought of as pointing to a directory entry for another file, rather than the file itself. A symlink can point to a file on another filesystem. If you create a symbolic link using the names above,

$ ln -s example examplelink

and then "rm example", then the file has really been removed. "examplelink" is now a broken link. If you use the "file" command now

$ file examplelink
examplelink: broken symbolic link to `example'

Trying to access the file now using "examplelink" will generate an error, since the file does not actually exist anymore.

Most of the time a symbolic link will be what is desired. There is a lot of flexiblility in using them, look up the /etc/alternatives system for an example. It is also used extensively in system startup scripts. To answer your 2nd question, yes, it would be copied twice, so could a symlink; there are options to the handle links in different ways for different copying commands.
 
Old 06-18-2008, 09:03 PM   #3
rajwinder
LQ Newbie
 
Registered: Jun 2008
Posts: 21

Rep: Reputation: 15
qq .. then what is diff between hardlink and a copy ?
 
Old 06-18-2008, 09:13 PM   #4
rajwinder
LQ Newbie
 
Registered: Jun 2008
Posts: 21

Rep: Reputation: 15
one i can think of is that inode will be same in case of hardlink but not in a copy
 
Old 06-18-2008, 09:40 PM   #5
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
A hardlink is magic - it behaves exactly like the file linked to. A file will NOT be deleted until all hardlinks are deleted. In contrast, you can delete the file which a softlink points to.

So with a hardlink you can do something like this:

mkdir orig
mkdir notorig

echo "Hello World" > orig/blahblah
ln orig/blahblah notorig/halbhalb
rm orig/blahblah

cat notorig/halbhalb
> Hello World

Try that with a soft link and you get:
> cat: notorig/halbhalb: No such file or directory
 
Old 06-18-2008, 09:59 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
There is no "magic" to a hardlink. Any file (a bucket of bits) my be referenced by any number of (hard) links. When the last link is removed, storage for the referenced file is removed as well, freeing up disk space.

A symbolic (aka: soft) link is a named reference to a file. When the file referenced is removed, as above, storage is freed, leaving a dangling symbolic link.

Think of (hard) links as named references to a given file in the file system, each indistinguishable from the other.
Think of symbolic links as files that *contain the name of* some other file, and the OS hides this from you the user.

Use a hard link when you want an alternate name for the same bucket of bits within the same file system.
Use a soft link when you want to refer to another file *anywhere* in the directory tree by some given name.

You cannot create hard links to directories - this creates cycles when traversing the directory tree, and was forbidden long ago.
 
Old 06-18-2008, 11:02 PM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
A directory entry contains the file name and a link to the inode structure for the file. A hard link creates another directory entry. The first directory entry is also a link. In other words, hard link <=> directory entry.

Create a hard link to a file and look at "ls -l". One of the fields is the number of links to a file.
Did you ever notice that a directory has at least two links? That is because the first one is the directory entry of the parent directory. The second is the "." entry in the directory itself. Since the parent directory contains a subdirectory, it will have a link count of at least 3. One for it's own "." entry. One for it's parents directory entry. One for the subdirectories ".." entry.

Look at the inode yourself for:
cd ~/Documents
ls -ilad .

cd ~
ls -ild Documents

The inode number will be the same.

Last edited by jschiwal; 06-18-2008 at 11:04 PM.
 
  


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
ldconfig changes softlinks PuppetZ Linux - Software 8 02-27-2007 05:04 AM
softlinks vs hardlinks irfanhab Linux - Newbie 4 02-06-2007 02:05 AM
Softlinks - cannot delete dajomu Red Hat 4 08-17-2005 02:36 PM
Crappy softlinks dajomu Linux - Newbie 4 08-16-2005 03:50 PM
Proftpd + softlinks + no go? BlackRabbit Linux - Software 6 12-16-2003 08:20 AM


All times are GMT -5. The time now is 05:34 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration