Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267
Source Link
Sigur
  • 2.5k
  • 8
  • 37
  • 45

Change depth for find when searching directories

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/data

So, I'd like to improve the use of find to:

  1. does not show repeated dirs (I think it happens because there are 2 .txt file therein)

  2. I'd like to print only the first level, that is, only ./out/ and ./output/