일부 디렉토리를 제외한 파일 찾기

일부 디렉토리를 제외한 파일 찾기

나는 다음 디렉토리 구조로 작업하고 있습니다.

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

관련 정보