GREP – how could I exclude one word?

Invert match using grep -v:

grep -v “unwanted word" file pattern


find . -type f -newerct 2013-02-20 -not -newerct 2013-02-21|grep -v .db$

找出 2013-02-21 當天檔案狀態有變更的檔案,但是不要顯示檔名以「.db」結尾的檔案。


利用 「find」 以日期為條件找出被修改或狀態改變的檔案


root@eps:/mnt/toyoink-ct_esf# find . -ctime 2|less


root@eps:/mnt/toyoink-ct_esf# find . -ctime +2|less


root@eps:/mnt/toyoink-ct_esf# find . -ctime -2|less

# 找出 2013-02-20之後(包含)至2013-02-21之前 ,也就是2013-02-20 有被存取過的檔案

find . -newerct 2013-02-20 -not -newerct 2013-02-21


使用 egrep 和 xargs 、sed 組合 搜尋與取代多個設定檔

有些時候我們可能會有很多設定檔,但是設定檔中有些設定參數值是相同的字串參數。就像我的 Nagios 設定,我會將每台所監控的 Host 都單獨的寫在各自的設定檔中。當監控的服務所執行的指命有任何改變時,都有可能需要手動一個一個修改設定檔的參數值(印象中,是更新nssclient++的版本時,新舊版本的命令有不一樣。)。

下面是在目前目錄及它的子目錄中,找尋檔案中包含字串 .jpg .gif .png 並將符合的字串全部改為 .bmp 。

egrep -lRZ "\.jpg|\.png|\.gif" . | xargs -0 -l sed -i -e 's/\.jpg\|\.gif\|\.png/.bmp/g'

除了用「egrep」、我們也可以用「find」或是「ls」找出我們想要的檔案,將它傳給 xargs 當作參數的來源要修改的檔案名。最後,用 sed 取代符合的字串到新的字串值。

egrep: find matching lines using extended regular expressions
-l: only list matching filenames
-R: search recursively through all given directories
-Z: use as record separator
“\.jpg|\.png|\.gif": match one of the strings “.jpg", “.gif" or “.png"
.: start the search in the current directory

xargs: execute a command with the stdin as argument

-0: use as record separator. This is important to match the -Z of egrep and to avoid being fooled by spaces and newlines in input filenames.
-l: use one line per command as parameter

sed: the stream editor

-i: replace the input file with the output without making a backup
-e: use the following argument as expression
‘s/\.jpg\|\.gif\|\.png/.bmp/g’: replace all occurrences of the strings “.jpg", “.gif" or “.png" with “.bmp"
but on freebsd egrep -Z did’nt separate args , so non-use -Z on egrep and -0 on xargs . And then it works fine.

sed: 1: file: extra characters at the end of l command

On *BSD you must use

must use the like this

sed -i '' -e 's/FIND/REPLACE/' file
sed -i -e 's/FIND/REPLACE/' file

I try to modify nagios config file replace ip address to another,use find command to find all config files then use sed to modify the file content.

/usr/local/etc/nagios/objects]# find /usr/local/etc/nagios/objects/ -name '*.cfg' -exec sed -i '' -e 's/192\.168\.0/192\.168\.128/g' {} \;
/usr/local/etc/nagios/objects/ -name '*.cfg' -exec sed -i -e 's/192\.168\.0/192\.168\.128/g' {} \;