Saída detalhada do comando Bash find

37

Existe uma maneira de dizer ao comando bash find para gerar o que está fazendo (modo detalhado)?

Por exemplo, para o comando: find /media/1Tb/videos -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \; para a saída:

Found /media/1Tb/videos/102, executing rm -rf /media/1Tb/videos/102
...
    
por Alex 07.10.2011 / 07:59

5 respostas

52

Você pode inventar algo com -printf , mas o mais fácil é apenas colocar -print no final. Isso mostrará o que foi excluído com sucesso.

    
por 07.10.2011 / 08:02
17

Que tal usar apenas rm -vf para a saída verbose rm?

$ touch file1 file2 file3
$ find . -name "file?" -exec rm -vf {} \;
removed './file2'
removed './file3'
removed './file1'
    
por 07.10.2011 / 08:02
8

Uma alternativa é permitir que os comandos sejam executados por sh -x :

$ find . -type f -print0 | xargs -0 -n1 echo rm | sh -x
+ rm ./file1
+ rm ./file2
+ rm ./file3
    
por 07.10.2011 / 11:08
1

Há também find -D xxxx que pode ajudar em alguns casos.

 $ find -D help
 Valid arguments for -D:
 help       Explain the various -D options
 tree       Display the expression tree
 search     Navigate the directory tree verbosely
 stat       Trace calls to stat(2) and lstat(2)
 rates      Indicate how often each predicate succeeded
 opt        Show diagnostic information relating to optimisation
 exec       Show diagnostic information relating to -exec, -execdir, -ok and -okdir

Abaixo estão dois exemplos de find -D search :

Usando o RHEL 6.3 ( find v4.4):

$ mkdir -p aa/bb
$ touch aa/11 aa/22 aa/33 aa/bb/44 aa/bb/55
$ find -D search aa -type f -delete
consider_visiting: fts_info=FTS_D , fts_level= 0, prev_depth=-2147483648 fts_path='aa', fts_accpath='aa'
consider_visiting: fts_info=FTS_D , fts_level= 1, prev_depth=0 fts_path='aa/bb', fts_accpath='bb'
consider_visiting: fts_info=FTS_NSOK, fts_level= 2, prev_depth=1 fts_path='aa/bb/55', fts_accpath='55'
consider_visiting: fts_info=FTS_NSOK, fts_level= 2, prev_depth=2 fts_path='aa/bb/44', fts_accpath='44'
consider_visiting: fts_info=FTS_DP, fts_level= 1, prev_depth=2 fts_path='aa/bb', fts_accpath='bb'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/22', fts_accpath='22'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/33', fts_accpath='33'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/11', fts_accpath='11'
consider_visiting: fts_info=FTS_DP, fts_level= 0, prev_depth=1 fts_path='aa', fts_accpath='aa'
$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX FTS() CBO(level=0)

Usando o Cygwin 1.7 ( find 4.5):

$ mkdir -p aa/bb
$ touch aa/11 aa/22 aa/33 aa/bb/44 aa/bb/55
$ find -D search aa -type f -delete
consider_visiting (early): 'aa': fts_info=FTS_D , fts_level= 0, prev_depth=-2147483648 fts_path='aa', fts_accpath='aa'
consider_visiting (late): 'aa': fts_info=FTS_D , isdir=1 ignore=1 have_stat=1 have_type=1
consider_visiting (early): 'aa/11': fts_info=FTS_NSOK, fts_level= 1, prev_depth=0 fts_path='aa/11', fts_accpath='11'
consider_visiting (late): 'aa/11': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/22': fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/22', fts_accpath='22'
consider_visiting (late): 'aa/22': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/33': fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/33', fts_accpath='33'
consider_visiting (late): 'aa/33': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb': fts_info=FTS_D , fts_level= 1, prev_depth=1 fts_path='aa/bb', fts_accpath='bb'
consider_visiting (late): 'aa/bb': fts_info=FTS_D , isdir=1 ignore=1 have_stat=1 have_type=1
consider_visiting (early): 'aa/bb/44': fts_info=FTS_NSOK, fts_level= 2, prev_depth=1 fts_path='aa/bb/44', fts_accpath='44'
consider_visiting (late): 'aa/bb/44': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb/55': fts_info=FTS_NSOK, fts_level= 2, prev_depth=2 fts_path='aa/bb/55', fts_accpath='55'
consider_visiting (late): 'aa/bb/55': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb': fts_info=FTS_DP, fts_level= 1, prev_depth=2 fts_path='aa/bb', fts_accpath='bb'
consider_visiting (late): 'aa/bb': fts_info=FTS_DP, isdir=1 ignore=0 have_stat=1 have_type=1
consider_visiting (early): 'aa': fts_info=FTS_DP, fts_level= 0, prev_depth=1 fts_path='aa', fts_accpath='aa'
consider_visiting (late): 'aa': fts_info=FTS_DP, isdir=1 ignore=0 have_stat=1 have_type=1
$ find --version
find (GNU findutils) 4.5.11
Packaged by Cygwin (4.5.11-1)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)
    
por 10.09.2013 / 12:08
1

A resposta do hlovdav foi suficiente para mim, mas fiz algumas modificações para meu próprio uso

find . -name 'application*.yml' -print0 | xargs -0 -I %% bash -c 'rm -v "$1"' -- "%%"

Explicação

  1. encontre
  2. pattern
  3. Imprimir null separado, importante se você tiver nomes de arquivo com espaços ou caracteres incomuns nele
  4. xargs read null separated, defina cada alocador de espaço de registro como %% Isso também garante que toda vez que ele usa apenas um argumento
  5. O comando bash , one-liner, qualquer coisa que esteja dentro, deve estar entre aspas simples '
  6. -- significa que qualquer coisa que eu faça depois disso não é xargs ou bash opções, mas parâmetros posicionais para meu script de uma linha
  7. Espaço reservado é dado como argumento único, citando-o, aspas simples ou duplas não importam. Se você usar aspas duplas, também poderá inserir variáveis do shell.
  8. Dentro do script bash , você pode acessar %% como $1 , argumento posicional No # 1

Observação: você pode alterar %% com qualquer coisa, apenas certifique-se de não precisar usá-la para nada além de um espaço reservado. Usar o dólar $ ou @ pode não ser bom, a menos que seja o dobro @ como @@ .

    
por 27.09.2018 / 05:10