Problemas con find, xargs y egrep

Problemas con find, xargs y egrep

Esto es lo que estoy tratando de hacer (excepto trabajar)

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

¿Qué estoy haciendo mal?

$ 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'
$

Necesito grep porque la lista de exclusión provendrá de un archivo al final, por lo que usar buscar para filtrar no es suficiente. También me gustaría que grep devuelva una NULlista terminada. Y después voy a canalizar el resultado de esto a otra cosa, así que no sé si la opción de búsqueda -execsería apropiada.

Cosas que he mirado:

$ 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

Descargo de responsabilidad: no tengo mucha experiencia en Linux o Shell.

Respuesta1

Parece que quieres hacerlo grepcon los nombres de archivos, pero si lo haces:

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

en xargsrealidad presenta la lista de archivos que salen findcomo argumento de egrep.

Qué debe hacer para manejar la entrada terminada en NUL (de -print0)

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

( egrepestá en desuso, por eso lo cambié a 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. 

Entonces necesitas ambos -zy-Z

información relacionada