一部のディレクトリを除いたファイルを検索する

一部のディレクトリを除いたファイルを検索する

私は次のディレクトリ構造で作業しています:

onathan@Aristotle:~/EclipseWorkspaces/ShellUtilities/ShellUtilities$ ls -R
.:
calculateTargetDay  CustomizeIso  ec  makeExecutable  Models  modifyElementList  Sourced  test file  Testing  valrelease

./Models:
testcase

./Sourced:
colors  stddefs  stdFunctions  SupportTesting

./Testing:
test  testCalculateTargetDay  testColors  testModifyElementList  testStddefs  testStdFunctions  testSupportTesting  tst

私がやりたいのは、トップレベルのディレクトリとディレクトリ内のすべてのファイルに対してコマンドを実行することですテストディレクトリ内のファイルに対してコマンドを実行したくないソースそしてモデルこれを行うには、次のコマンドを実行しました。

find . -name Sourced -prune -name Models -prune ! -name '\.*'  -execdir echo '{}' \;

この例では、ディレクトリ構造内のどのファイルに対してもコマンドは実行されません。

同じディレクトリ構造に対して次のコマンドを実行しました。

find . ! -name '\.*'  -execdir echo '{}' \;

次のような結果が出ました

./calculateTargetDay
./CustomizeIso
./Testing
./testModifyElementList
./test
./testColors
./testStdFunctions
./testCalculateTargetDay
./testStddefs
./testSupportTesting
./tst
./test file
./modifyElementList
./ec
./Sourced
./stdFunctions
./stddefs
./SupportTesting
./colors
./valrelease
./Models
./testcase
./makeExecutable

ご覧のとおり、ディレクトリ ツリーに対して 1 つのコマンドを実行して、それをすべてのファイルに適用することも、選択的に実行して、ファイルに対して実行しないようにすることもできます。必要なコマンドを選択的に適用するにはどうすればよいでしょうか。

答え1

親ディレクトリから、正規表現を使用して実行できます。

find . -type f -regextype posix-egrep -regex '\./([^/]+|Testing/.*)$'

\./([^/]+|Testing/.*)$-type f現在のディレクトリとディレクトリ内のみにあるすべてのファイル ( ) を検索しますTesting

コマンドを実行するには、-execアクションを追加します。

find . -type f -regextype posix-egrep -regex '\./([^/]+|Testing/.*)$' -exec echo {} \;

echo実際のコマンドに置き換えます。

例:

$ find . -type f                                                                           
./foo
./Sourced/src
./Testing/test
./bar
./spam
./Models/model

$ find . -type f -regextype posix-egrep -regex '\./([^/]+|Testing/.*)$'                 
./foo
./Testing/test
./bar
./spam

$ find . -type f -regextype posix-egrep -regex '\./([^/]+|Testing/.*)$' -exec echo {} \;
./foo
./Testing/test
./bar
./spam

関連情報