Linux文件查找
1.find查找概述
為什么要有文件查找,因為很多時候我們可能會忘了某個文件所在的位置,此時就需要通過find來查找。
find命令可以根據不同的條件來進行查找文件,例如:文件名稱、文件大小、文件修改時間、屬主屬組、權限、等等方式。同時find命令是Linux下必須掌握的。
*find 命令的基本語法如下*
命令 | 路徑 | 選項 | 表達式 | 動作 |
---|---|---|---|---|
find | [path…] | [options] | [expression] | [action] |
查找 | 地區 | 妹紙 | 18-25歲 | 約? |
是linux里面的一個實時查找工具,通過制定路徑完成文件查找
find [options] ..... [查找路徑] [查找條件] [處理動作]
查找路徑:查找的位置,默認是當前文件夾
查找條件:制定查找的標準,文件名、大小、類型、日期等等
處理動作:對符合條件的文件做什么操作,默認是輸出到屏幕上
2.find查找示例
*以下列出所有find常用的選項*
1.find名稱查找
#1.創建文件 touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1} #2.查找/etc目錄下包含ifcfg-eth0名稱的文件 [root@lqz ~]# find /etc -name "ifcfg-eth1" #3.-i 忽略大小寫 [root@lqz ~]# find /etc -iname "ifcfg-eth1" #查找/etc目錄下包含ifcfg-eth名稱所有文件 [root@lqz ~]# find /etc/ -name "ifcfg-eth*" [root@lqz ~]# find /etc -iname "ifcfg-eth*"
2.find大小查找
#1.查找大于5M的文件 [root@lqz ~]# find /etc -size +5M #2.查找等于5M的文件 [root@lqz ~]# find /etc -size 5M #3.查找小于5M的文件 [root@lqz ~]# find /etc -size -5M
3.find類型查找
# f 文件 [root@lqz ~]# find /dev -type f # d 目錄 [root@lqz ~]# find /dev -type d # l 鏈接 [root@lqz ~]# find /dev -type l # b 塊設備 [root@lqz ~]# find /dev -type b # c 字符設備 [root@lqz ~]# find /dev -type c # s 套接字 [root@lqz ~]# find /dev -type s # p 管道文件 [root@lqz ~]# find /dev -type p
4.find時間查找
#1.創建測試文件(后期shell會講) [root@lqz ~]# for i in {01..28};do date -s 201904$i && touch file-$i;done #2.查找7天以前的文件(不會打印當天的文件) [root@lqz ~]# find ./ -iname "file-*" -mtime +7 #3.查找最近7天的文件,不建議使用(會打印當天的文件) [root@lqz ~]# find ./ -iname "file-*" -mtime -7 #4.查找第7天文件(不會打印當天的文件) [root@lqz ~]# find ./ -iname "file-*" -mtime 7 #5.本地文件保留最近7天的備份文件, 備份服務器保留3個月的備份文件(實際使用方案) find /backup/ -iname "*.bak" -mtime +7 -delete find /backup/ -iname "*.bak" -mtime +90 -delete
5.find用戶查找
#查找屬主是jack [root@lqz ~]# find /home -user jack #查找屬組是admin [root@lqz ~]# find /home -group admin #查找屬主是jack, 屬組是admin [root@lqz ~]# find /home -user jack -group admin #查找屬主是jack, 并且屬組是admin [root@lqz ~]# find /home -user jack -a -group admin #查找屬主是jack, 或者屬組是admin [root@lqz ~]# find /home -user jack -o -group admin #查找沒有屬主 [root@lqz ~]# find /home -nouser #查找沒有屬組 [root@lqz ~]# find /home -nogroup #查找沒有屬主或屬組 [root@lqz ~]# find /home -nouser -o -nogroup
6.find權限查找
#精切匹配644權限 [root@lqz ~]# find . -perm 644 -ls #包含444權限即可 [root@lqz ~]# find . -perm -444 -ls #查找全局可寫(每位權限必須包含w) [root@lqz ~]# find . -perm -222 -ls #包含set uid [root@lqz ~]# find /usr/sbin -perm -4000 -ls #包含set gid [root@lqz ~]# find /usr/sbin -perm -2000 -ls #包含sticky [root@lqz ~]# find /usr/sbin -perm -1000 -ls
查找條件
根據文件名查找
-name 指定名稱,可以使用正則
-iname 忽略大小寫
-links n 引用次數為n的文件
-regex 后面跟完整路徑,而不是文件名, 必須整個路徑完全匹配
制定搜索的層級
-maxdepth level 最大的搜索深度,指定的目錄為第1層
-mindepth level 最小的搜索深度,包括level層
根據屬主、屬組來查找
-user username 查找屬主為username的文件
-group groupname 查找屬組為groupname的文件
-uid id 查找屬主為id的文件
-gid id 查找屬組為id的文件
-nouser 查找沒有屬主的文件
-nogroup 查找沒有屬組的文件
m[root@192 test]#chown qiao b m[root@192 test]#ll total 0 drwxr-xr-x. 4 root root 24 Dec 4 22:50 a -rw-r--r--. 1 qiao root 0 Dec 6 17:53 b m[root@192 test]#chown :llx b m[root@192 test]#ll total 0 drwxr-xr-x. 4 root root 24 Dec 4 22:50 a -rw-r--r--. 1 qiao llx 0 Dec 6 17:53 b m[root@192 test]#find -group llx ./b m[root@192 test]#id root uid=0(root) gid=0(root) groups=0(root) m[root@192 test]#id qiao uid=1000(qiao) gid=1000(qiao) groups=1000(qiao) m[root@192 test]#find -uid 1000 ./b m[root@192 test]#useradd xiaobao m[root@192 test]#chown xiaobao b m[root@192 test]#ll total 0 drwxr-xr-x. 4 root root 24 Dec 4 22:50 a -rw-r--r--. 1 xiaobao llx 0 Dec 6 17:53 b m[root@192 test]#userdel xiaobao m[root@192 test]#ll total 0 drwxr-xr-x. 4 root root 24 Dec 4 22:50 a -rw-r--r--. 1 1002 llx 0 Dec 6 17:53 b m[root@192 test]#find -nouser ./b # 全盤找 m[root@192 test]#find / -nouser
根據文件類型 -type
d 目錄
f 文件
l 符號鏈接
s 套接字
b 塊設備
c 字符設備
p 管道文件
m[root@192 test]#find -type f ./b
空文件或者空目錄
-empty
m[root@192 test]#find -empty
條件
與 -a
或 -o
非 -not
m[root@192 test]#find -empty -o -type d m[root@192 test]#find -empty -not -type d ./b
摩根定律
非(A或者B) 非A 且非B
非(A且B)非A或非B
m[root@192 ~]#find !(-empty -a -tpye d)
排除目錄
-path
[root@localhost test]#find /etc -name *_config /etc/ssh/ssh_config /etc/ssh/sshd_config [root@localhost test]#find /etc -path /etc/ssh -name *_config
按照大小來查找
-size # (#-1,#] 不包括#-1,包括#
-size -# [0,#-1] 包括#-1
-size +# (#,......)
按照時間來查找
-amin
-mmin
-cmin
-atime # [#,#+1)
-atime -# (0,#)
-atime +# [#+1,....]
查找7天以后的文件 find -atime +7
-mtime
-ctime
以分鐘為單位
3 處理動作
find動作處理,比如查找到一個文件后,需要對文件進行如何處理, find的默認動作是 -print
1.find查找后的動作命令示例
動作 | 含義 |
---|---|
打印查找到的內容(默認) | |
-ls | 以長格式顯示的方式打印查找到的內容 |
-delete | 刪除查找到的文件(僅能刪除空目錄) |
-ok | 后面跟自定義 shell 命令(會提示是否操作) |
-exec | 后面跟自定義 shell 命令(標準寫法 -exec |
- -print 默認的處理動作,顯示在屏幕上
- -ls 類似于ls -l 顯示長格式
- -delete 刪除查找到的文件
- -fls file 將查找的結果以長格式保存到文件中
- -ok command {} ; 對每一個查找到的文件執行command命令,在執行命令之前要先提示用戶是否要執行
find -size 2M -ok rm -rf {} ; 找到2M的文件,刪除,提示刪除
-exec command {} ; 對查到的每一個文件執行command命令,不需要確認,一次性交給后面命令處理
find -size 2M -exec rm -rf {} ;
m[root@192 test]#find -size 2M -delete
#1.使用-print打印查找到的文件 [root@lqz ~]# find /etc -name "ifcfg*" [root@lqz ~]# find /etc -name "ifcfg*" -print #2.使用-ls打印查找到的文件,以長格式顯示 [root@lqz ~]# find /etc -name "ifcfg*" -ls #3.使用-delete刪除文件,但僅能刪除空目錄 [root@lqz ~]# find /etc -name "ifcfg*" -delete #4.使用-ok實現文件拷貝,但會提示是否拷貝 [root@lqz ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp ; #5.使用-exec實現文件拷貝和文件刪除。 [root@lqz ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp ; [root@lqz ~]# find /etc -name "ifcfg*" -exec rm -f {} ;
2.使用find命令結合xargs
有的命令不支持管道
命令參數過長
xargs 將管道前面的內容一條一條的交給后面命令處理
echo file{1..50000}|xargs touch
一般會跟find使用
#xargs將前者命令查找到的文件作為一個整體傳遞后者命令的輸入 [root@lqz ~]# touch file.txt [root@lqz ~]# find . -name "file.txt" |xargs rm -f [root@lqz ~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp
3.find邏輯運算符
符號 | 作用 |
---|---|
-a | 與 |
-o | 或 |
-not|! | 非 |
#1.查找當前目錄下,屬主不是hdfs的所有文件 [root@lqz ~]# find . -not -user hdfs [root@lqz ~]# find . ! -user hdfs #2.查找當前目錄下,屬主屬于hdfs,且大小大于300字節的文件 [root@lqz ~]# find . -type f -a -user hdfs -a -size +300c #3.查找當前目錄下的屬主為hdfs或者以xml結尾的普通文件 [root@lqz ~]# find . -type f -a ( -user hdfs -o -name '*.xml' )
4.find相關練習題
1.查找/tmp目錄下,屬主不是root,且文件名不以f開頭的文件 2.查找/var目錄下屬主為root,且屬組為mail的所有文件 3.查找/var目錄下不屬于root、lp、gdm的所有文件 4.查找/var目錄下最近一周內其內容修改過,同時屬主不為root,也不是postfix的文件 5.查找/etc目錄下大于1M且類型為普通文件的所有文件 6.將/etc/中的所有目錄(僅目錄)復制到/tmp下,目錄結構不變 7.將/etc目錄復制到/var/tmp/,/var/tmp/etc的所有目錄權限777/var/tmp/etc目錄中所有文件權限666 8.保留/var/log/下最近7天的日志文件,其他全部刪除 9.創建touch file{1..10}10個文件, 保留file9,其他一次全部刪除 10.解釋如下每條命令含義 mkdir /root/dir1 touch /root/dir1/file{1..10} find /root/dir1 -type f -name "file5" find /root/dir1 ! -name "file5" find /root/dir1 -name "file5" -o -name "file9" find /root/dir1 -name "file5" -o -name "file9" -ls find /root/dir1 ( -name "file5" -o -name "file9" ) -ls find /root/dir1 ( -name "file5" -o -name "file9" ) -exec rm -rvf {} ; find /root/dir1 ! ( -name "file4" -o -name "file8" ) -exec rm -vf {} ;
linux三劍客
三劍客詳解
grep
awk
sed
grep
grep [option] "模式" file
option
--color=auto 對匹配到的行添加顏色 -v 取反 -i 不區分大小寫 -n 查找的內容增加行號 -c 打印匹配到的行數 -o 只顯示匹配到的文字 -q 靜默模式 -A # after 向下顯示#行 -B # before 向上顯示#行 —C # context 上下分別顯示#行 -e 或者 grep -e 'user' -e 'root' passwd 或的意思 -E 擴展正則表達式 -F 不使用正則表達式 -r 遞歸 -w 匹配整個單詞
正則表達式元字符
字符匹配
. 任意單個字符
[] 匹配指定范圍內的任意單個字符 [0-9] [a-z] [A-Z]
[^] 取反
[] 大寫字母
[] 小寫字母
[] 字母和數字
[] 大小寫字母
[] 數字
m[root@192 test]#grep '[[]]+' c # 匹配數字 [:black:] 空白 [:punct:] 標點符號 匹配次數 * 表示任意次數 .* 任意字符任意次數 ? 表示0或者1次 + 至少一次 {n} 表示n次 轉義字符 {m,n} 最少m次,最多n次 {n,} 至少n次 {,n} 至多n次 位置錨定 ^ 開頭 $結尾 ^$ 空行 grep -v "^#" /etc/ssh/sshd_config |grep -v "^$" 顯示不以#開頭并且不是空行
分組
grep "(c|C)at" a
向后引用
1 前面第一個括號出現的內容匹配完成之后再后面在出現一次
2 前面第二個括號出現的內容匹配完成之后再后面在出現一次
grep "(l..e).*1" c love djfdjfd;d love
擴展正則表達式
與正則表達式的區別是不需要轉義
grep -E "(c|C)at" a
不需要加進行轉義
-
Linux
+關注
關注
87文章
11345瀏覽量
210385 -
Find
+關注
關注
0文章
54瀏覽量
11667
原文標題:正則表達式元字符
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論