find -name 搜索命令及搜索結果顯示顏色設置

在 Linux 下用 grep 時高亮顯示匹配的部分

用 grep 匹配文件時,顯示結果黑壓壓的一片

在你執行find命令前,先執行一下這條命令,重新 grep 試試看
export GREP_OPTIONS='--color=auto'
好看多了,不是嗎?
 也可以這命令加入到bash_profile,以後就沒必要每次搜索前都執行export GREP_OPTIONS='--color=auto'命令
你可以把 export GREP_OPTIONS='--color=auto' 這條命令添加到 ~/.bash_profile 或.bashrc文件(具體可自己進入自己用戶目錄下執行ls -al 查看)的最後,重新登錄一下就能生效了

(備註).bash_profile 文件就在你當前用戶目錄下面:

cd ~

ll

已可以看到該文件,但並不是每個人的電腦的是這個文件,當有沒有這個文件時必要.bashrc 文件,將 export GREP_OPTIONS='--color=auto' 命令加入 .bashrc文件最後面即可,

 


grep -srn "cannot_connect_camera_new"    //用此命令搜索

 

find -name "build.prop"|xargs rm -rf {};  搜索當前目錄下的所有build.prop文件並刪除

 

find -name "*.xml"|xargs grep -nrws "config_systemUIServiceComponents"

解釋:*.xml  只查找.xml文件,n是顯示行數 r是忽悠大小寫,

在ubuntu中搜索文件一直是用的: find -name "*" |xargs grep “xxx",在用了一陣後感覺這命令還是太好,顯示的搜索信息太多了,不匹配的搜索結果也顯示在控件臺中,百度了一下,可以用以下兩個參數:

xxxx@ubuntu:~/project/MSM8916_R113502NEW/LINUX/android/packages$ find -name "*" |xargs grep -s "custom_content_page"


./apps/Launcher3/res/layout/custom_content_page_indicator_marker.xml:        android:src="@drawable/custom_content_page"
./apps/Launcher3/res/layout/custom_content_page_indicator_marker.xml:        android:src="@drawable/custom_content_page"

 


xxxx@ubuntu:~/project/MSM8916_R113502NEW/LINUX/android/packages$ find -name "*" |xargs grep -l "custom_content_page"
./apps/Launcher3/res/layout/custom_content_page_indicator_marker.xml

 

參數-s 與的-l的區別是 -s 會顯示要搜索內容,而-l只會顯示匹配搜索的文件名:

 

grep [options]

3.主要參數

[options]主要參數:

-c:只輸出匹配行的計數。

-I:不區分大 小寫(只適用於單字符)。

-h:查詢多文件時不顯示文件名。

-l:查詢多文件時只輸出包含匹配字符的文件名。

-n:顯示匹配行及 行號。

-s:不顯示不存在或無匹配文本的錯誤信息。

-v:顯示不包含匹配文本的所有行。

 -w: 按單詞搜索

 -r: 逐層遍歷目錄查找

 

如果想查找當前目錄(/home/student)下的tmp.txt文件,但是想要避開sep目錄:

 find /home/student -path /home/student/sep -prune -o -name "tmp.txt" -print

 

 sep後面不能加/ 即/home/student/sep/是錯誤的 如果當前目錄爲/home/student 也可以這樣

 find . -path ./sep -prune -o -name "tmp.txt" -print

總結:-path 只是匹配find出來的路徑,可以通過使用匹配符號* [] ?等 例如:

 

 [student@bioeng ~]$ find . -name file

./file

./dir/file

./dir/dir555/file

./dir/dir2/file

./dir/dir1/file

[student@bioeng ~]$

 

 [student@bioeng ~]$ find . -path "*dir[12]" -prune -o -name file -print

./file

./dir/file

./dir/dir555/file

 

 [student@bioeng ~]$ [student@bioeng ~]$ find . -path "*dir*" -prune -o -name file -print

./file

 [student@bioeng ~]$

如:搜索字符串“SystemUI”並排除out目錄:

find . -path ./out -prune -o -type f -name "*.java" -print | xargs grep -swn "xxx"
find . -path ./out -prune -o -type f -print | xargs grep -swn "xxxx"   備註:w選項是精確查找

多關鍵字搜索:

find -name "*"|xargs grep  -e  'sim|Sim'

grep "ro.mtk_hdmi_support" -nrw device/

查找到替換字符串:

find . -path ./out -prune -o -name "*.java" -print0 | xargs -0 perl -pi -e 's/xxxx/xxxxtihuan/g'  //替換將xxxx字符串搜索出來然後替換成:xxxxtihuan

其中find 命令的-print0選項一定要加,要不然會在替換的時候會提示文件打不開,

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章