ファイルまたはディレクトリを取得する find コマンド

ファイルまたはディレクトリを取得する find コマンド

ファイルとディレクトリを検索するfindコマンドを作成しようとしていますが、問題は、コマンドの特定の変数に従ってファイルまたはエラーのみを受け取ることです。

このコマンドは何も出力しません。

find '/mnt/downloads/cache/' -depth -mindepth 1 \( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty \) -print

これは、これら2つのフィルタが()内にカプセル化されていないためです。なぜ二重引用符が必要なのですか?

このコマンドはファイルのみを出力し、ディレクトリは出力しません。

find '/mnt/downloads/cache/' -depth -mindepth 1 \( \( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty \) \) -print

2つのパラメータを囲む追加の括弧が唯一の違いです。

このコマンドの最後のバリエーションによってエラーが発生します。

find '/mnt/downloads/cache/' -depth -mindepth 1 \( \( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty {} \) \) -print

find: paths must precede expression: {}

ディレクトリテスト後に2番目の{}セットを追加すると、このエラーが発生するのはなぜですか?

修正されたコマンド:

find "/mnt/downloads/cache" -depth -mindepth 1 \( \
-type f \! -exec fuser -s '{}' \; -o \
-type d \! -empty \) \
-print

ベストアンサー1

暗黙的だからそして-a)の優先順位が高いほど、-o最初の式の右側が1つ-oの単位として扱われます。またはタスクは次のとおりです。

\( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty \) -print

次のように動作します。

\( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty -print \)

したがって、ファイルの場合は実行され、fuserディレクトリの場合は名前が印刷されます。


第二:

\( \( -type f ! -exec fuser -s {} \; \) -o \( -type d -empty \) \) -print

明示的な括弧がありますが、内部括弧が必要ない場合は同じです。

\( -type f ! -exec fuser -s {} \;  -o  -type d -empty  \) -print

fuser無効な値(!= 0、inversion)を返す!すべての空のディレクトリとファイルを印刷する必要があります。


... -empty {} ...パス名で処理される 3 番目のトピックでは、 {}エラー メッセージは何が問題なのかを示します。

おすすめ記事