find
を使用して、1つのディレクトリを除いて、パターンに一致するすべてのファイルとディレクトリを見つけるにはどうすればよいですか。 ?
次のファイル構造があるとします。
. foo-exclude-me/ foo.txt foo-exclude-me-not/ foo.txt bar/ foo.txt foobar/ bar.txt foofoo.txt
find
を使用して次の出力を取得するにはどうすればよいですか :
./bar/foo.txt ./bar/foobar ./bar/foobar/foofoo.txt ./foo-exclude-me-not ./foo-exclude-me-not/foo.txt
次の両方のコマンドを使用してみました:
find . -name 'foo-exclude-me' -prune -o -name 'foo*' find . -name 'foo*' ! -path './foo-exclude-me/*'
しかし、両方ともこれを返します:
./bar/foo.txt
./bar/foobar
./bar/foobar/foofoo.txt
./foo-exclude-me # << this should be excluded
./foo-exclude-me-not
./foo-exclude-me-not/foo.txt
foo-exclude-me
を適切に除外するにはどうすればよいですか ディレクトリ?
承認された回答:
find . -name 'foo-exclude-me' -prune -o -name 'foo*' -print
-print
なし 、暗黙のデフォルトアクションは、プルーニングされたものも含め、すべての一致に適用されます。明示的な-print
-name 'foo*'
である指定された条件下でのみ適用されます -name 'foo-exclude-me'
のelseブランチでのみ 。
一般的に、明示的な-print
を使用します 述語の接続詞よりも複雑なことをしているときはいつでも。
! -path './foo-exclude-me/*'
./foo-exclude-me
のため、機能しませんでした ./foo-exclude-me/*
と一致しません (末尾の/
はありません )。 ! -path ./foo-exclude-me
うまくいくでしょう。