Empty Directories

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Empty Directories
# 1  
Old 11-28-2016
Empty Directories

Please help me. How i can find empty directories in solaris??
# 2  
Old 11-28-2016
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:
Code:
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:
Code:
find /MountPoint -type d -xdev -links 2


Last edited by Don Cragun; 11-29-2016 at 09:15 PM.. Reason: Simplify 1st suggestion.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-30-2016
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
Code:
find startdir -type d -empty

With the native Solaris find it becomes more complicated. For example
Code:
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...
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 11-30-2016
Quote:
Originally Posted by MadeInGermany
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
Code:
find startdir -type d -empty

With the native Solaris find it becomes more complicated. For example
Code:
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...
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 .:
Code:
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).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-30-2016
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).
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Delete empty directories recursively - HP-UX

Hi, I want to delete all empty directories in a long directore tree structure. I want to use that from a script that will run on HP-UX 11. My definition of empty directory is that there is no regular file under it and directly beneath it. To elaborate, I have below directories. /app/dev/java... (14 Replies)
Discussion started by: asutoshch
14 Replies

2. Shell Programming and Scripting

How to exclude the empty directories

Hi., I have a script, in which I am processing a files present in the directory types. ls -lrt | grep ^d | grep Dir_type | awk -f '{print $9}' |\ while read dir_name; do #operations done where Dir_type is the pattern in which directories get created. How to filter out empty... (2 Replies)
Discussion started by: IND123
2 Replies

3. Shell Programming and Scripting

Deleting empty directories using find

Hello, I'm submitting this thread, because I was looking a way to delete empty directories using find and I found a thread from 2007 that helped me. I have worked from that threat, but I found that the command sent would analyze original directory and may delete it to. I have come up with expanded... (3 Replies)
Discussion started by: lramirev
3 Replies

4. Shell Programming and Scripting

Listing non empty directories

Hi Gurus, How to list directories that are non-empty and non-hidden Thanks in advance (2 Replies)
Discussion started by: kinny
2 Replies

5. UNIX for Dummies Questions & Answers

Remove only Empty Directories

I know this one was answered before in forum below - https://www.unix.com/unix-dummies-questions-answers/58210-removing-empty-folders-using-find-command.html But that one is closed & I have a question so here it goes. I want to delete all 2006 files. Now if along with the files, if the... (2 Replies)
Discussion started by: kedar.mehta
2 Replies

6. Shell Programming and Scripting

identify the empty directories

Hi Wrote the below script to identify the empty directories ,when executing the below showing that directory is not empty but the directories are empty.Please help to identify the empty directories 33 is not empty 33 is not empty 33 is not empty 33 is not empty for file in `find .... (5 Replies)
Discussion started by: mohan705
5 Replies

7. UNIX for Dummies Questions & Answers

Empty directories having different size

$ls -lrt mydir total 12 drwxrwxrwx 2 nobody nobody 512 Aug 8 11:51 tmp drwxrwxrwx 2 nobody nobody 4608 Jan 19 12:20 web.cache $ ls -lrt mydir/web.cache/ total 0 $ ls -lrt mydir/tmp/ total 0 Can anyone explain me the above results? I know the o/p of ls, but this... (3 Replies)
Discussion started by: rahulrathod
3 Replies

8. UNIX for Dummies Questions & Answers

Help identifying empty directories

Is there a way you can identify directories that are empty? I do not need to remove them, I just need to identify them below a cetain path. I have tried the following already and it returned everything for some reason. #!/bin/sh && set -- . find "$@" -type d -depth -print > dir.txt |... (2 Replies)
Discussion started by: dboard
2 Replies

9. Shell Programming and Scripting

searching for list of empty directories

Guys I need to write a korn shell script that will search for a list of empty sub-directories in a specific directory and then email a list of these empty directories. Any ideas - apologies, I am new to shell scripting. Thanks (4 Replies)
Discussion started by: man80
4 Replies

10. UNIX for Dummies Questions & Answers

scanning empty directories

Hi, I want to produce a text file representing a list of empty directories on a unix system starting from a specified directory. I hope I explained well my problem. Thanks in advance. (7 Replies)
Discussion started by: N065956BM
7 Replies
Login or Register to Ask a Question