{"id":18,"date":"2010-02-02T17:28:54","date_gmt":"2010-02-02T17:28:54","guid":{"rendered":"http:\/\/www.vidarholen.net\/contents\/blog\/?p=18"},"modified":"2010-02-02T17:28:54","modified_gmt":"2010-02-02T17:28:54","slug":"whats-up-with-directory-hard-link-counts","status":"publish","type":"post","link":"https:\/\/www.vidarholen.net\/contents\/blog\/?p=18","title":{"rendered":"What&#8217;s up with directory hard link counts?"},"content":{"rendered":"<p>Ever considered the hard link count from ls on directories? <\/p>\n<pre> \r\nvidar@kelvin ~\/src $ ls -l\r\ntotal 108\r\ndrwxr-xr-x  <strong>4<\/strong> vidar vidar  4096 2009-11-22 12:52 aml-lsb\r\ndrwxr-xr-x <strong>13<\/strong> vidar vidar  4096 2009-12-13 16:00 delta3d_REL-2.4.0\r\ndrwxr-xr-x <strong>23<\/strong> vidar vidar  4096 2010-02-02 18:22 linux-2.6.32.7\r\n...\r\n<\/pre>\n<p>For files, this is the number of hard links. You can use <code>find \/ -samefile filename<\/code> to find all files that point to the same file inode. <\/p>\n<p>So what does this number mean for directories? Exactly the same thing. <\/p>\n<p>Users, including root, are blocked from creating directory hard links out of the kernel&#8217;s mortal fear of cyclical directory trees (or should I say directory graphs?). The kernel still creates them though, specifically in the form of the &#8220;.&#8221; entry in the directory itself, and &#8220;..&#8221; in each subdirectory. <\/p>\n<p>An empty directory <code>\/foo\/bar<\/code> will have two links, <code>\/foo\/bar<\/code> itself, and <code>\/foo\/bar\/.<\/code>. When creating a subdirectory <code>\/foo\/bar\/baz<\/code>, you will get the additional hard link <code>\/foo\/bar\/baz\/..<\/code>. In other words, the hard link count is the number of subdirectories plus two. <\/p>\n<p>Here&#8217;s a party trick for listing directory hard links in bash:<\/p>\n<pre>\r\nvidar@kelvin ~\/src $ ls -ld aml-lsb\/{,.,*\/..}\r\ndrwxr-xr-x 4 vidar vidar 4096 2009-11-22 12:52 aml-lsb\/\r\ndrwxr-xr-x 4 vidar vidar 4096 2009-11-22 12:52 aml-lsb\/.\r\ndrwxr-xr-x 4 vidar vidar 4096 2009-11-22 12:52 aml-lsb\/bin\/..\r\ndrwxr-xr-x 4 vidar vidar 4096 2009-11-22 12:52 aml-lsb\/lib\/..\r\nvidar@kelvin ~\/src $ \r\n<\/pre>\n<p>Clearly, each of them refers to the same thing, and the numbers add up (if they don&#8217;t, <code>shopt -s dotglob<\/code>)<\/p>\n<p>As a side note, you can use <code>mount --rbind<\/code> to fake a directory hard link. This will remount a directory and all submounts on some other directory, but will prevent cycles. <\/p>\n<p>You can also use <code>mount --bind<\/code> to remount without submounts. This can be useful for when you want to copy the contents of a directory that has another file system mounted over it. This is most commonly \/dev, which is over-mounted with udev early in the boot process. Many people don&#8217;t realize that they have an entire \/dev they&#8217;ve never seen!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever considered the hard link count from ls on directories? vidar@kelvin ~\/src $ ls -l total 108 drwxr-xr-x 4 vidar vidar 4096 2009-11-22 12:52 aml-lsb drwxr-xr-x 13 vidar vidar 4096 2009-12-13 16:00 delta3d_REL-2.4.0 drwxr-xr-x 23 vidar vidar 4096 2010-02-02 18:22 linux-2.6.32.7 &#8230; For files, this is the number of hard links. You can use find &hellip; <a href=\"https:\/\/www.vidarholen.net\/contents\/blog\/?p=18\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;What&#8217;s up with directory hard link counts?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6,4],"tags":[24,53],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-basic-linux","category-linux","tag-fs","tag-linux"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":0,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}