-   Linux - Newbie (
-   -   ls long listing format - number of hard links / directories (

sljunkie 09-15-2011 04:11 PM

ls long listing format - number of hard links / directories

I have a question regarding the 'ls -l' command - what does the 'number of linked hard-links' mean when the file is a directory?

anomie 09-15-2011 04:36 PM

Even for directories, it should still refer to hard links.



$ mkdir foo

$ ls -ld foo
drwxr-xr-x 2 me me 4096 Sep 15 15:32 foo

Right now there are two hard links:
  • foo
  • foo/.

The latter is the directory's reference to itself.

Let's create some stuff inside it:


$ touch foo/bar foo/baz

$ mkdir foo/boo

$ ls -ld foo
drwxr-xr-x 3 me me 4096 Sep 15 15:34 foo

Now there are three hard links for the directory. Why? Because now we have:
  • foo
  • foo/.
  • foo/boo/..

The last entry is the parent of boo (which is foo). If we create more subdirectories inside foo, the hard links will increase accordingly.

MensaWater 09-15-2011 04:47 PM

Hard links are refrences to the same inode (as opposed to file) on a given filesystem. Hard links do not take up any more space because all the space is in the original file.

Soft links (a/k/a symbolic links and slinks) are references to a file and do not have to be on the same filesystem as the original file. Each slink takes up a tiny amount of space.

The most common default hard links to a directory are the "." and ".." seen in subdirectory paths below it. The "." in a directory is shorthand for "this directory" and the ".." is shorthand for "the parent directory of this directory".

Of course you can use the ln (link) command to create additional hard links or soft links to any given file. (Note that a directory is a "file" in UNIX/Linux.)

sljunkie 09-15-2011 05:43 PM

Thanks folks, I'm currently teaching informatics for the little ones (10/11 years old) and I tough them how to make hard and soft links, and the long listing format, and now I can explain to them this topic better.

All times are GMT -5. The time now is 03:19 AM.