Linux Classes
Share This With a Friend  

Linux Ln Command

How Do I Link Linux Files?

You can link files together in two ways: with hard links or symbolic links. Creating a Hard Link

The ln command lets a file on your disk be accessed with more than one file name by hard-linking a new file name to it. When you hard-link a file, you create a new file name and connect it with the original file. Hard-linking can let two users share the same file or provide a more convenient way of referencing files buried deep in a series of directories.

Here's an example. Suppose hermie and sigmund are working on the Top Secret Snowgun Project, and the formula for the Snowgun is stored in /home/hermie/projects/snowgun/formula.txt. Sigmund doesn't want to type that long, ugly file name every time he needs to update the file, so he creates a hard link to the file, like this:

ln /home/hermie/projects/snowgun/formula.txt sgformula

The preceding command links the file name sgformula to the file formula.txt contained at the end of that directory string. There's still only one copy of the snowgun formula (formula.txt) on the disk, but now sigmund can access it quickly with the name sgformula (assuming hermie gives him write permission to the file). Interestingly, if hermie deletes his projects/snowgun/formula.txt file, Linux will not remove the file from the file system because sigmund still has a link to it.

Think of the ln command as a way of creating a kind of nickname for a file. By the way, ther e is no parallel to this file linking concept in the DOS world--you'd have to create a second copy of the file and keep the various copies in synch manually to get a similar result.

Creating a Symbolic Link

The type of link we just created is called a hard link. There's another type called a symbolic link. Symbolic links (also called symlinks) work like hard links, but you can do a bit more with them. If you want to create a link to a directory (as opposed to a file), you must create a symlink. Symlinks are also required when linking to a file on a different disk partition or on a network.

To create a symbolic link, add the -s parameter to the ln command, like this:

ln -s /dev/fd0 A:

ln -s /etc/httpd/conf web

The first example allows you to access the floppy drive as drive A, just like in DOS, and the second creates a directory entry called web that can be used instead of /etc/httpd/conf.

Previous Lesson: Task Scheduling
Next Lesson: The Vi Editor



Comments - most recent first
(Please feel free to answer questions posted by others!)

slimstan1     (06 Mar 2015, 04:28)
How can you link two existing files in the same dir together? eg file A and file B, and both files contain different useful information for the program.
sai     (07 Jul 2014, 19:01)
how to create symbolic link to existing folder ? for example i would like to create link to folder ARCHIVE which is existing in /main to point to in /main/sfiles/abc/gather/ARCHIVE ?
naser     (09 Jun 2014, 05:36)
How can I search only link files in Linux?

