views:

198

answers:

7

i need to find empty directories for given list of directories some directories have directories inside it

if inside directories also empty

i can say main directory is empty otherwise it's not empty

how can i test this

for example

A>A1(file1),A2 this is not empty beacuse of file1
B>B1(no file) this is empty
C>C1,C2 this is empty

thanks

+5  A: 

Check whether find <dir> -type f outputs anything, here's an example:

for dir in A B C; do
    [ -z "`find $dir -type f`" ] && echo "$dir is empty"
done
Marcelo Cantos
No, it will not output subdirectories. That's what the `-type f` is for.
Marcelo Cantos
Howerver if there are *empty files*, your solution won't yield the message.
Yasir Arsanukaev
Yasir: I would think that a directory containing an empty file wound not be empty itself. Would that not be the correct result?
Ukko
A: 

Hi. Use code below

find . -type d -empty
mosg
-empty doesnt work for me
soField
-empty only works if the current directory is completely empty, not if the subtree contains no files.
Marcelo Cantos
mosg
hp-ux so there is no empty parameter
soField
@soField So, you see, that it's better (for all of us) to post this data at the top of your question...
mosg
A: 

I created a simple structure as follows:

test/
test/test2/
test/test2/test2.2/
test/test3/
test/test3/file

The test/test3/file contains some junk text.

Issuing find test -empty returns "test/test2/test2.2" as the only empty directory.

jsumners
the op asks for test/test2 to be returned also
hop
Indeed. But I figured he could read the find man page to go further. A script that adjusts depths based on results would work just fine.
jsumners
A: 

a simple approach would be,

$ [ "$(ls -A /path/to/direcory)" ] && echo "not empty" || echo "its empty"

also,

if [ "$(ls -A /path/to/direcory)" ]; then
   echo "its not empty"
else 
   echo "empty directory"
phoenix24
A: 

It depends a little on what you want to do with the empty directories. I use the command below when I wish to delete all empty directories within a tree.

find test -depth -empty -delete

One thing to notice about the command above is that it will also remove empty files, use the -type d option avoid that.

If your definition of an empty directory tree is that it does't contain any files then you be able to stick something together based on whether find test -type f return anything.

Find is a great utility, but you need to read the man page to really understand how much it can do :-)

Martin Skøtt
+2  A: 

This recursive function would seem to do the trick:

# Bash
findempty() {
    find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
    do
        if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
        then
            findempty "$dir"
            echo "$dir"
        fi
    done
}

Given this example directory structure:

    .
    |-- dir1/
    |-- dir2/
    |   `-- dirB/
    |-- dir3/
    |   `-- dirC/
    |       `-- file5
    |-- dir4/
    |   |-- dirD/
    |   `-- file4
    `-- dir5/
        `-- dirE/
            `-- dir_V/

The result of running that function would be:

    ./dir1
    ./dir5/dirE/dir_V
    ./dir5/dirE
    ./dir5
    ./dir2/dirB
    ./dir2

which misses /dir4/dirD. If you move the recursive call findempty "$dir" after the fi, the function will include that directory in its results.

Dennis Williamson
A: 
find . -name -type d -ls |awk '($2==0){print $11}'
Vijay Sarathi