ファイル構造が次の場合:
/a/p/c/d...
/c/a/c/g/f/...
/a/c/d/e/...
...
私はこれをしたい:
find -mindepth 3 -type d -name p -prune -or -name c -print
ただし、このコマンドは "p"ディレクトリを削除せず、最初の行が含まれています。私はこれが実際に衝突ではないことを知っています。しかし、最小深さを適用した状態で「p」を切り取る方法は?
答え1
FreeBSDとGNU find
(サポートされている数少ない実装の1つ-mindepth
)の場合は、3つ以上の深さのディレクトリのみ-prune
を削除します。p
FreeBSDとGNUfind
ドキュメントの両方が非常に明確に説明されています(ここにGNUドキュメントがあります):
-- Option: -mindepth levels Do not apply any tests or actions at levels less than LEVELS (a non-negative integer). `-mindepth 1' means process all files except the command line arguments.
いつでも標準的な方法で実行できます。
find . -type d -name p -prune -o -path './*/*/*' -name c -print
(-mindepth
そして-or
標準述部でもなく、find
ディレクトリを省略するわけでもありません)。
または以下を使用してzsh
:
setopt extendedglob
printf '%s\n' ^p/^p/(^p/)#c
答え2
私はこのように思う:
find -mindepth 3 -type d ! -path '*/p/*' -name c -print
@StephaneChazelasのフィードバックに基づいて、私はこのアプローチが/p/
すべてのディレクトリの無関係な検索を削除すると信じています。
find -mindepth 3 -type d -path '*/p/*' -prune -o -name c -print
分析結果が見つかりました
検索コマンドを比較するには、デバッグスイッチを追加して、特定の-D search
検索が他の検索に対してどのように実行されるかを確認できます。
@StephaneChazelasのコマンドと私のコマンドを実行して、違いが何であるかを確認しました。これら2つのコマンドを実行すると、出力は次のようになりますsdiff
。
$ sdiff \
<(find -D search -mindepth 3 -type d -path '*/p/*' -prune -o -name c -print 2>&1) \
<(find -D search -type d -name p -prune -o -path './*/*/*' -name c -print 2>&1)
consider_visiting (early): `.': fts_info=FTS_D , fts_level= 0 consider_visiting (early): `.': fts_info=FTS_D , fts_level= 0
consider_visiting (late): `.': fts_info=FTS_D , isdir=1 ignor | consider_visiting (late): `.': fts_info=FTS_D , isdir=1 ignor
consider_visiting (early): `./a': fts_info=FTS_D , fts_level= consider_visiting (early): `./a': fts_info=FTS_D , fts_level=
consider_visiting (late): `./a': fts_info=FTS_D , isdir=1 ign | consider_visiting (late): `./a': fts_info=FTS_D , isdir=1 ign
consider_visiting (early): `./a/c': fts_info=FTS_D , fts_leve consider_visiting (early): `./a/c': fts_info=FTS_D , fts_leve
consider_visiting (late): `./a/c': fts_info=FTS_D , isdir=1 i | consider_visiting (late): `./a/c': fts_info=FTS_D , isdir=1 i
consider_visiting (early): `./a/c/d': fts_info=FTS_D , fts_le consider_visiting (early): `./a/c/d': fts_info=FTS_D , fts_le
consider_visiting (late): `./a/c/d': fts_info=FTS_D , isdir=1 consider_visiting (late): `./a/c/d': fts_info=FTS_D , isdir=1
consider_visiting (early): `./a/c/d/e': fts_info=FTS_D , fts_ consider_visiting (early): `./a/c/d/e': fts_info=FTS_D , fts_
consider_visiting (late): `./a/c/d/e': fts_info=FTS_D , isdir consider_visiting (late): `./a/c/d/e': fts_info=FTS_D , isdir
consider_visiting (early): `./a/c/d/e': fts_info=FTS_DP, fts_ consider_visiting (early): `./a/c/d/e': fts_info=FTS_DP, fts_
consider_visiting (late): `./a/c/d/e': fts_info=FTS_DP, isdir consider_visiting (late): `./a/c/d/e': fts_info=FTS_DP, isdir
consider_visiting (early): `./a/c/d': fts_info=FTS_DP, fts_le consider_visiting (early): `./a/c/d': fts_info=FTS_DP, fts_le
consider_visiting (late): `./a/c/d': fts_info=FTS_DP, isdir=1 consider_visiting (late): `./a/c/d': fts_info=FTS_DP, isdir=1
consider_visiting (early): `./a/c': fts_info=FTS_DP, fts_leve consider_visiting (early): `./a/c': fts_info=FTS_DP, fts_leve
consider_visiting (late): `./a/c': fts_info=FTS_DP, isdir=1 i consider_visiting (late): `./a/c': fts_info=FTS_DP, isdir=1 i
consider_visiting (early): `./a/p': fts_info=FTS_D , fts_leve consider_visiting (early): `./a/p': fts_info=FTS_D , fts_leve
consider_visiting (late): `./a/p': fts_info=FTS_D , isdir=1 i | consider_visiting (late): `./a/p': fts_info=FTS_D , isdir=1 i
consider_visiting (early): `./a/p/c': fts_info=FTS_D , fts_le | consider_visiting (early): `./a/p': fts_info=FTS_DP, fts_leve
consider_visiting (late): `./a/p/c': fts_info=FTS_D , isdir=1 <
consider_visiting (early): `./a/p/c': fts_info=FTS_DP, fts_le <
consider_visiting (late): `./a/p/c': fts_info=FTS_DP, isdir=1 <
consider_visiting (early): `./a/p': fts_info=FTS_DP, fts_leve <
consider_visiting (late): `./a/p': fts_info=FTS_DP, isdir=1 i consider_visiting (late): `./a/p': fts_info=FTS_DP, isdir=1 i
consider_visiting (early): `./a': fts_info=FTS_DP, fts_level= consider_visiting (early): `./a': fts_info=FTS_DP, fts_level=
consider_visiting (late): `./a': fts_info=FTS_DP, isdir=1 ign consider_visiting (late): `./a': fts_info=FTS_DP, isdir=1 ign
consider_visiting (early): `./c': fts_info=FTS_D , fts_level= consider_visiting (early): `./c': fts_info=FTS_D , fts_level=
consider_visiting (late): `./c': fts_info=FTS_D , isdir=1 ign | consider_visiting (late): `./c': fts_info=FTS_D , isdir=1 ign
consider_visiting (early): `./c/a': fts_info=FTS_D , fts_leve consider_visiting (early): `./c/a': fts_info=FTS_D , fts_leve
consider_visiting (late): `./c/a': fts_info=FTS_D , isdir=1 i | consider_visiting (late): `./c/a': fts_info=FTS_D , isdir=1 i
consider_visiting (early): `./c/a/c': fts_info=FTS_D , fts_le consider_visiting (early): `./c/a/c': fts_info=FTS_D , fts_le
consider_visiting (late): `./c/a/c': fts_info=FTS_D , isdir=1 consider_visiting (late): `./c/a/c': fts_info=FTS_D , isdir=1
consider_visiting (early): `./c/a/c/g': fts_info=FTS_D , fts_ consider_visiting (early): `./c/a/c/g': fts_info=FTS_D , fts_
consider_visiting (late): `./c/a/c/g': fts_info=FTS_D , isdir consider_visiting (late): `./c/a/c/g': fts_info=FTS_D , isdir
consider_visiting (early): `./c/a/c/g/f': fts_info=FTS_D , ft consider_visiting (early): `./c/a/c/g/f': fts_info=FTS_D , ft
consider_visiting (late): `./c/a/c/g/f': fts_info=FTS_D , isd consider_visiting (late): `./c/a/c/g/f': fts_info=FTS_D , isd
consider_visiting (early): `./c/a/c/g/f': fts_info=FTS_DP, ft consider_visiting (early): `./c/a/c/g/f': fts_info=FTS_DP, ft
consider_visiting (late): `./c/a/c/g/f': fts_info=FTS_DP, isd consider_visiting (late): `./c/a/c/g/f': fts_info=FTS_DP, isd
consider_visiting (early): `./c/a/c/g': fts_info=FTS_DP, fts_ consider_visiting (early): `./c/a/c/g': fts_info=FTS_DP, fts_
consider_visiting (late): `./c/a/c/g': fts_info=FTS_DP, isdir consider_visiting (late): `./c/a/c/g': fts_info=FTS_DP, isdir
consider_visiting (early): `./c/a/c': fts_info=FTS_DP, fts_le consider_visiting (early): `./c/a/c': fts_info=FTS_DP, fts_le
consider_visiting (late): `./c/a/c': fts_info=FTS_DP, isdir=1 consider_visiting (late): `./c/a/c': fts_info=FTS_DP, isdir=1
consider_visiting (early): `./c/a': fts_info=FTS_DP, fts_leve consider_visiting (early): `./c/a': fts_info=FTS_DP, fts_leve
consider_visiting (late): `./c/a': fts_info=FTS_DP, isdir=1 i consider_visiting (late): `./c/a': fts_info=FTS_DP, isdir=1 i
consider_visiting (early): `./c': fts_info=FTS_DP, fts_level= consider_visiting (early): `./c': fts_info=FTS_DP, fts_level=
consider_visiting (late): `./c': fts_info=FTS_DP, isdir=1 ign consider_visiting (late): `./c': fts_info=FTS_DP, isdir=1 ign
consider_visiting (early): `.': fts_info=FTS_DP, fts_level= 0 consider_visiting (early): `.': fts_info=FTS_DP, fts_level= 0
consider_visiting (late): `.': fts_info=FTS_DP, isdir=1 ignor consider_visiting (late): `.': fts_info=FTS_DP, isdir=1 ignor
./c/a/c ./c/a/c
わかると、Stephenのアプローチには私のアプローチにはない欠陥があります。梅干しにも。私はこれが彼のアプローチが無視されるべきディレクトリに入る余分な作業を避けることを示していると思います。
答え3
findのマニュアルページによると:
-prune True; if the file is a directory, do not descend into it. If
-depth is given, false; no effect.
したがって、あなたの具体的なケースでは、次のことを行います。
find a/* -mindepth 2 -type f -print
しかし、もちろん、必ずしもそう簡単な解決策があるわけではありません。