Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

As the title says, I would like to know the difference between a hard link and a soft link created by the command ln. The command man ln does provide information, but does not sufficiently answer my question.

Also, it would be nice if someone could provide a setting where hard link might be preferable over a symbolic link.

share|improve this question
3  
one of differences... you have some file, for example file test. If you make ln test hardlink, make ln -s test symlink and then move file test to other dir ( or rename ), symlink wont work. Hardlink will work. Now try deleting file test. Hardlink will still work, in fact you will be still able to acces file until number of hardlinks to file isnt 0. Thats because of inodes, it is written in manual... –  Denwerko May 18 '11 at 9:35
1  
I've reopened this because it deserves a good generic answer on this issue (unlike the previous question that was an obscure C example). –  Oli May 18 '11 at 14:50

7 Answers 7

up vote 68 down vote accepted

A hardlink isn't a pointer to a file, it's a directory entry (a file) pointing to the same inode. Even if you change the name of the other file, a hardlink still points to the file. If you replace the other file with a new version (by copying it), a hardlink will not point to the new file. You can only have hardlinks within the same filesystem. With hardlinks you don't have concept of the original files and links, all are equal (think of it as a reference to an object). It's a very low level concept.

On the other hand, a symlink is actually pointing to another path (a file name); it resolves the name of the file each time you access it through the symlink. If you move the file, the symlink will not follow. If you replace the file with another one, keeping the name, the symlink will point to the new file. Symlinks can span filesystems. With symlinks you have very clear distinction between the actual file and symlink, which stores no info beside the path about the file it points to.

share|improve this answer
    
A+ precise answer, thank you! –  Konklone Jul 14 at 2:20

A hardlink can only work on the same filesystem, it is simply a different name for the same inode (files are internally referenced by inodes). A file will only be deleted from disk when the last link to its inode is gone (you rmd or unlinkd the last link). Hardlinks usually only work for files, not directories.

A symlink (symbolic link) is a special file containing a path to another file. This path can be absolute or relative. symlinks can work across file systems, and can even point to different files, if you for example unplug an external hard drive and replace it with another one, which has a different file at the same path. A symlink can point to either files or directories.

share|improve this answer
    
Thanks, this tells me how they work, but what exactly does the hard link do? And why doesn't it work for directories? –  ste_kwr Feb 29 '12 at 18:41
    
@knittl: you sure? It seems on some file systems hardlinks to directories are allowed but only root can create them. See the -d, -F, --directory switches. And yes, I have seen the note in the ln(1) page :) –  0xC0000022L Feb 29 '12 at 19:08
1  
@kniwor: the easiest way to describe hardlinks is "just another name for the same file (that is, data on disk)". And – at least on my system(s) – ln cannot be used to make hardlinks to directories. There exist hardlinks to directories though, the most prominent example being . and ... I didn't want to include that in my original answer, since that would only complicate things. –  knittl Mar 1 '12 at 10:26
2  
@STATUS_ACCESS_DENIED: well okay… but it's usually not a good idea. That's why I wrote »usually« in my original answer. Also see my previous comment for examples. –  knittl Mar 1 '12 at 10:29
    
Thanks, now that I understand it. I realize I have great use for the hard link!! –  ste_kwr Mar 1 '12 at 17:54

Both are pointers to files; the difference is the kind of pointer. A symbolic link points to another file by name. It has a special mode bit that identifies it as a symbolic link, and its contents are the name of the real file. Because it just contains a name, that name does not actually have to exist, or may exist on a different filesystem. If you replace the named file, then the link still contains the same name, and so now it points to the new file. You can easily identify a symbolic link and see the name of the file it points to.

A hard link points to the file by inode number. As such, hard links are no different than the first name of a file. There is no "real" name vs. hard link name; all hard links are equally valid names for the file. Because of this, the file you link to must actually exist and be in the same filesystem where you are trying to create the link. If you delete the original name, then the hard link still points to the same file. Because all hard links are equally valid name(s) for the file, you can not look at one and see the other names for the file; to find this, you have to go looking at every file and compare their inode number to find the other name(s) that have the same inode number.

You can tell how many names a file has from the output of ls -l. The first number after the file mode is the link count. A file with more than 1 link has other name(s) somewhere, and conversely, a file with a link count of only 1 has no (other) hard links.

share|improve this answer

One of the answers from the other thread (now linked from the top of your post) mentions this page which I think is a fairly good medium-level explanation. If you're getting lost in the ascii art, here's the tl;dr version:

  • Standard files are a pointer from the filesystem to an inode which in turn point to physical data. The file component stores its link to the filesystem (essentially its path) and a link to the inode.
  • Hard-links, are just like files. They're just an additional pointer directly to an inode.
  • Symbolic-links are separate files (including separate inode and data) that store a filesystem path to a file.

The kernel and filesystems involved translate everything transparently.

