38

When I run:

ls -n

I get something like:

-rwxrwxrwx  1 1000 1000   765528 2009-10-15 18:41 file1
drwxr-xr-x 10 1000 1000     4096 2010-12-07 20:50 dir1
drwxr-xr-x  3 1000 1000     4096 2010-10-24 16:57 dir2

What does the second column (the number) mean?

5 Answers 5

52

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

2
  • 1
    +1. this answer properly addresses the OP; excellent examples. thx Bribles =)
    – violet313
    Mar 13, 2014 at 23:28
  • 2
    The 2 Linux Gazette links does not exist anymore! Jun 9, 2014 at 10:28
21
karthick@Ubuntu-desktop:~$ ls -n
drwxr-xr-x  2 1000 1000  4096 2010-12-02 15:56 Books

First Column: drwxr-xr-x

  • It shows file permission.

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.

1
  • That makes sense. I was wondering why it was different for directories. Dec 31, 2010 at 6:00
7

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.

1

This field specifies the number of links or directories inside this directory.

For example:

aneesh@aneesh-VirtualBox:~$ ls -n 
total 146500
drwxr-xr-x  6 1000 1000      4096 2010-12-30 11:27 Desktop
drwxr-xr-x 10 1000 1000      4096 2010-12-30 16:20 Documents
drwxr-xr-x  7 1000 1000      4096 2010-12-30 17:46 Downloads
drwxrwsr-x  9 1000 1000      4096 2010-12-28 17:18 eclipse
-rw-r--r--  1 1000 1000 149897197 2010-10-04 15:18 eclipse-php-helios-linux-gtk.tar.gz
-rw-r--r--  1 1000 1000       179 2010-11-26 08:50 examples.desktop
drwxr-xr-x  2 1000 1000      4096 2010-11-26 09:20 Music
drwxr-xr-x  6 1000 1000      4096 2010-12-23 14:59 MyWork
drwxr-xr-x  8 1000 1000      4096 2010-12-23 12:18 new
-rw-r--r--  1 1000 1000     37985 2010-12-18 12:09 output.pdf
drwxr-xr-x  7 1000 1000      4096 2010-12-21 19:35 Pictures
drwxr-xr-x  2 1000 1000      4096 2010-11-26 09:20 Public
drwxrwxrwx  2 1000 1000      4096 2010-12-29 12:47 public_html
-rw-r--r--  1 1000 1000       942 2010-12-30 19:55 rn
drwxr-xr-x 19 1000 1000      4096 2010-07-29 16:37 simplesamlphp
drwxr-xr-x  6 1000 1000      4096 2010-12-14 11:48 SoftMaker
drwxr-xr-x  2 1000 1000      4096 2010-12-28 14:52 Templates
drwxrwxr-x  8 1000 1000      4096 2010-12-03 14:48 Ubuntu One
drwxr-xr-x  4 1000 1000      4096 2010-12-28 10:37 Videos
drwxr-xr-x  3 1000 1000      4096 2010-12-20 19:26 Work

You can see the number of links or directories in the Desktop is 6. Let us check it.

aneesh@aneesh-VirtualBox:~$ ls -la Desktop/
total 72
drwxr-xr-x  6 aneesh aneesh 4096 2010-12-30 11:27 .
drwxr-xr-x 46 aneesh aneesh 4096 2010-12-31 10:22 ..
-rw-r--r--  1 aneesh aneesh   48 2010-11-30 11:50 bugs
drwxr-xr-x  2 aneesh aneesh 4096 2010-12-30 14:49 Bugs
-rw-r--r--  1 aneesh aneesh  793 2010-12-07 12:34 Hello_world.xml
-rw-r--r--  1 aneesh aneesh  440 2010-12-09 20:08 new
drwxr-xr-x  2 aneesh aneesh 4096 2010-12-14 16:00 plugin
drwxr-xr-x  2 aneesh aneesh 4096 2010-12-17 17:32 plugins
drwxr-xr-x  2 aneesh aneesh 4096 2010-12-21 12:35 saml

Here you can see the links and directories inside the desktop are :

  1. '.' # the parent directory

  2. '..' # current directory or present working directory.

  3. 'Bugs'

  4. 'plugin'

  5. 'plugins'

  6. 'saml'

    so total 6. Hope this helps.

check this link for more info. http://www.go2linux.org/command-ls-file-permissions

0

This gives the number of links/directories immediately inside this file/directory. For files this number should be 1 and for directories it should be number of directories+2 i.e for . and .. directories

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .