1、locate
locate指令和find找尋檔案的功能類似,但locate是透過update程序將硬盤中的所有檔案和目錄資料先建立一個索引數據庫,在 執行loacte時直接找該索引,查詢速度會較快,索引數據庫一般是由操作系統管理,但也可以直接下達update強迫系統立即修改索引數據庫。
命令參數:
-e 將排除在尋找的范圍之外。
-1 如果 是 1.則啟動安全模式。在安全模式下,使用者不會看到權限無法看到 的檔案。這會始速度減慢,因為 locate 必須至實際的檔案系統中取得檔案的 權限資料。
-f 將特定的檔案系統排除在外,例如我們沒有到理要把 proc 檔案系統中的檔案 放在資料庫中。
-q 安靜模式,不會顯示任何錯誤訊息。
-n 至多顯示 n個輸出。
-r 使用正規運算式 做尋找的條件。
-o 指定資料庫存的名稱。
-d 指定資料庫的路徑
-h 顯示輔助訊息
-V 顯示程式的版本訊息
使用實例:
實例1:查找和pwd相關的所有文件
命令:
locate pwd
輸出:
peida-VirtualBox ~ # locate pwd
/bin/pwd
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc
實例2: 搜索etc目錄下所有以sh開頭的文件
命令:
locate /etc/sh
輸出:
peida-VirtualBox ~ # locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
peida-VirtualBox ~ #
2、find
Linux下find命令在目錄結構中搜索文件,並執行指定的操作。Linux下find命令提供了相當多的查找條件,功能很強大。由於find具有強大的功能,所以它的選項也很多,其中大部分選項都值得我們花時間來了解一下。即使系統中含有網絡文件系統( NFS),find命令在該文件系統中同樣有效,只你具有相應的權限。 在運行一個非常消耗資源的find命令時,很多人都傾向於把它放在后台執行,因為遍歷一個大的文件系統可能會花費很長的時間(這里是指30G字節以上的文件系統)。
1.命令格式:
find pathname -options [-print -exec -ok ...]
2.命令功能:
用於在文件樹中查找文件,並作出相應的處理
3.命令參數:
pathname: find命令所查找的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄。
-print: find命令將匹配的文件輸出到標准輸出。
-exec: find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式為'command' { } \;,注意{ }和\;之間的空格。
-ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓用戶來確定是否執行。
4.命令選項:
-name 按照文件名查找文件。
-perm 按照文件權限來查找文件。
-prune 使用這一選項可以使find命令不在當前指定的目錄中查找,如果同時使用-depth選項,那么-prune將被find命令忽略。
-user 按照文件屬主來查找文件。
-group 按照文件所屬的組來查找文件。
-mtime -n +n 按照文件的更改時間來查找文件, - n表示文件更改時間距現在n天以內,+ n表示文件更改時間距現在n天以前。find命令還有-atime和-ctime 選項,但它們都和-m time選項。
-nogroup 查找無有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。
-nouser 查找無有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改時間比文件file1新但比文件file2舊的文件。
-type 查找某一類型的文件,諸如:
b - 塊設備文件。
d - 目錄。
c - 字符設備文件。
p - 管道文件。
l - 符號鏈接文件。
f - 普通文件。
-size n:[c] 查找文件長度為n塊的文件,帶有c時表示文件長度以字節計。-depth:在查找文件時,首先查找當前目錄中的文件,然后再在其子目錄中查找。
-fstype:查找位於某一類型文件系統中的文件,這些文件系統類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。
-mount:在查找文件時不跨越文件系統mount點。
-follow:如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。
-cpio:對匹配的文件使用cpio命令,將這些文件備份到磁帶設備中。
另外,下面三個的區別:
-amin n 查找系統中最后N分鍾訪問的文件
-atime n 查找系統中最后n*24小時訪問的文件
-cmin n 查找系統中最后N分鍾被改變文件狀態的文件
-ctime n 查找系統中最后n*24小時被改變文件狀態的文件
-mmin n 查找系統中最后N分鍾被改變文件數據的文件
-mtime n 查找系統中最后n*24小時被改變文件數據的文件
5.使用實例:
實例1:查找指定時間內修改過的文件
命令:
find -atime -2
輸出:
[root@peidachang ~]# find -atime -2
.
./logs/monitor
./.bashrc
./.bash_profile
./.bash_history
說明:
超找48小時內修改過的文件
實例2:根據關鍵字查找
命令:
find . -name "*.log"
輸出:
[root@localhost test]# find . -name "*.log"
./log_link.log
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log
說明:
在當前目錄查找 以.log結尾的文件。 ". "代表當前目錄
實例3:按照目錄或文件的權限來查找文件
命令:
find /opt/soft/test/ -perm 777
輸出:
[root@localhost test]# find /opt/soft/test/ -perm 777
/opt/soft/test/log_link.log
/opt/soft/test/test4
/opt/soft/test/test5/test3
/opt/soft/test/test3
說明:
查找/opt/soft/test/目錄下 權限為 777的文件
實例4:按類型查找
命令:
find . -type f -name "*.log"
輸出:
[root@localhost test]# find . -type f -name "*.log"
./log2014.log
./test4/log3-2.log
./test4/log3-3.log
./test4/log3-1.log
./log2013.log
./log2012.log
./log.log
./test5/log5-2.log
./test5/log5-3.log
./test5/log.log
./test5/log5-1.log
./test5/test3/log3-2.log
./test5/test3/log3-3.log
./test5/test3/log3-1.log
./test3/log3-2.log
./test3/log3-3.log
./test3/log3-1.log
[root@localhost test]#
說明:
查找當前目錄,以.log結尾的普通文件
實例5:查找當前所有目錄並排序
命令:
find . -type d | sort
輸出:
[root@localhost test]# find . -type d | sort
.
./scf
./scf/bin
./scf/doc
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/info
./scf/service/deploy/product
./test3
./test4
./test5
./test5/test3
[root@localhost test]#
實例6:按大小查找文件
命令:
find . -size +1000c -print
輸出:
[root@localhost test]# find . -size +1000c -print
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./log2012.log
./test5
./test5/test3
./test3
[root@localhost test]#
說明:
查找當前目錄大於1K的文件
find之exec
find是我們很常用的一個Linux命令,但是我們一般查找出來的並不僅僅是看看而已,還會有進一步的操作,這個時候exec的作用就顯現出來了。
exec解釋:
-exec 參數后面跟的是command命令,它的終止是以;為結束標志的,所以這句命令后面的分號是不可缺少的,考慮到各個系統中分號會有不同的意義,所以前面加反斜杠。
{} 花括號代表前面find查找出來的文件名。
使用find時,只要把想要的操作寫在一個文件里,就可以用exec來配合find查找,很方便的。在有些操作系統中只允許-exec選項執行諸如l s或ls -l這樣的命令。大多數用戶使用這一選項是為了查找舊文件並刪除它們。建議在真正執行rm命令刪除文件之前,最好先用ls命令看一下,確認它們是所要刪除的文件。 exec選項后面跟隨着所要執行的命令或腳本,然后是一對兒{ },一個空格和一個\,最后是一個分號。為了使用exec選項,必須要同時使用print選項。如果驗證一下find命令,會發現該命令只輸出從當前路徑起的相對路徑及文件名。
實例1:ls -l命令放在find命令的-exec選項中
命令:
find . -type f -exec ls -l {} \;
輸出:
[root@localhost test]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#
說明:
上面的例子中,find命令匹配到了當前目錄下的所有普通文件,並在-exec選項中使用ls -l命令將它們列出。
實例2:在目錄中查找更改時間在n日以前的文件並刪除它們
命令:
find . -type f -mtime +14 -exec rm {} \;
輸出:
[root@localhost test]# ll
總計 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
在shell中用任何方式刪除文件之前,應當先查看相應的文件,一定要小心!當使用諸如mv或rm命令時,可以使用-exec選項的安全模式。它將在對每個匹配到的文件進行操作之前提示你。
實例3:在目錄中查找更改時間在n日以前的文件並刪除它們,在刪除之前先給出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} \;
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
在上面的例子中, find命令在當前目錄中查找所有文件名以.log結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除之前先給出提示。 按y鍵刪除文件,按n鍵不刪除。
實例4:-exec中使用grep命令
命令:
find /etc -name "passwd*" -exec grep "root" {} \;
輸出:
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
說明:
任何形式的命令都可以在-exec選項中使用。 在上面的例子中我們使用grep命令。find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執行grep命令看看在這些文件中是否存在一個root用戶。
實例5:查找文件移動到指定目錄
命令:
find . -name "*.log" -exec mv {} .. \;
輸出:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
實例6:用exec選項執行cp命令
命令:
find . -name "*.log" -exec cp {} test3 \;
輸出:
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;
cp: “./test3/log2014.log” 及 “test3/log2014.log” 為同一文件
cp: “./test3/log2013.log” 及 “test3/log2013.log” 為同一文件
cp: “./test3/log2012.log” 及 “test3/log2012.log” 為同一文件
[root@localhost test]# cd test3
[root@localhost test3]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3]#
在使用 find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鍾之后,就會出現溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。
find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,並如此繼續下去。
在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高; 而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
使用實例:
實例1: 查找系統中的每一個普通文件,然后使用xargs命令來測試它們分別屬於哪類文件
命令:
find . -type f -print | xargs file
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text
[root@localhost test]#
實例2:在整個系統中查找內存信息轉儲文件(core dump) ,然后把結果保存到/tmp/core.log 文件中
命令:
find / -name "core" -print | xargs echo "" >/tmp/core.log
輸出:
[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost tmp]# ll
總計 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log
drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766
drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815
drwx------ 2 root root 4096 11-03 07:11 vmware-root
實例3:在當前目錄下查找所有用戶具有讀、寫和執行權限的文件,並收回相應的寫權限
命令:
find . -perm -7 -print | xargs chmod o-w
輸出:
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -perm -7 -print | xargs chmod o-w
[root@localhost test]# ll
總計 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 19:32 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
說明:
執行命令后,文件夾scf、test3和test4的權限都發生改變
實例4:用grep命令在所有的普通文件中搜索hostname這個詞
命令:
find . -type f -print | xargs grep "hostname"
輸出:
[root@localhost test]# find . -type f -print | xargs grep "hostname"
./log2013.log:hostnamebaidu=baidu.com
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
實例5:用grep命令在當前目錄下的所有普通文件中搜索hostnames這個詞
命令:
find . -name \* -type f -print | xargs grep "hostnames"
輸出:
[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#
說明:
注意,在上面的例子中, \用來取消find命令中的*在shell中的特殊含義。
實例6:使用xargs執行mv
命令:
find . -name "*.log" | xargs -i mv {} test4
輸出:
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:54 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4]#
實例7:find后執行xargs提示xargs: argument line too long解決方法:
命令:
find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
輸出:
[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[root@pdtest4]#
說明:
-l1是一次處理一個;-t是處理之前打印出命令
實例8:使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[]
命令:
輸出:
[root@localhost test]# ll
總計 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost test4]# ll
總計 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test4]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
說明:
使用-i參數默認的前面輸出用{}代替,-I參數可以指定其他代替字符,如例子中的[]
實例9:xargs的-p參數的使用
命令:
find . -name "*.log" | xargs -p -i mv {} ..
輸出:
[root@localhost test3]# ll
總計 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:06 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test3
[root@localhost test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
總計 0[root@localhost test3]# cd ..
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#
說明:
-p參數會提示讓你確認是否執行后面的命令,y執行,n不執行。
find參數說明
find一些常用參數的一些常用實例和一些具體用法和注意事項。
1.使用name選項:
文件名選項是find命令最常用的選項,要么單獨使用該選項,要么和其他選項一起使用。 可以使用某種文件名模式來匹配文件,記住要用引號將文件名模式引起來。 不管當前路徑是什么,如果想要在自己的根目錄$HOME中查找文件名符合*.log的文件,使用~作為 'pathname'參數,波浪號~代表了你的$HOME目錄。
find ~ -name "*.log" -print
想要在當前目錄及子目錄中查找所有的‘ *.log‘文件,可以用:
find . -name "*.log" -print
想要的當前目錄及子目錄中查找文件名以一個大寫字母開頭的文件,可以用:
find . -name "[A-Z]*" -print
想要在/etc目錄中查找文件名以host開頭的文件,可以用:
find /etc -name "host*" -print
想要查找$HOME目錄中的文件,可以用:
find ~ -name "*" -print 或find . -print
要想讓系統高負荷運行,就從根目錄開始查找所有的文件。
find / -name "*" -print
如果想在當前目錄查找文件名以一個個小寫字母開頭,最后是4到9加上.log結束的文件:
命令:
find . -name "[a-z]*[4-9].log" -print
輸出:
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find . -name "[a-z]*[4-9].log" -print
./log2014.log
./log2015.log
./test4/log2014.log
[root@localhost test]#
2.用perm選項:
按照文件權限模式用-perm選項,按文件權限模式來查找文件的話。最好使用八進制的權限表示法。
如在當前目錄下查找文件權限位為755的文件,即文件屬主可以讀、寫、執行,其他用戶可以讀、執行的文件,可以用:
[root@localhost test]# find . -perm 755 -print
.
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
[root@localhost test]#
還有一種表達方法:在八進制數字前面要加一個橫杠-,表示都匹配,如-007就相當於777,-005相當於555,
命令:
find . -perm -005
輸出:
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find . -perm -005
.
./test4
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
./test3
[root@localhost test]#
3.忽略某個目錄:
如果在查找文件時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查找的文件,那么可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因為如果你同時使用了-depth選項,那么-prune選項就會被find命令忽略。如果希望在test目錄下查找文件,但不希望在test/test3目錄下查找,可以用:
命令:
find test -path "test/test3" -prune -o -print
輸出:
[root@localhost soft]# find test -path "test/test3" -prune -o -print
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
4.使用find查找文件的時候怎么避開某個文件目錄:
實例1:在test 目錄下查找不在test4子目錄之內的所有文件
命令:
find test -path "test/test4" -prune -o -print
輸出:
[root@localhost soft]# find test
test
test/log2014.log
test/log2015.log
test/test4
test/test4/log2014.log
test/test4/log2013.log
test/test4/log2012.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]# find test -path "test/test4" -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
test/test3
[root@localhost soft]#
說明:
find [-path ..] [expression]
在路徑列表的后面的是表達式
-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的簡寫表達式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果
-path "test" 為真,則求值 -prune , -prune 返回真,與邏輯表達式為真;否則不求值 -prune,與邏輯表達式為假。如果 -path "test" -a -prune 為假,則求值 -print ,-print返回真,或邏輯表達式為真;否則不求值 -print,或邏輯表達式為真。
這個表達式組合特例可以用偽碼寫為:
if -path "test" then
-prune
else
實例2:避開多個文件夾:
命令:
find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print
test
test/log2014.log
test/log2015.log
test/scf
test/scf/lib
test/scf/service
test/scf/service/deploy
test/scf/service/deploy/product
test/scf/service/deploy/info
test/scf/doc
test/scf/bin
test/log2013.log
test/log2012.log
[root@localhost soft]#
說明:
圓括號表示表達式的結合。 \ 表示引用,即指示 shell 不對后面的字符作特殊解釋,而留給 find 命令去解釋其意義。
實例3:查找某一確定文件,-name等選項加在-o 之后
命令:
find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
輸出:
[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print
test/log2014.log
test/log2015.log
test/log2013.log
test/log2012.log
[root@localhost soft]#
5.使用user和nouser選項:
按文件屬主查找文件:
實例1:在$HOME目錄中查找文件屬主為peida的文件
命令:
find ~ -user peida -print
實例2:在/etc目錄下查找文件屬主為peida的文件:
命令:
find /etc -user peida -print
說明:
實例3:為了查找屬主帳戶已經被刪除的文件,可以使用-nouser選項。在/home目錄下查找所有的這類文件
命令:
find /home -nouser -print
說明:
這樣就能夠找到那些屬主在/etc/passwd文件中沒有有效帳戶的文件。在使用-nouser選項時,不必給出用戶名; find命令能夠為你完成相應的工作。
6.使用group和nogroup選項:
就像user和nouser選項一樣,針對文件所屬於的用戶組, find命令也具有同樣的選項,為了在/apps目錄下查找屬於gem用戶組的文件,可以用:
find /apps -group gem -print
要查找沒有有效所屬用戶組的所有文件,可以使用nogroup選項。下面的find命令從文件系統的根目錄處查找這樣的文件:
find / -nogroup-print
7.按照更改時間或訪問時間等查找文件:
如果希望按照更改時間來查找文件,可以使用mtime,atime或ctime選項。如果系統突然沒有可用空間了,很有可能某一個文件的長度在此期間增長迅速,這時就可以用mtime選項來查找這樣的文件。
用減號-來限定更改時間在距今n日以內的文件,而用加號+來限定更改時間在距今n日以前的文件。
希望在系統根目錄下查找更改時間在5日以內的文件,可以用:
find / -mtime -5 -print
為了在/var/adm目錄下查找更改時間在3日以前的文件,可以用:
find /var/adm -mtime +3 -print
8.查找比某個文件新或舊的文件:
如果希望查找更改時間比某個文件新但比另一個文件舊的所有文件,可以使用-newer選項。
它的一般形式為:
newest_file_name ! oldest_file_name
其中,!是邏輯非符號。
實例1:查找更改時間比文件log2012.log新但比文件log2017.log舊的文件
命令:
find -newer log2012.log ! -newer log2017.log
輸出:
[root@localhost test]# ll
總計 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log
-rw-r--r-- 1 root root 0 11-16 14:43 log2017.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find -newer log2012.log ! -newer log2017.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
實例2:查找更改時間在比log2012.log文件新的文件
命令:
find . -newer log2012.log -print
輸出:
[root@localhost test]# find -newer log2012.log
.
./log2015.log
./log2017.log
./log2016.log
./test3
[root@localhost test]#
9.使用type選項:
實例1:在/etc目錄下查找所有的目錄
命令:
find /etc -type d -print
實例2:在當前目錄下查找除目錄以外的所有類型的文件
命令:
find . ! -type d -print
實例3:在/etc目錄下查找所有的符號鏈接文件
命令:
find /etc -type l -print
10.使用size選項:
可以按照文件長度來查找文件,這里所指的文件長度既可以用塊(block)來計量,也可以用字節來計量。以字節計量文件長度的表達形式為N c;以塊計量文件長度只用數字表示即可。
在按照文件長度查找文件時,一般使用這種以字節表示的文件長度,在查看文件系統的大小,因為這時使用塊來計量更容易轉換。
實例1:在當前目錄下查找文件長度大於1 M字節的文件
命令:
find . -size +1000000c -print
實例2:在/home/apache目錄下查找文件長度恰好為100字節的文件:
命令:
find /home/apache -size 100c -print
實例3:在當前目錄下查找長度超過10塊的文件(一塊等於512字節)
命令:
find . -size +10 -print
11.使用depth選項:
在使用find命令時,可能希望先匹配所有的文件,再在子目錄中查找。使用depth選項就可以使find命令這樣做。這樣做的一個原因就是,當在使用find命令向磁帶上備份文件系統時,希望首先備份所有的文件,其次再備份子目錄中的文件。
實例1:find命令從文件系統的根目錄開始,查找一個名為CON.FILE的文件。
命令:
find / -name "CON.FILE" -depth -print
說明:
它將首先匹配所有的文件然后再進入子目錄中查找
12.使用mount選項:
在當前的文件系統中查找文件(不進入其他文件系統),可以使用find命令的mount選項。
實例1:從當前目錄開始查找位於本文件系統中文件名以XC結尾的文件
命令:
find . -name "*.XC" -mount -print