So based on that:

  • Hard-links only allow same-filesystem linking. Symlinks can point at any path.
  • Hard-links (essentially) point to absolute data. Symlinks can point to relative paths (eg ../parent.file)
  • By extension, if you move the target pointer of a hard-link (which, remember, itself is essentially just a hard-link pointing to an inode), the hard-link still works. Moving the target of a symlink would usually break the symlink.
  • Resolving a hard-link would be faster but immeasurably so. That insignificant portion of speed comes at the cost of a inflexible filesystem.

I might have confused myself a little but reading through various things, I'm struggling to find the difference between a standard file and a hardlink. The way I'm reading it is every file consists of a hardlink (storing the filename), linking to an inode that points at physical data.

Adding a hardlink just provides an inode with an additional filesystem-based pointer. Is that right?

share|improve this answer
3  
I think you are right, every file is a pathname for an inode, and a hard link is an additional pathname for the same inode. So a hard link is no different from a normal file. –  enzotib May 18 '11 at 15:20
    
I'm trying to understand this... but you say: > "Symbolic-links are separate files (including separate inode and data) that store a filesystem path to a file." Does a symlink really have separate data? Then its just like a copy of the dir it links to, right? ... and every time something is written to the symlink, it has to be written twice to disk? Makes no sense. –  MiniGod Dec 29 '12 at 23:43
    
@MiniGod No a symlink is an inode to a block of data that stores a path to another inode (filename). Yeah, it's Matrix-like confusing but once you get it, you'll never forget :) –  Oli Dec 29 '12 at 23:48
    
@Oli I might be confused, but when you say: "including separate inode and data ", you mean that the symlink has separate data!? –  MiniGod Dec 31 '12 at 1:34
    
@MiniGod Yeah. The Symlink is an inode pointing to data (just like a normal file) and that data is a path. It's a little more clever than that — to allow transparent usage through symlinks — but that's essentially all they are. –  Oli Dec 31 '12 at 2:21

In Linux/Unix Shortcuts are knows as Link


Link are of two types Soft links (symbolic links ) or Hard link

  1. Soft Links (symbolic links )

    You can make links for files & folder & you can create link (shortcut) on different partition & got different inode number from original.

    If real copy is deleted the link will not work.

  2. Hard Links

    For files only & you cannot create on different partition ( it should be on same partition ) & got same inode number as original

    If the real copy is deleted the link will work ( because it act as original file )


Question: How to make soft link ?

Answer: A soft link can be made with ln -s, 1st you need to define the source and then you need to define the destination ( keep it mind you need to define the full path of source and destination otherwise it will not work )

 sudo ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 /usr/lib32/libGL.so.1
             (----------Source-------)             ( Destination )

enter image description here

As you can see if has different inode and can be made on different partition


Question: How to make Hard link ?

Answer: A Hard link can be made with ln , 1st you need to define the source and then you need to define the destination ( keep it mind you need to define the full path of source and destination otherwise it will not work )

I have a script in /script folder name firefox

 ls -i   ( Shows you the inode )
 5898242 firefox

 ln /scripts/firefox /scripts/on-fire
       ( Source )    ( Destination )

enter image description here

As you can see it has same inode and if I delete the original one the link will work, as it act as original

enter image description here

1st i have check that link is working or not, then i have deleted firefox script


You Question : It would be nice if someone could provide a setting where hard link might be preferable over a symbolic link.

Answer : Depending on disk partition layout is, Hard Link have limitation must be on same partition ( -1 point ) and can be of file ( -1 point ) ) but +1 point is that if original is deleted the link will work as it act as original

where as soft link can be made of folders & files (+1 point) , No partition limitation (+1 point), But only (-1 point) is that if source is deleted the link will not work

share|improve this answer

For an excellent noob-and-ex-Windoze-user-friendly explanation, with nice diagrams and a FAQ check out this page http://www.geekride.com/hard-link-vs-soft-link/. Their copyright restrictions prevent me from excerpting their stuff, so suffice it that I provide the link here.

This is my 2nd or even 3rd attempt at grasping the soft/hard-link enigma, always throwing in the towel and postponing my understanding to some indefinite point of time in the future -- as soon as the explanations and man-pages get deeply kerneled and over-technical with inodes and all...

Enjoy!

share|improve this answer

When to use Soft Link:

Link across filesystems: If you want to link files across the filesystems, you can only use symlinks/soft links.

Links to directory: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.

When to use Hard Link:

Storage Space: Hard links takes very negligible amount of space, as there are no new inodes created while creating hard links. In soft links we create a file which consumes space (usually 4KB, depending upon the filesystem)

Performance: Performance will be slightly better while accessing a hard link, as you are directly accessing the disk pointer instead of going through another file. Moving file location: If you move the source file to some other location on the same filesystem, the hard link will still work, but soft link will fail.

Redundancy: If you want to make sure safety of your data, you should be using hard link, as in hard link, the data is safe, until all the links to the files are deleted, instead of that in soft link, you will lose the data if the master instance of the file is deleted.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.