查找命令(10)

查找命令:find,locate,xargs,execphp


9.1.locatenode

功能:在數據庫中查找,速度快,缺點:不精確,會忽略臨時目錄/tmp、/var/tmp正則表達式

經常使用選項:docker

-i:忽略大小寫shell

-n:打印查找結果的前n行數據庫

示例:express

[root@localhost ~]# locate -i /etc/passwd   #報錯,沒有locate命令
-bash: locate: command not found
[root@localhost ~]# yum install -y mlocate   #安裝locate命令
[root@localhost ~]# updatedb  #刷新數據庫
[root@localhost ~]# locate -i /etc/passwd  #不區分大小寫
/etc/passwd
/etc/passwd-
 [root@localhost ~]# locate -n 1 /etc/passwd
/etc/passwd

9.2.find(重點掌握)vim

功能:在目錄結構中搜索文件,並執行指定的操做。此命令提供了至關多的查找條件,功能很強大。安全

特色:精確查找,磁盤搜索,IO讀寫,cpu開銷相對較大bash

語法: find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

經常使用選項:

-name: 按照文件名查找,支持*號和[]號。

-iname:忽略大小寫按照文件名查找

-perm:按照文件權限來查找,支持徹底指定和-號、+號部分符合。

-prune:使用這一選項可使find命令不在當前指定的目錄中查找,若是同時使用-depth選項,那麼-prune將被find命令忽略。

-user:按照文件屬主來查找

-group:按照文件所屬的組來查找

-mtime :-n +n按照文件的更改時間來查找

-amin(atime): -n +n按照文件的訪問時間來查找

-cmin (ctime):-n +n按照文件狀態的更改時間來查找

stat +文件名 能夠查看amc文件時間

- n表明n天之內,+n表明那天之前,n表明當天

以下面的例子:

25 26 27 28 29 30 31 (當天爲28)

n=3 表示找出28號的文件

+3 表示找出2五、2六、27號的文件

-3 表示找出2九、30、31號的文件

-nogroup:查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。

-nouser:查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。

-newer file1 ! -newer file2查找更改時間比文件file1新但比文件file2舊的文件。

-type:按照文件類型查找

  b - 塊設備文件。

  d - 目錄。

  c - 字符設備文件。

  p - 管道文件。

  l - 符號連接文件。

 f - 普通文件。

 s socket文件

-size n:[c] 查找文件長度爲n塊的文件,帶有c時表示文件長度以字節計。

-depth:在查找文件時,首先查找當前目錄中的文件,而後再在其子目錄中查找。

-fstype:查找位於某一類型文件系統中的文件,這些文件系統類型一般能夠在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。

-mount:在查找文件時不跨越文件系統mount點。

-follow:若是find命令遇到符號連接文件,就跟蹤至連接所指向的文件。

-regex pattern:對搜索結果的 整個路徑 按正規表達式進行過濾,必須對全路徑考慮,例如結果是./test,那正規表達式應該用"./t.*",而不能用"t.*"

-cpio:對匹配的文件使用cpio命令。

find命令還支持使用邏輯運算符:

-a 相似&& ,鏈接兩個不一樣的條件(兩個條件必須同時知足)

-o 相似|| ,鏈接兩個不一樣條件 (兩個條件知足其一便可)

!,也是非、否

-exec:直接執行後面所跟的命令,不提示

-ok:交互式執行後面所跟的命令

-not:對條件取反

示例:

#列出當前目錄及子目錄的全部文件
[root@localhost home]# find
.
./file5
./old03
./old03/.gnome2
./old03/.bash_logout
./old03/.bash_profile
說明:什麼參數都不加就是列出當前目錄及子目錄的全部文件,也能夠這樣寫find .  ,find . -print

#查找特殊目錄文件或路徑
[root@localhost scripts]# find ./test -name "*.txt"
./test/test.txt
./test/town.txt
./test/number.txt
./test/name.txt
[root@localhost scripts]# find ./test -iname "*.txt"   #i忽略大小寫
./test/test.txt
./test/town.txt
./test/number.txt
./test/AV.txt
./test/name.txt
#限制目錄查找的深度
須要用到選項maxdepth
[root@localhost ~]# find . -maxdepth 2 -name "*.vim"  
./.vim_old/.vim
./.vim
[root@localhost ~]# find . -maxdepth 3 -name "*.vim"
說明:maxdepth後接的數字表明層級,在當前目錄下,vim_old爲第一層,.vim到第二層就結束了。跟這個選項還有一個相對的mindepth,這個是從子目錄往上找,maxdepth是從父目錄往下找。

#反向查找
[root@localhost ~]# find . -not -name "*.vim"
[root@localhost ~]# find .  ! -name "*.vim"
[root@localhost ~]#find . \( ! -name '*log*' -a ! -name '*cfg*' \) #找到不包含log和cfg的文件
說明:! -not都是表明不包含.vim後綴的文件
#以文件類型及執行其餘命令查找
[root@localhost ~]# find . -type f -mtime -1 -print  #在當前文件夾下搜索最近1小時被修改的文件並打印
[root@localhost ~]# find . -type d -name ".svn"|xargs rm -rf  #刪除.svn目錄
[root@localhost ~]# find . -type d -name ".svn" -exec rm -rf {} \;  #刪除.svn目錄
[root@localhost ~]#find . -type f -name "*.php" -delete   #搜索並刪除文件
[root@localhost ~]#find ./ -type f -name "*.sh"
f[root@localhost ~]#find /data/bbb -type d \( -name 'city' -o -name 'equipment' -o -name 'soldier'  \) #多目錄查找
[root@localhost ~]#find . -type d -name "*p*"   #遞歸找出帶有p字符的目錄
[root@localhost ~]#find . -type f -exec ls -l {} \; >yourfile #重定向操做
[root@localhost ~]#find . -type d| sort  #列出全部目錄並排序
[root@localhost ~]# find /etc -type f -empty  #查找空文件
[root@localhost ~]#find /etc -type d -empty  #查找空目錄

#以文件修改時間查找
[root@localhost ~]# find . -type f -atime  -7 -print  # #在當前文件夾下搜索最近7天內被訪問過的文件並打印
   1 >date
   2. Mon Aug 23 19:25:44 CST 2010
   3. >find ./ -mtime +22 -a -mtime -54   #列出mtime爲7月的文件,+22表示22天之前就在7月份了,可是前面的日期沒有限定,22天之前包含1-7月份啊,再加個54天之內的,那就是54-22,恰好是7月份的文件。
[root@docker-node5 ~]#find logs -type f -mtime +5 -exec rm {} \;  #在/ l o g s目錄中查找更改時間在5日之前的文件並刪除
[root@docker-node5 ~]#find ./ -mtime +7 -ok rm -f {} \;    #3保留7天之內的文件(7天之前刪掉)
< rm ... ./file8 > ? y
< rm ... ./file7 > ? y
< rm ... ./file6 > ? y
[root@localhost mnt]# find /var/log/ -mtime +3 -type f -print  #找出3天之前被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime -3 -type f -print  #找出3天內被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime 3 -type f -print  #找出第三天被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime +2 -mtime -4 -type f -print #找出第三天被修改過的文檔
#多條件查找
[root@localhost mnt]# find /etc -name "*.sh" -o -name "*.txt" #在etc目錄下查找sh或txt結尾的文件,知足其一便可
[root@docker-node5 ~]#find /etc -name "passwd*" -exec grep "sam" {} \; #查找passwd文件,並查看有沒有sam這個用戶。
[root@localhost mnt]# pwd
/mnt
[root@localhost mnt]# find /mnt/ -name abc
/mnt/test/abc
/mnt/abc
[root@localhost mnt]# find /mnt/ -path /mnt/test -prune -o -name abc -print  #查找abc,除過test下的abc
/mnt/abc
#以文件權限查找
[root@localhost ~]#find . -type f -perm 644 -print  #列出具備特定權限的文件
[root@localhost ~]#find . -group root -exec ls -l {} \;  #搜索屬於root組的文件
[root@localhost ~]# find / -perm 2644  #查找有644及s屬性
[root@localhost ~]# find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/umount
/bin/su
/bin/mount
/bin/ping
/bin/ping6
/sbin/unix_chkpwd
/sbin/pam_timestamp_check
[root@localhost ~]# ll /bin/umount 
-rwsr-xr-x. 1 root root 53472 Oct 15  2014 /bin/umount
[root@localhost ~]# find / -maxdepth 1 -perm /u=r
/
/lib64
/boot
/bin
/home
/usr
[root@localhost ~]# find . -user root  
#以文件大小查找
[root@localhost ~]# find . -type f -size +2k  #搜索文件大於2k的文件
#支持正則表達式查找
-regextype 指定所使用的正則表達式類型,可選的有emacs(默認),posix-awk,posix-basic,posix-egrep,posix-extended,喜歡grep -E,就用posix-egrep
用find查找目錄下以1-3位數字命名的文件
$find ./ -regextype posix-egrep -regex '.*/[0-9]{1,3}'
[root@localhost mnt]# find /etc -regex ".*\.\(txt\|sh\)"  #在etc目錄下查找txt及sh結尾的文件
/etc/kde/env/imsettings-kde.sh
/etc/pki/nssdb/pkcs11.txt
/etc/X11/xinit/xinitrc.d/50-xinput.sh
/etc/X11/xinit/xinitrc.d/localuser.sh
/etc/X11/xinit/xinitrc.d/00-start-message-bus.sh
/etc/bash_completion.d/gdbus-bash-completion.sh
#查找隱藏文件
[root@localhost ~]# find ~ -type f -name ".*"
說明:查找家目錄下的隱藏文件
-exec會把find查找到的結果一次性所有交給後面的命令來進行處理,有時候系統對可以傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘以後,就會出現 溢出錯誤。錯誤信息一般是「參數列太長」或「參數列溢出」。這就是xargs命令的用處所在 ,特別是與find命令一塊兒使用。有些時候匹配到的文件發起的一個進程,並不是將匹配到的文件所有做爲參數一次執行,會致使進程過多,系統性能降低的問題,所以效率不高。

