Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As per the question, I am thinking mkdir ~/a is either two or three:

  • 1 entry for the dir it sits in (~/a)
  • 1 entry for itself (cd a && ls .)
  • and/or 1 entry for itself again (cd a && ls ..)

Could someone clarify if this is two or three?

share|improve this question
    
You mind looking at the question again, and clarify; the 2nd and 3rd commands are exactly again. –  Tshepang Feb 22 '11 at 7:47
    
It is three - think of mkdir /test - /test links to itself (.) and its parent (..), and then / now links to /test. Indeed, /test has two entries inside of it, but three entries total are created. –  AlexWebr Aug 10 '12 at 20:32

2 Answers 2

In an empty directory:

% stat .
  File: `.'
  Size: 6               Blocks: 0          IO Block: 4096   directory
Device: fe04h/65028d    Inode: 537317737   Links: 2
Access: (0750/drwxr-x---)  Uid: ( 1000/stribika)   Gid: (  100/   users)
Access: 2011-02-22 08:47:20.935036074 +0100
Modify: 2011-02-22 08:47:20.935036074 +0100
Change: 2011-02-22 08:47:20.935036074 +0100
 Birth: -
% mkdir foo
% stat .
  File: `.'
  Size: 16              Blocks: 0          IO Block: 4096   directory
Device: fe04h/65028d    Inode: 537317737   Links: 3
Access: (0750/drwxr-x---)  Uid: ( 1000/stribika)   Gid: (  100/   users)
Access: 2011-02-22 08:47:20.935036074 +0100
Modify: 2011-02-22 08:47:57.465036072 +0100
Change: 2011-02-22 08:47:57.465036072 +0100
 Birth: -
% stat foo
  File: `foo'
  Size: 6               Blocks: 0          IO Block: 4096   directory
Device: fe04h/65028d    Inode: 509269      Links: 2
Access: (0750/drwxr-x---)  Uid: ( 1000/stribika)   Gid: (  100/   users)
Access: 2011-02-22 08:47:57.465036072 +0100
Modify: 2011-02-22 08:47:57.465036072 +0100
Change: 2011-02-22 08:47:57.465036072 +0100
 Birth: -

As you can see there are 2 links to an empty directory. When I create a new one inside it the link count increases to 3. Additionaly there are 2 links to the new directory. The total is 3 new links.

This is because every directory has a link to itself (.) and its parent (..) .

share|improve this answer
    
Thanks, this cleared up what I was thinking! –  Deuterium Feb 22 '11 at 16:34
    
I like Gilles answer better - a brand-new directory has two links to it, but creating a new directory creates three new entries in the filesystem: . and .. in the new directory, and the actual link to the new directory, in its parent directory. –  AlexWebr Aug 10 '12 at 20:29

In the original Unix implementation, in order to keep the filesystem code inside the kernel simple, directory manipulation programs did some extra work: in particular, mkdir /parent/a created an entry for a in /parent, plus an entry called . in a (pointing to a itself) and an entry called .. in a (pointing to /parent¹).

Pretty soon the code for mkdir and friends moved into the kernel anyway, but the filesystem format kept having explicit . and .. entries, which filesystem traversal code found by name (as opposed to having two special-format pointers in each directory).

Nowadays, some (most?) filesystems fake it: directories don't actually have . and .. entries on the disk, they're generated by the driver. However, from a user's point of view, this is transparent. A directory's link count is still two plus the number of subdirectories (the entry in the parent, the directory's own ., and each subdirectory's ..). In particular, an empty directory has a link count of two (i.e. there are two entries in the filesystem pointing to it), but creating it creates three entries (the third one is .. which points to the parent).

¹ By reference, not by name. So if you rename /parent, a's .. keeps pointing to a's parent directory, wherever it moves to in the filesystem structure.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.