Problemas com find, xargs e egrep

Problemas com find, xargs e egrep

É com isso que estou tentando acabar (exceto trabalhar)

find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 egrep -vZ 'vvv|iii'

O que estou fazendo de errado?

$ ll
total 0
-rw-rw-r-- 1 yyy yyy 0 Sep 18 10:36 iii.txt
-rw-rw-r-- 1 yyy yyy 0 Aug 29 10:35 old1.txt
-rw-rw-r-- 1 yyy yyy 0 Aug 29 10:35 old2.txt
-rw-rw-r-- 1 yyy yyy 0 Aug 29 10:35 old3.txt
-rw-rw-r-- 1 yyy yyy 0 Nov 16 09:36 vvv.txt
-rw-rw-r-- 1 yyy yyy 0 Nov  5 09:41 young.txt 
$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 egrep -viZ 'vvv|iii'
$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 egrep -vilZ 'vvv|iii'
$    find ./ -mindepth 1 -type f -mtime +60 -print0 
./old3.txt./old1.txt./old2.txt$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 egrep 'old'
$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 grep 'old'
$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 grep 'o'
$    find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 grep '.*o.*' 
$    find ./ -mindepth 1 -type f -mtime +60 | xargs egrep 'o'
$    find ./ -mindepth 1 -type f -mtime +60 | xargs egrep '.*o.*'
$    find ./ -mindepth 1 -type f -mtime +60
./old3.txt
./old1.txt
./old2.txt
$    find ./ -mindepth 1 -type f -mtime +60 | grep 'o'
./old3.txt
./old1.txt
./old2.txt
$    find ./ -mindepth 1 -type f -mtime +60 | xargs grep 'o'
$    find ./ -mindepth 1 -type f -mtime +60 -print | xargs grep 'o'
$    find . -name "*.txt" | xargs grep "old"
$    find . -name "*.txt"
./old3.txt
./vvv.txt
./iii.txt
./old1.txt
./old2.txt
./young.txt
$ find ./ | grep 'o'
./old3.txt
./old1.txt
./old2.txt
./young.txt
$ find ./ | xargs grep 'o'
$

Eu preciso do grep porque a lista de exclusão virá de um arquivo no final, então apenas usar find para filtrar não é suficiente. Gostaria que o grep NULtambém retornasse uma lista encerrada. E vou canalizar o resultado disso para outra coisa depois, então não sei se a opção find -execseria apropriada.

Coisas que eu olhei:

$ bash -version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
$ cat /proc/version
Linux version 2.6.18-371.8.1.0.1.el5 ([email protected]) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)) #1 SMP Thu Apr 24 13:43:12 PDT 2014

isenção de responsabilidade: não tenho muita experiência em Linux ou shell.

Responder1

Parece que você deseja grepnomes de arquivos, mas se desejar:

find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 egrep -vZ 'vvv|iii'

na xargsverdade, apresenta a lista de arquivos que saem findcomo argumento para egrep.

O que você deve fazer para lidar com a entrada terminada em NUL (de -print0)

find ./ -mindepth 1 -type f -mtime +60 -print0 | xargs -0 grep -EvzZ 'vvv|iii'

( egrepestá obsoleto, é por isso que mudei para grep -E)

De man grep:

   -z, --null-data
          Treat the input as a set of lines, each  terminated  by  a  zero
          byte  (the  ASCII NUL character) instead of a newline.  Like the
          -Z or --null option, this option can be used with commands  like
          sort -z to process arbitrary file names.

   -Z, --null
          Output  a  zero  byte  (the  ASCII NUL character) instead of the
          character that normally follows a file name. 

Então você precisa de ambos -ze-Z

informação relacionada