Help identifying empty directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help identifying empty directories
# 1  
Old 11-09-2005
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

[ $# -lt 1 ] && set -- .

find "$@" -type d -depth -print > dir.txt |
while read dir
do
[ `ls "$dir" | wc -l` -lt 1 ] || continue
echo >&2 "$0: found empty directory: $dir"
# rmdir "$dir" || exit $?
done
exit 0
# 2  
Old 11-09-2005
you might find this thread useful and interesting to read.
# 3  
Old 11-09-2005
Whoa! Here's some syntax that I've never seen before...

command1 > somefile | command2

What is that supposed to do? I tested it and I found that all of command1's output went to the file and nothing went to the pipe. But this means your while loop should run zero times. So I can't explain how you get anything. It looks to me like you should get nothing. And when I try your code, I do get nothing.

After I removed "> dir.txt", it seemed to work for me.

The -depth does no good so I would get rid of that. If a directory has subdirctories, it is not empty and does not need to be checked. You should check only "leaf directories". To do that add "-links 2" to your find statement (think about it). And you have a bug. A directory containing only ".secretfile" is not empty. Do a "ls -a" and check for 3 or more entries. ls sorts its output and a sort can take time. "ls -fa" will bypass the sort and possibly speed stuff up.

But I have no idea why you are getting everything. Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Empty Directories

Please help me. How i can find empty directories in solaris?? (4 Replies)
Discussion started by: FoDeGe
4 Replies

2. 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

3. 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

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

Identifying New and Modified Files/Directories

Hi. Our shop is migrating to a new UNIX server and our hope is to do a full migration of all files to the new server weeks in advance of the final migration. As a result we want to identify files on our SOLARIS 8 UNIX server that have changed or that were created after a specific date & time... (2 Replies)
Discussion started by: buechler66
2 Replies

7. 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

8. 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

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