Procura arquivos excluindo alguns diretórios

1

Estou trabalhando com a seguinte estrutura de diretório:

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

O que eu quero fazer é executar um comando em todos os arquivos no diretório de nível superior e no diretório Testing . Eu não quero executar o comando nos arquivos nos diretórios Sourced e Modelos . Para fazer isso, eu corri o seguinte comando:

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

Este exemplo não executou o comando em nenhum dos arquivos na estrutura de diretórios.

Quando eu executei o seguinte comando na mesma estrutura de diretórios:

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

Eu tenho o seguinte resultado

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

Como você pode ver, posso executar um comando na árvore de diretórios e aplicá-lo a todos os arquivos, ou posso tentar ser seletivo e acabar sendo executado sem arquivos. Como posso obter a aplicação seletiva de um comando que eu preciso?

    
por Jonathan 10.05.2016 / 05:32

1 resposta

1

Você pode fazer isso com o Regex, no diretório pai:

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

\./([^/]+|Testing/.*)$ encontrará todos os arquivos ( -type f ) no diretório atual e apenas no diretório Testing .

Para executar um comando, adicione -exec action:

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

Substitua echo pelo comando real.

Exemplo:

$ 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
    
por heemayl 10.05.2016 / 05:48