Joseph Whitehead     (02 Sep 2012, 04:36)
Well there's no analog in FAT12/16/32 ('DOS') other than the Windows '.LNK' (shortcuts) kludge, there IS support for similar functionality in NTFS. AFAIK, there is no standard 'ln' command equivalent, though. In Windows XP for example, you'd do this for folders in Disk Management. Sysinternals has a 'junctions' command for Windows. Schinagl's site has 'ln.exe' that is similar. Note that these are 'hard links' in Linux terminology.
Majed     (14 Mar 2012, 12:51)
@summit i think you can study a suse course and become a suse certified expert but in these days it is not enough to be a linux expert to get a job you have to know how to program and know a bit about configuring switches and routers and that is why a university degree is needed, alternatively, you could study related courses outside of university but you'll find it harder to find work with no degree. i hope this helped :)
sumit kumar india     (14 Mar 2012, 12:30)
good evening sir,
i m student persuing in computer science . now a days i am learning linux from an institute for my major project.i want to make linux as my career for my future so i want to know can i learn linux on ur website rather than goiong to institute and without paying a big amount of money.i want to gain complete knowledge of linux, so that i can earn money by doing job.
so please sir i request you, please advice me. thanks.
sumit kumar
tinnitus remedies     (05 Mar 2012, 01:33)
EuDmVP A round of applause for your article.Really looking forward to read more. Much obliged.
Anoop     (01 Feb 2012, 02:41)
Can you share the command to link a unix directory to a window directory?
Jon     (29 Nov 2011, 08:27)
Hi, my name is Jon Säfström and i have some questions! How do u uninstall Linux?
Majed     (08 Sep 2011, 10:35)
i did a symlink by the following command:
srvmon plugins # ln -s /usr/lib/nagios/plugins plugins
but how to access it using the symlink?
i tried:
srvmon / # cd plugins
i get:
-bash: cd: plugins: No such file or directory
rahul     (28 Jul 2011, 06:40)
thank u sir it is good way to share knowledge...........
thank u so much and pls put more shell script programm
vidit     (30 Mar 2011, 00:46)
Bob, i want know that how a user like hermie or root permit to other user..As you are talking about hard linking only if hermie allows to sigmund ,then sigmund can access the how do user permit to others?
hury     (13 Mar 2011, 12:43)
hello,i am happy to see your article.i use linux os fisrt time,about 1 hour,honestly speaking,this tonight,i want to use my os. when i want to study how to use a editor vi ,i search with google, then i come here. i find read you article,i learn how to use it .thanks.
orthus     (19 Jan 2011, 04:20)
Finally I found a Linux manual that I can understand. I read a lot about symbolic links but I didn't understand them until I found your article, thanks!!
Sir     (08 Jan 2011, 03:34)
Just stopping by to say I've been doing your tutorials and that they are the best beginner linux tuts out there. Would you mind if I convert all your tuts to PDF? I will give you absolute recognition of course. Please email me
sathish s     (04 Jan 2011, 06:55)
dear bob: can u pls explain me abt soft link
Yared     (09 Dec 2010, 03:17)
Thanks a lot for your short and precise explanation of ln command usage!
Bob Rankin     (10 Nov 2010, 21:48)
@Thamer - A link acts like a file, delete it the same way.
thamer     (18 Aug 2010, 09:12)
I want to delete the link that i have created
what is the command that help me doing this.
mirza     (09 Aug 2010, 01:01)
The number that is shown in the second column of output of ll, for files in hard link, i want to know what is it for directories?
for example /etc show 128 in that column, what is this number? is this a hard link or what?
thank you
reema alnsirat     (31 Jul 2010, 10:27)
Thank you for this important and great information
Eric     (12 Jun 2010, 14:37)
Devendra: What you saw does not contradict the article. When "instrahu" deletes the file, his reference to the physical file is removed. From his perspective, it's gone. The file is not actually deleted because a second reference still exists, the hard link created by "db2inst1". If "db2inst1" deletes his link, that was the last reference to the physical file and the file is removed from the filesystem.

Barbara: I believe that answers your question also. :)
Matt     (29 Apr 2010, 05:20)
Great explanation...makes perfect sense and nice quick simple read. Thanks!!!!!!!
Barbara     (13 Apr 2010, 02:11)
In the hard link example, if Hermie deletes the file, Linux keeps it. What happens if Sigmund deletes the link? Is the file finally deleted?
Devendra     (12 Apr 2010, 00:29)
Hey I found opposite to whatever you are stating for hard links.I had a file "Proactive_monitoring.out" on user "instrahu" and created the hardlink from another user "db2inst1"

1. I was able to delete the file even after creating the hard link.
2. Even after deleting the file I was able to access that file from other user from where I created the hard link. This was only possible if a local copy exist of the linked file and yes I found it on the same path where I created the hard link.
Ravster     (08 Apr 2010, 00:12)
hey thanks had to link to my htdocs directory and you made my day..easy simple..fabulous!! thanks homes
barrank     (19 Mar 2010, 19:07)
@moez: Try 'man ln'.

Bob, this site is great!!!
moez     (22 Feb 2010, 06:56)
i want to ask you if there is more options for 'ln' command .
moez     (22 Feb 2010, 06:54)

thank you for this interesting course
suraj     (15 Feb 2010, 08:53)
great help

I welcome your comments. However... I am puzzled by many people who say "Please send me the Linux tutorial." This website *is* your Linux Tutorial! Read everything here, learn all you can, ask questions if you like. But don't ask me to send what you already have. :-)

NO SPAM! If you post garbage, it will be deleted, and you will be banned.
Notify me about new comments on this page
Hide my email

Ask Bob Rankin - Free Tech Support

Copyright © by - Privacy Policy
All rights reserved - Redistribution is allowed only with permission.