Bash – How to show distinct directories on find

bashfind

I want to find all directories containing, for example, files named *.txt. but the output must not contain duplicates. How to do it?

Best Answer

Try:

find . -type f -name "*.txt" -printf '%h\n' | sort | uniq

This works as follows:

find . -type f -name "*.txt" -printf '%h\n' - find all files that end in *.txt and print it's directory (%h) followed by a newline.

| sort - sort the directories

| uniq - remove duplicates