如何找到嵌套目录?

use*_*818 5 linux bash file path

我有目录树像:

dir11/dir21/dir31......./dirn1
dir12/dir22/dir32......./dirn2
dir13/dir23/dir33......./dirn3
Run Code Online (Sandbox Code Playgroud)

深度不同.是否可以找到文件x.txt中存在长度> 0的存在目录的所有路径?可能需要使用bash脚本吗?谢谢.

Fré*_*idi 9

我相信GNU find可以自己匹配所有标准:

$ find /top/dir -not -empty -type f -name x.txt -printf '%h\n'
Run Code Online (Sandbox Code Playgroud)

上面以递归方式搜索名为的/top/dir非empty(-not -empty),regular(-type f)文件x.txt,并打印通向这些文件的目录(-printf '%h\n').


fge*_*fge 1

你非常需要这个,是的......

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
Run Code Online (Sandbox Code Playgroud)