I'd like to find all directories containing .txt files. But I need to print one level up the output. For example, I'm using find to search recursively for .txt files in the current directory. Then only the directories containing such files are shown.find . -name *.txt -printf '%h\n'outputs to (for example)./out/data./out/data./output/dataSo, I'd like to improve the use of find to:does not show repeated dirs (I think it happens because there are 2 .txt file therein)I'd like to print only the first level, that is, only ./out/ and ./output/
Change depth for find when searching directories
find
There might be a more find-centric way, but you could do it with a couple of other tools helping:find . -name '*.txt' -printf '%h\n' | cut -f1,2 -d/ | sort -uOr I guess we could save a process and use awk likefind . -name '*.txt' -printf '%h\n' | awk -F/ '{matched[$1/$2]=1} END {for(dir in matched) {print dir}}'