Albert P
Albert P

Reputation: 41

How to get the link count of a directory that has multiple directories inside of it?

I was thinking that it would be the sum of the link counts within the directories?

Upvotes: 3

Views: 3802

Answers (4)

wildplasser
wildplasser

Reputation: 44250

The link count is not about directories, it is about inodes. The link count is essentially a reference count.

  • an inode is the basic object inside a filesytem
  • directory entries link to inodes
  • a directory has an inode, too, just like a plain file
  • every link counts: a directory entry that links to an inode adds 1 to the count, so inodes that are referenced by two directory-entries have a link count of two.
  • a link to the parent directory is also a link for this directory (this is a safeguard to protect against orphan directories)
  • when the link count for an inode goes to zero it is unreferenced, and the inode (and associated diskblocks, or whatever) can be discarded as well
  • ["open" files invisibly have a similar mechanism]
  • symlinks don't count: when a symlink to a file is added/removed, its link count will not be affected. (thus: you can symlink to a non-existent file)

Upvotes: 5

cnst
cnst

Reputation: 27228

Do you just want to sum up the link count of all the subdirectories?

What's exactly the purpose, may I ask?

You could do something similar to Unix find average file size:

find ./ -type d -ls | awk '{sum += $4;} END {print sum;}'

You could even find the average link count, whilst at it:

find ./ -type d -ls | awk '{sum += $4; n++;} END {print sum/n;}'

Upvotes: 0

Anatoly
Anatoly

Reputation: 15530

Linux find utility has an option -type d to filter certain type (directory, file, symbolic link, socket or other) so it should help alongside with wc -l to count number of STDOUT lines:

find ./ -type d | wc -l

If you need to limit number of nested directories use -maxdepth parameter:

find ./ -type d -maxdepth 2 | wc -l

Upvotes: 0

Nitin Tripathi
Nitin Tripathi

Reputation: 1254

you can use tree utility to see the count of directories. enter image description here

Upvotes: 0

Related Questions