Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


How do Unix / Linux "hard links" work?

A reader writes:
So, I'm partway through your book, and I want to check on hard links. When you say it creates another door to the same body of data, is that literally true? For example, if all I have in my ./Documents directory is 001.doc (size 512K), and I hard link as hardlink.doc then run ls, do I see A or B?

A) 001.doc 512K
hardlink.doc 512K
total 1024K

B) 001.doc 512K
hardlink.doc 512K
total 512K

If it's not B, I'm not sure what the point is, other than maybe not changing the timestamp.


Dave's Answer:
An interesting question! The main point of hardlinks is to allow a single data entity to be shared by multiple entries in the file system. If I saved this posting as "001.html" on my server, then created a hard link to the same file from "002.html", then I could subsequently delete "001.html" and "002.html" would be unaffected, with the same contents within. It's only when all links to a data entity are removed that the data gets hooked into the free list and is essentially recycled for later reuse.

In terms of your specific question, the system will view the two files as having separate content. This can be demonstrated thusly:

$ du 
249     .
$ ls -l
total 248
-rw-r--r--  1 dtint  vuser  247034 Aug 24 03:24 some.data

Now let's create some hard links. Notice "ls" reports 248 blocks in this directory

$ ln some.data link2
$ ln some.data link3
$ ln some.data link4
$ ls -l
total 992
-rw-r--r--  4 dtint  vuser  247034 Aug 24 03:24 link2
-rw-r--r--  4 dtint  vuser  247034 Aug 24 03:24 link3
-rw-r--r--  4 dtint  vuser  247034 Aug 24 03:24 link4
-rw-r--r--  4 dtint  vuser  247034 Aug 24 03:24 some.data

As expected, "ls" now reports 992 blocks in use, seeing each of the new hard linked 
files as new data. But... notice what "du" reports, since it actually looks deeper into 
the file system:

$ du 
249     .
As you can see, some elements of the file system see hard links as new and separate data entities (e.g. "ls"), while others dig further into the file system to ascertain actual data blocks used and associated with the current location (e.g., "du"). This makes sense because otherwise if you had lots of hard links you could theoretically end up in a situation where you have greater than 100% disk space utilization, which would clearly be a bit confusing at best!

Related Unix and Linux Help articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  








Reader Comments To Date: 3

kjteoh said, on June 17, 2005 12:00 PM:

Another way to look at hard links - they share the same inode no matter what. Do this with ls -i and it will become obvious. The disadvantage of hard links is that you cannot hard link across 2 separate drives on your system ... say hda and hdb (when you can with soft or symlinks)
kjteoh

anjan said, on May 22, 2009 5:28 AM:

the information was very use full.
Thanks !!

please post some on cron jobs. Thanks in advance

Azeem said, on June 24, 2009 9:23 PM:

I just tested hardlinks on an NFS share, and it worked! .. i knew that symbolic links didn't work on NFS so i tried hard links and it worked

that leads me to believe you can also hard link between different harddrives or can anyone explain why it wouldn't work?

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

I do have a comment, now that you mention it!











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.