How to find nested directories?

I have a directory tree:

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

Depths are different. Can it find all the paths on which there is a directory with an x.txt file with a length> 0? May need to use a bash script? Thank.

+5
source share
4 answers

I believe GNU find can meet all your criteria:

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

The above recursively searches /top/dirfor non-empty ( -not -empty), regular ( -type f) files with a name x.txtand prints directories leading to these files ( -printf '%h\n').

+8
source

Perhaps with find you can use:

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

, ...

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
+1

All Articles