find 查詢命令 & 正則表示式
阿新 • • 發佈:2021-12-20
今日內容
- find
- 正則表示式
- Linux 三劍客之 grep
內容詳細
一、find 按名稱或屬性查詢檔案
按名稱查詢
find [查詢目錄] [引數] [] 萬用字元 : * 表示匹配多個字元 ? 表示匹配一個字元 忽略大小寫 : -i find /etc -name '*.repo' find /etc -iname '*Host' [root@localhost ~]# find /etc/yum.repos.d -name '*.repo' /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-CR.repo [root@localhost ~]# find /etc -iname 'Hos*' /etc/host.conf /etc/hosts
按照時間查詢
+ 表示幾天之前; - 表示幾天以內 -mtime : 按檔案內容被修改時間 -ctime : 按檔案屬性被修改時間 -atime : 按檔案最新被訪問時間 find /etc -mtime +5 find /etc -ctime -3 find /etc -atime -5 [root@localhost ~]# find /root -mtime -3 /root /root/anaconda-ks.cfg /root/.bash_history [root@localhost ~]# find /root -ctime -3 /root /root/.bash_logout /root/.bash_profile
按檔案大小查詢
+ 為大於; - 為小於;沒符號是等於;數字後面跟單位(M, b, k..) `b' for 512-byte blocks `c' for bytes `w' for two-byte words `k' for Kilobytes (units of 1024 bytes) `M' for Megabytes (units of 1048576 bytes) `G' for Gigabytes (units of 1073741824 bytes) find /etc -size +5M [root@localhost ~]# find /etc -size 2M /etc/selinux/targeted/contexts/files/file_contexts.bin [root@localhost ~]# find /etc -size +100k /etc/pki/ca-trust/extracted/java/cacerts /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
按 所屬使用者 查詢
# 查詢所有所屬主是 root 的檔案
find /etc -user root
[root@localhost etc]# find /var/spool/ -user root
/var/spool/lpd
/var/spool/mail
/var/spool/anacron
按所屬組查詢
# 查詢所有所屬組是 root 的檔案
find /etc -group root
[root@localhost etc]# find /var/log -group root
/var/log/tallylog
/var/log/grubby_prune_debug
/var/log/lastlog
按照檔案型別查詢
命令: find /root -type []
# 檔案型別:
普通檔案 : f
資料夾 : d
連結檔案 : l
裝置檔案:
磁碟檔案 : b
字元檔案 : c
套接字檔案 : s
管道檔案 : p
[root@localhost test]# echo {1..4}.txt | xargs touch
[root@localhost test]# find /test -type f
/test/1.txt
/test/2.txt
/test/3.txt
/test/4.txt
[root@localhost test]# find /test -type d
/test/index1
/test/index2
按 index node 號查詢
find /root -inum []
[root@localhost test]# find /test -inum 202280969
/test/10.txt
按許可權查詢
find /root -perm []
[root@localhost test]# find /test -perm 644
/test/1.txt
/test/2.txt
/test/3.txt
/test/4.txt
[root@localhost test]# find /test -perm 755
/test/index1
/test/index2
其它引數
-a : 並且(可以省略,預設時並且)
-o : 或者
-maxdepth : 查詢的目錄深度(必須放置與第一個引數位)
-exec : 將find處理好的結果交給其它命令繼續處理 (結果以 {} 表示)
知識儲備
知識儲備:
# dd 可以造出指定大小的檔案,造出來的檔案是用 '@' 符號填充
dd : 生成檔案(生成指定大小的檔案)
if :從什麼地方讀
of : 寫入到什麼檔案
bs : 每次寫入多少記憶體的內容
count : 寫入多少次
dd if=/dev/zero of=5.txt bs=1M count=5
知識儲備
| : 前面一個命令的結果交給後面一個命令處理
xargs : 把處理的文字變成以空格分割的一行
`` : 提前執行命令,然後將結果交給其他命令來處理
[root@localhost test]# echo {a..d} | xargs mkdir
drwxr-xr-x. 2 root root 6 Dec 20 20:25 a
drwxr-xr-x. 2 root root 6 Dec 20 20:25 b
drwxr-xr-x. 2 root root 6 Dec 20 20:25 c
drwxr-xr-x. 2 root root 6 Dec 20 20:25 d
tar -czPf /tmp/etcv2.tar.gz `find /etc/ -type f | xargs`
實操
案例1:查詢/etc目錄下hosts檔案
[root@localhost ~]# find /etc/ -name 'hosts'
/etc/hosts
案例2:查詢/etc目錄下名稱中包含hosts檔案
[root@localhost ~]# find /etc/ -name '*hosts*'
案例3:要求把/etc目錄下,所有的普通檔案打包壓縮到/tmp目錄
[root@localhost /tmp]# tar -czPf /tmp/etcv2.tar.gz `find /etc/ -type f | xargs`
二、Linux 三劍客之 grep
Linux三劍客之一,文字過濾器(根據文字內容過濾檔案)。
語法格式
grep [引數] [匹配規則] [操作物件]
引數
引數:
-n : 過濾文字時,將過濾出來的內容在檔案內的行號顯示出來
-A : 匹配成功之後,將匹配行的後n行顯示出來
-B : 匹配成功之後,將匹配行的前n行顯示出來
-C : 匹配成功之後,將匹配行的前後各n行顯示出來
-c : 只顯示匹配成功的行數
-o : 只顯示匹配成功的內容
-v : 反向過濾
-q : 靜默輸出
-i : 忽略大小寫
-l : 匹配成功之後,將文字的名稱打印出來
-R|-r : 遞迴匹配
-E : 使用拓展正則 等價於 egrep
知識儲備
知識儲備:
$? : 上一行命令執行的結果,0代表執行成功,其他數字代表執行失敗。
wc : 匹配行數
-l : 列印匹配行數
-c : 列印匹配的位元組數
在/etc目錄下,有多少個檔案包含root。
grep -rl 'root' /etc/ | wc -l
三、正則表示式
1、正則表示式的分類(grep)
1、普通正則表示式
2、拓展正則表示式
2、普通正則表示式
特殊符號
^ : 以某字元開頭
$ : 以某字元結尾
. : 匹配除換行符之外的任意單個字元
[] : 某組字串的任意一個字元
[a-z] : 匹配小寫字母
[A-Z] : 匹配大寫字母
[a-zA-Z] : 匹配字母
[0-9] : 匹配數字
[^] : 取反
() : 分組
\ : 取消轉義
\n : 代表第n個分組
量詞
* :匹配前導字元的任意個數
3、拓展正則
{} :匹配的次數
{n} : 匹配n次
{n,} :至少匹配n次
{n,m} :匹配 n 到 m 次
{,m} :最多匹配m次
+ :匹配至少有一個前導字元
? : 匹配一個或零個前導字元
| :或
4、實操案例
案例1:在/etc/passwd檔案中,匹配以ftp開頭的行
grep '^ftp' /etc/passwd
案例2:在/etc/passwd檔案中,匹配以bash結尾的行
grep 'bash$' /etc/passwd
案例3:匹配本機中有哪些ip
ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'
案例4:要求將/etc/fstab中的去掉包含 # 開頭的行,且要求 # 後至少有一個空格
grep -vE '^#\ +' /etc/fstab
案例5:找出檔案中至少有一個空格的行
grep -E '\ +' xxx
案例6:將 nginx.conf 檔案中以#開頭的行和空行,全部刪除
grep -vE '^\ *#|^$' /etc/nginx/nginx.conf