xargs會把find查找到的結果逐一交給後面的命令進行來處理,而xargs命令每次只獲取一部分文件而不是所有,不像-exec選項那樣。這樣它能夠先處理最早獲取的一部分文件,而後是下一批,繼續下去,直到結束。
-ok和-exec的做用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每個命令以前,都會給出提示,讓用戶來肯定是否執行

9.3.xargs

功能:從標準輸入來執行命令

經常使用選項:

-a file   從指定文件讀取數據做爲標準輸入

-0      處理包含空格的文件名,print0

-d  delimiter  分隔符,默認是空格分隔顯示

-i      標準輸入的結果以{}代替

-I      標準輸入的結果以指定的名字代替

-t      顯示執行命令

-p      交互式提示是否執行命令

-n      最大命令行參數

--show-limits  查看系統命令行長度限制

示例:

上面find的例子已經不少了

介紹兩個經常使用的:

[root@localhost scripts]# cat number.txt   
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[root@localhost scripts]# cat number.txt |xargs -n1  #列變行,-n1就是最大1列打印
1
2
3
4
5
6
7
8
[root@localhost scripts]# cat number.txt |xargs -n2  #這裏就是最大兩列打印
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
[root@localhost scripts]# cat number.txt |xargs -n3   #3列顯示
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20


9.4.練習

一、找出根目錄下的全部塊設備文件,而且將標準輸出及標準錯誤重定向到/tmp/find.test文件中

[root@localhost mnt]# find / -type b > /tmp/find.test 2>&1

二、找出/root目錄下小於2M的文件並長列出,同時將其追加到/tmp/find.test文件中

1M:等於1M

+1M:大於1M

-1M:小於1M


[root@localhost mnt]# find /root/ -size -2M -exec ls -l {} \; >>/tmp/find.test

三、在/home/test目錄中建立10個文件,而且修改file1~file5的時間爲系統時間的5天前,file6的時間爲5月28,file7的爲5月29,file8的爲5月30

[root@localhost home]# pwd
/home
[root@localhost home]# touch file{1..10}
[root@localhost home]# ls
file1   file2  file4  file6  file8  old01  old03  old05  old07  old09  yyl
file10  file3  file5  file7  file9  old02  old04  old06  old08  old10  yyl01
[root@localhost home]# touch -d $(date +%Y%m%d) --date="6 days ago" file{1..5}
[root@localhost home]# ll
total 48
-rw-r--r-- 1 root  root     0 May 25 15:37 file1
-rw-r--r-- 1 root  root     0 May 31 15:35 file10
-rw-r--r-- 1 root  root     0 May 25 15:37 file2
-rw-r--r-- 1 root  root     0 May 25 15:37 file3
-rw-r--r-- 1 root  root     0 May 25 15:37 file4
-rw-r--r-- 1 root  root     0 May 25 15:37 file5
-rw-r--r-- 1 root  root     0 May 31 15:35 file6
-rw-r--r-- 1 root  root     0 May 31 15:35 file7
-rw-r--r-- 1 root  root     0 May 31 15:35 file8
-rw-r--r-- 1 root  root     0 May 31 15:35 file9
[root@localhost home]# touch -d 20170528 file6
[root@localhost home]# touch -d 20170529 file7
[root@localhost home]# touch -d 20170530 file8
[root@localhost home]# ll
total 48
-rw-r--r-- 1 root  root     0 May 25 15:37 file1
-rw-r--r-- 1 root  root     0 May 31 15:35 file10
-rw-r--r-- 1 root  root     0 May 25 15:37 file2
-rw-r--r-- 1 root  root     0 May 25 15:37 file3
-rw-r--r-- 1 root  root     0 May 25 15:37 file4
-rw-r--r-- 1 root  root     0 May 25 15:37 file5
-rw-r--r-- 1 root  root     0 May 28 00:00 file6
-rw-r--r-- 1 root  root     0 May 29 00:00 file7
-rw-r--r-- 1 root  root     0 May 30 00:00 file8
-rw-r--r-- 1 root  root     0 May 31 15:35 file9

要求:

1)找出5天之前的文件並將其拷貝到/backup目錄

[root@localhost home]# find ./ -mtime +5 -ok cp {} /backup/ \;

2)找出5天之內的文件並將其拷貝到192.168.5.1上的/home/test目錄裏,而且重命名成本身的名字。redhat用戶密碼爲123

[root@localhost home]# find /home/redhat -mtime -5 -exec scp {} redhat@192.168.5.1:/home/test/zhangsan \;
相關文章
相關標籤/搜索