find 查詢命令 & 正則表達式


今日內容

  • 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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM