Q. How do I create links under UNIX / Linux operating systems?
A. You need to use ln command, which is a standard Unix / Linux / BSD command, used to create links to files. There are two types of links under UNIX, hard and soft link:
Hard link vs. Soft link in Linux or UNIX
[a] Hard links cannot links directories ( cannot link /tmp with /home/you/tmp)
[b] Hard links cannot cross file system boundaries ( cannot link /tmp mounted on/tmp to 2nd hard disk mounted on /harddisk2)
[c] Symbolic links refer to a symbolic path indicating the abstract location of another file
[d] Hard links, refer to the specific location of physical data.
UNIX Create Symboliclink Command
To create a symbolic link, enter
$ ln -s {/path/to/file-name} {link-name}
$ ln -s /shared/sales/data/file.txt sales.data.txt
$ vi sales.data.txt
$ ls -l sales.data.txt
To delete a link, enter
$ rm {link-name}
$ rm sales.data.txt
$ ls -l
$ ls -l /shared/sales/data/file.txt
If you delete the soft link itself (sales.data.txt) , the data file would still be there ( /shared/sales/data/file.txt ). However, if you delete /shared/sales/data/file.txt, sales.data.txt becomes a broken link and data is lost.
UNIX Create Hardlink Command
To create hard link, enter (without the -s option):
$ ln {file.txt} {hard-link}
$ ln /tmp/file link-here
You can delete hard link with rm command itself:
$ rm {hard-link}
$ rm link-here
If you delete a hard link, your data would be there. If you delete /tmp/file your data still be accessible via link-here hard link file.
See how to create a hard links in Linux / UNIX for more information.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 3 comments… read them below or add one }
I am trying to create a hard link to PHP file after installation of PHP4 as a CGI .
My Current directory is clients cgi-bin and want to create a link to usr/local/php4/bin/php but getting following error:
ln: creating hard link `php’ to `/usr/local/php4/bin/php’: Invalid cross-device link
Hi,
You may delete soft link using below command
# unlink {link name};
I could never get the order of the two things in “ln -s X Y” right, and ended up clobbering Ys with dangling links to X. So I wrote this little sanity-checker wrapper around it: lns.
It does stuff like making sure that your link won’t overwrite anything, and that it’s a link to something that already exists. Just common sense, but sense that I didn’t have.
lns: http://interglacial.com/~sburke/pub/lns.html
I’ve been using it for ten years, and I think it’s the most useful program I’ve ever written. (crontab2english is a runner-up, people tell me)