What does the second column in the output of 'ls -n' mean?

The second column is the number of hard links to the file. For a directory, the number of hard links is the number of immediate subdirectories it has plus its parent directory and itself.

$ ls -n
total 0
$ touch f1
$ touch f2
$ ln f1 hardlink
$ ln -s f2 softlink
$ mkdir d1
$ mkdir d2
$ mkdir d2/a d2/b d2/c
$ ls -n
total 8
drwxr-xr-x 2 1000 1000 4096 2010-12-31 00:07 d1
drwxr-xr-x 5 1000 1000 4096 2010-12-31 00:07 d2
-rw-r--r-- 2 1000 1000    0 2010-12-31 00:06 f1
-rw-r--r-- 1 1000 1000    0 2010-12-31 00:06 f2
-rw-r--r-- 2 1000 1000    0 2010-12-31 00:06 hardlink
lrwxrwxrwx 1 1000 1000    2 2010-12-31 00:07 softlink -> f2

Linux Gazette Issue 35

Linux Gazette Issue 93


karthick@Ubuntu-desktop:~$ ls -n
drwxr-xr-x  2 1000 1000  4096 2010-12-02 15:56 Books

First Column: drwxr-xr-x

Second Column: 2

  • It shows the hard link count to that file/directory.

Third nd Fourth Column: 1000 1000

  • It shows UID and GID of the user.

Fifth column: 4096

  • It shows the size.

Sixth column: 2010-12-02 15:56

  • It shows last modified date and time.

Seventh column: Books

  • It shows name of the file/directory.

NOTE:

For more information look at this link.


The answers given regarding directories will give the right number, but for the wrong reasons. The number is not a count of the subdirectories plus "." and ".."

The number is actually the same as for a file: the number of hard links to the directory. For example, let us create a new directory:

someuser@mymachine:~/test$ mkdir temp
someuser@mymachine:~/test$ ls -al
total 24
drwxrwxr-x  3 someuser someuser  4096 2012-02-27 15:58 .
drwx------ 50 someuser someuser 16384 2012-02-27 15:52 ..
drwxrwxr-x  2 someuser someuser  4096 2012-02-27 15:58 temp
someuser@mymachine:~/test$

You can see that the number of links is 2. These links are the name "temp" and the "." directory within temp (aka "temp/.") Not the ".." directory. That is a link to the parent of "temp". Which kind of explains why creating a sub-directory creates a new link. Let's do it:

someuser@mymachine:~/test$ cd temp
someuser@mymachine:~/test/temp$ mkdir subtemp
someuser@mymachine:~/test/temp$ ls -al
total 12
drwxrwxr-x 3 someuser someuser 4096 2012-02-27 16:03 .
drwxrwxr-x 3 someuser someuser 4096 2012-02-27 15:58 ..
drwxrwxr-x 2 someuser someuser 4096 2012-02-27 16:03 subtemp
someuser@mymachine:~/test/temp$

There are now 3 links to the directory. They are "temp", "temp/." and "subtemp/.." (that is, the ".." directory within "subtemp"). So that's why subdirectories add a link - because they all have a ".." directory referring to the parent.