find、xargs、egrepに関する問題

find、xargs、egrepに関する問題

これは私が得たい結果です(仕事以外)

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

私は何が間違っていましたか?

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

除外リストは最終的にファイルから来るのでgrepが必要なので、findを使ってフィルタリングするだけでは不十分です。また、grepが終了したリストを返したいと思いますNUL。この結果を別のものにパイプするので、検索オプションが適切かどうかわかりません-exec

私が見たもの:

$ 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

免責事項:私はLinuxやシェルの経験はあまりありません。

ベストアンサー1

grep次のような場合は、ファイル名にbufを使用したいと思います。

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

実際にはとマークされxargsています。findegrep

-print0NUL終了入力(で)をどのように処理する必要がありますか?

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

egrep使用されなくなったので、次のように変更しましたgrep -E。)

から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. 

-zだから両方が必要です。-Z

おすすめ記事