user710818
user710818

Reputation: 24248

How to find nested directories?

I have directory tree like:

dir11/dir21/dir31......./dirn1
dir12/dir22/dir32......./dirn2
dir13/dir23/dir33......./dirn3

Depths are different. Does it possible find all paths on which exists directory with file x.txt that has length>0 ? May be need use bash script? Thanks.

Upvotes: 5

Views: 4786

Answers (4)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

I believe GNU find can match all your criteria by itself:

$ find /top/dir -not -empty -type f -name x.txt -printf '%h\n'

The above recursively searches /top/dir for non-empty (-not -empty), regular (-type f) files named x.txt, and prints the directories leading to these files (-printf '%h\n').

Upvotes: 9

user810430
user810430

Reputation: 11499

find . -type f -name *x.txt -size +1

Upvotes: 3

Benoit
Benoit

Reputation: 79185

Probably with find you can use:

find /top/dir -type f -name x.txt -size +1b -printf '%h\n'

Upvotes: 3

fge
fge

Reputation: 121710

You pretty much need that, yes...

for dir in $(find /the/root/dir -type d); do
    if [ ! -f "$dir/x.txt" ]; then
        continue
    fi
    size=$(stat -c %s "$dir/x.txt")
    if [ "$size" != "0" ]; then
       echo $dir
    fi
done

Upvotes: 1

Related Questions