Empty Directories

Please help me. How i can find empty directories in solaris??

To get an ls -l format list of empty directories in the file system mounted on the mount point /MountPoint on a Solaris system, try:

find /MountPoint -type d -xdev -links 2 -exec ls -ld {} +

---
Update... And, if you just want the names of the empty directories, you could also try:

find /MountPoint -type d -xdev -links 2
1 Like

Don, your suggestion is not correct.
In Unix the link count of a directory is increased if sub directories are added, but not if files are added.
The plausible reason is that a directory is linked to . and .. plus every sub directory's ..
So, an empty directory has always got a link count 2, but not vice versa.
--
For the request to find/list empty directories the straight answer is GNU find

find startdir -type d -empty

With the native Solaris find it becomes more complicated. For example

find startdir -type d -links 2 -exec /bin/sh -c 'test -n "`ls -A "$1" 2>/dev/null`" || echo "$1"' sh {} \;

The -links 2 condition is useful - it reduces the calls to /bin/sh and ls. Still much overhead. A solution in perl with it's built-in lstat() would be more efficient...

1 Like

Not on the systems I have used and not according to the standards. The link count on a directory is the number of links in that directory (one for every file no matter what type of file it is); for a regular file it is the number of hard links to that file; and for other file types it can vary. Depending on filesystem type, a directory need not include links for dot and dot-dot, but for the VAST majority of filesystem types that are usually mounted on Solaris systems, those links are included and the link count on an empty directory will be 2. In theory, we should determine the filesystem type for every directory mounted, create an empty directory and determine if the link count on that directory is 0 or 2 and use that number when doing the find on that filesystem's mount point; in practice, on any system I've used, 2 works. I'm on a macOS 10.12.1 system now. Here is the output from ls -lRa . :

total 0
drwxr-xr-x  9 dwc  staff  306 Nov 30 01:24 .
drwxr-xr-x  9 dwc  staff  306 Nov 27 11:28 ..
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV49.bam
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV49.vcf
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV50.bam
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV50.vcf
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV51.bam
-rw-r--r--  1 dwc  staff    0 Nov 27 11:28 MEV51.vcf
drwxr-xr-x  3 dwc  staff  102 Nov 30 01:24 a

./a:
total 0
drwxr-xr-x  3 dwc  staff  102 Nov 30 01:24 .
drwxr-xr-x  9 dwc  staff  306 Nov 30 01:24 ..
drwxr-xr-x  6 dwc  staff  204 Nov 30 01:24 b

./a/b:
total 0
drwxr-xr-x  6 dwc  staff  204 Nov 30 01:24 .
drwxr-xr-x  3 dwc  staff  102 Nov 30 01:24 ..
drwxr-xr-x  2 dwc  staff   68 Nov 30 01:24 c
-rw-r--r--  1 dwc  staff    0 Nov 30 01:24 f1
-rw-r--r--  1 dwc  staff    0 Nov 30 01:24 f2
-rw-r--r--  1 dwc  staff    0 Nov 30 01:24 f3

./a/b/c:
total 0
drwxr-xr-x  2 dwc  staff   68 Nov 30 01:24 .
drwxr-xr-x  6 dwc  staff  204 Nov 30 01:24 ..

Note that the link count on a is 3 (dot, dot-dot, and b are those three links. The link count on a/b is 6 (dot, dot-dot, c , and the three regular files f1 , f2 , and f3 . And, the link count on a/b/c (an empty directory) is 2 (dot and dot-dot).

1 Like

Hmm, interesting. Also Solaris zfs is like this.
But Solaris ufs,vxfs, AIX jfs2, Linux ext3,ext4,xfs are using the other scheme (traditional Unix I assume).

1 Like