results.out ファイルを含む複数レベルのサブディレクトリが複数あります。
./dir1/results.out
./dir2/dir21/results.out
./dir3/dir31/dir311/results.out
これらのサブディレクトリを別の場所に移動する必要があるため、含まれているstring1
ディレクトリパスを検索して抽出する必要があります。results.out
たとえば、次のコードを使用してファイルパスを取得できます。results.out
string1
for i in $(find . -type f -name "results.out);
do
grep -l "string1" $i
done
ディレクトリパスのみを取得するには、上記のコードをどのように変更する必要がありますか?
ベストアンサー1
GNUがある場合は、フォーマット指定子を使用してfind
パスを印刷できます。%h
%h Leading directories of file's name (all but the last ele‐
ment). If the file name contains no slashes (since it is
in the current directory) the %h specifier expands to
".".
たとえば、あなたはできます
find . -name 'results.out' -exec grep -q 'string1' {} \; -printf '%h\n'