人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Linux中使用grep命令搜索文件名及文件內(nèi)容的方法

發(fā)布日期:2022-01-23 13:36 | 文章來源:gibhub

從文件中搜索并顯示文件名
當你從不止一個的文件中搜索時,默認它將顯示文件名:

復制代碼
代碼如下:

grep "word" 文件名
grep root /etc/*

示例輸出:

復制代碼
代碼如下:

/etc/bash.bashrc: See "man sudo_root" for details.
/etc/crontab:17 * * * *root cd / && run-parts --report /etc/cron.hourly
/etc/crontab:25 6 * * *root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
/etc/crontab:47 6 * * 7root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
/etc/crontab:52 6 1 * *root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
/etc/group:root:x:0:
grep: /etc/gshadow: Permission denied
/etc/logrotate.conf: create 0664 root utmp
/etc/logrotate.conf: create 0660 root utmp

每行開始的第一個部分是文件名(如:/etc/crontab、/etc/group)。使用 -l 選項可以只顯示文件名:

復制代碼
代碼如下:

grep -l "string" filename
grep -l root /etc/*

示例輸出:

復制代碼
代碼如下:

/etc/aliases
/etc/arpwatch.conf
grep: /etc/at.deny: Permission denied
/etc/bash.bashrc
/etc/bash_completion
/etc/ca-certificates.conf
/etc/crontab
/etc/group

你也可以逆轉(zhuǎn)輸出;使用 -L 選項來輸出那些不匹配的文件的文件名:

復制代碼
代碼如下:

grep -L "word" filename
grep -L root /etc/*

示例輸出:

復制代碼
代碼如下:

/etc/apm
/etc/apparmor
/etc/apparmor.d
/etc/apport
/etc/apt
/etc/avahi
/etc/bash_completion.d
/etc/bindresvport.blacklist
/etc/blkid.conf
/etc/bluetooth
/etc/bogofilter.cf
/etc/bonobo-activation
/etc/brlapi.key

根據(jù)文件內(nèi)容查找文件
輸入以下命令:

復制代碼
代碼如下:

grep 'string' *.txt
grep 'main(' *.c
grep '#include<example.h>' *.c
grep 'getChar*' *.c
grep -i 'ultra' *.conf
grep -iR 'ultra' *.conf

其中
-i : 忽略模式(匹配字符串 valid、 VALID、 ValID )和輸入文件(匹配 file.c FILE.c FILE.C)的大小寫。
-R : 遞歸讀取每個目錄下的所有文件。

高亮匹配到的模式
在搜索大量文件的時候你可以輕松地高亮模式:

復制代碼
代碼如下:

$ grep --color=auto -iR 'getChar();' *.c

為查找到的模式顯示文件名和行號
你也許需要顯示文件名和行號:

復制代碼
代碼如下:

$ grep --color=auto -iRnH 'getChar();' *.c

其中,
-n : 在輸出的每行前面添加以 1 開始的行號。
-H : 為每個匹配打印文件名。要搜索多個文件時這是默認選項。(LCTT 譯注:-h 選項強制隱藏文件名;另外 -l 和 -L 選項用于僅顯示匹配/不匹配的文件名,而 -H 和 -h用于控制在顯示匹配行前顯示/不顯示文件名,注意區(qū)分。)

復制代碼
代碼如下:

$grep --color=auto -nH 'DIR' *

輸出樣例:

你也可以使用 find 命令:

復制代碼
代碼如下:

$ find . -name "*.c" -print | xargs grep "main("

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關(guān)注
微信

關(guān)注官方微信
頂部