尋找排除某些目錄的文件

尋找排除某些目錄的文件

我正在使用以下目錄結構:

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

您可以使用 Regex 從父目錄執行此操作:

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

相關內容