
What are they?
Symbolic links are like shortcuts or references to the actual file or directory. Most of the time these links are transparent when working with them through other programs. For example you could have a link to a directory to one hard drive inside a directory that lies on a different hard drive and an application will still treat it the same.
Symbolic links are used all the time to link libraries and make sure files are in consistent places without moving or copying the original. Links are often used to “store” multiple copies of the same file in different places but still reference to one file.
Creating a Symbolic Link
To create a symbolic link in Linux we use this syntax:
What happens if I edit the link? Any modifications to the linked file will be changed on the original file.
What happens if I delete the link? If you delete the link the original file is unchanged. It will still exist.
What happens if I delete the original file but not the link? The link will remain but will point to a file that does not exist. This is called an orphaned or dangling link.
Creating a Hard Link
You can create a hard link like so:
The Difference Between Soft and Hard Links
Hard links
- Only link to a file not a directory
- Can not reference a file on a different disk/volume
- Links will reference a file even if it is moved
- Links reference inode/physical locations on the disk
Symbolic (soft) links
- Can link to directories
- Can reference a file/folder on a different hard disk/volume
- Links remain if the original file is deleted
- Links will NOT reference the file anymore if it is moved
- Links reference abstract filenames/directories and NOT physical locations. They are given their own inode
Practice Makes Perfect
To really grasp the concept of symbolic links lets give it a shot.
Go into the tmp directory:
Make a directory
Copy over the host.conf file or any file to test with.
cp /etc/host.conf .
List the contents and take note of the inode (first column)
Create a symbolic link to host.conf called linkhost.conf
Now do list out the inodes
Notice how the inode for the link is different.
Now create a hard link to the same file
Now list the inoes one more time
Notice how the inode numbers are exactly the same for the hard link and the actual file.
Lets try some file operations now
Open up linkhost.conf and edit it and save it
Now look in host.conf and notice that the changes were made
Lets move host.conf now and see if it causes any problems
Uh oh, now when we list the directory our link turned red lets see what is in side it
It looks like our symbolic link is now broken as it linked to a file name and not the inode. What about our hard link?
Looks like our hard link still works even though we moved the original file. This is because the hard link was linked to the inode the physical reference on the hard drive where the file resides. The soft link (symbolic link) on the other hand was linked to the abstract file name and was broken the moment we moved the file.
This leads to an interesting question. What happens if I delete a hard link? Even though the hard link is referenced to the physical location of the file on the hard drive though an inode, removing a hard link will not delete the original file.
Symbolic links will remain intact but will point to a non existent file.
Was this information useful?
11 Responses
-
Slightly off topic (maybe)
You can use symbolic links to control behaviour of scripts. Consider following snippet:
#!/bin/bash
case $0 in
“path/to/script”) do something;;
“path/to/symbolic_link”) do something else;;
esaclots of other clever stuff
exit; -
drbobb
6-7-2009
you wrote:
“if you remove a the original file all hard links will also disappear.”
to which I say: Huh?
-
Anon
6-7-2009
Removing the original file does not remove the hardlinks. They are still acessible.
-
Andrew
6-7-2009
Hard Links: “Links are removed if the original file is deleted”
This is not true. A file exists in one block on disk (for simplicity), each inode in the filesytem will point to this block. The block will have a reference count (the number of inodes referencing this block). The block is not recovered “marked free” unless the reference count is zero. So, for the file to be deleted all links must be removed. Removing one hard link does not affect the other links until they are all gone.
-
Rhabarberkuchen
6-8-2009
Hard links are not removed when the original file is removed. In fact, the operating system can’t distinguish between the original and the hard link.
The OS keeps a counter how often a certain file is linked to via hard links (including the initial reference, which is nothing else but just a hard link). When that counter reaches zero, the file is removed.
If you post a “Understanding …” entry on a site named “nixtutor”, do a little research first, please.
-
When you remove a file, you remove a (hard) link. That’s why the Unix syscall is called unlink(). Most files have one link. Your hard link commands above show creating an additional link. At that point, removing one link will still leave the other. When you remove the last link, the file is finally removed. Try it!
-
Mark Sanborn
6-8-2009
Opps it looks like I made a mistake when talking about hard Links. The post is updated.
Thanks for the comments guys
-
clarity4sure
6-16-2009
Dude, love the Practice Makes Perfect bit, I’m new to unix and find a good deal of forum posts (and other posts) to be a bit pretentious in their premise that clarity is universal about some, what may appear to be, basics. What I’ve discovered is this is an incorrect premise and generally some simple (and quick) justification of the context of the posting adds heaps to not only the validity of the information, but the overall usefulness, by providing a ‘ball park’ for the remarks. Thanks. Mark
-
[...] typically used to sync files that reside in the /home/Dropbox directory we are going to make use of symbolic links to link files outside this directory in an attempt to sync config files across multiple Linux [...]
-
mib
9-22-2009
If you want to completely remove a file and its hard links, all in one shot, another way would be removing the file by its *inode number* (ls -il), using find:
find . -inum 192222 -exec rm {} \;
BTW, same method can be used to remove/rename bad filenames.
-
[...] Symbolic Links: http://www.nixtutor.com/freebsd/understanding-symbolic-links/ [...]




6-6-2009