find . -type f -name '*f*' | sed -r 's|/[^/]+$||' |sort |uniq
The above finds all files below the current directory (.
) that are regular files (-type f
) and have f
somewhere in their name (-name '*f*'
). Next, sed
removes the file name, leaving just the directory name. Then, the list of directories is sorted (sort
) and duplicates removed (uniq
).
The sed
command consists of a single substitute. It looks for matches to the regular expression /[^/]+$
and replaces anything matching that with nothing. The dollar sign means the end of the line. [^/]+'
means one or more characters that are not slashes. Thus, /[^/]+$
means all characters from the final slash to the end of the line. In other words, this matches the file name at the end of the full path. Thus, the sed command removes the file name, leaving unchanged the name of directory that the file was in.
Simplifications
Many modern sort
commands support a -u
flag which makes uniq
unnecessary. For GNU sed:
find . -type f -name '*f*' | sed -r 's|/[^/]+$||' |sort -u
And, for MacOS sed:
find . -type f -name '*f*' | sed -E 's|/[^/]+$||' |sort -u
Also, if your find
command supports it, it is possible to have find
print the directory names directly. This avoids the need for sed
:
find . -type f -name '*f*' -printf '%h\n' | sort -u
More robust version (Requires GNU tools)
The above versions will be confused by file names that include newlines. A more robust solution is to do the sorting on NUL-terminated strings:
find . -type f -name '*f*' -printf '%h\0' | sort -zu | sed -z 's/$/\n/'
Simplified using dirname
Imagine needing the command in a script where command will be in single quotes, escaping sed command is painful and less than ideal, so replace with dirname.
Issues regard special chars and newline are also mute if you did not need to sort or directories names are not affected.
find . -type f -name "*f*" -exec dirname "{}" \; |sort -u
take care of newline issue:
find . -type f -name "*f*" -exec dirname -z "{}" \; |sort -zu |sed -z 's/$/\n/'
uniq
into the mix helps a lot by removing the repeated lines that are already right next to each other.find . -type f -name '*f*' -printf '%h\0' | uniq -z | sort -zu | tr '\0' '\n'
. Or if your tools are a little older, then uniq may not have the -z option.find . -type f -name '*f*' -printf '%h\n' | uniq | sort -u
– jbo5112 Jun 30 '17 at 18:06-E
for MacOS. – John1024 Apr 01 '18 at 05:17file names that include newlines
- bonus points for diligence and taking extra care; don't think I have ever encountered those, though - in what context would those ever occur ? you aren't talking about whitespace as in spaces or tabs, are you ? – ssc Jun 28 '20 at 08:00