find命令中-path 參數的作用(意思)解釋
-path pattern
File name matches shell pattern pattern. The metacharacters do not treat /' or .' specially; so, for example,
find . -path './sr*sc'
will print an entry for a directory called './src/misc' (if one exists). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to skip the directory `src/emacs' and all files and directories under it, and print the names of the other files found, do something like this:
find . -path './src/emacs' -prune -o -print
意思就是說,find命令中用了-path 參數,則檔案名稱是匹配shell萬用字元模式。這時候,元字元不能識別 '/ ' 和 ' . ' 。舉個例子
假設/root 目錄下有四個檔案,其絕對路徑分別為:
/root/src/misc
/root/src/test/misc
/root/src1misc
/root/test/src/misc
這時候輸入下面的命令:
[root@dabu.info ~]# cd /root
[root@dabu.info ~]#find . -path './sr*sc'
./src1misc
./src/misc
./src/test/misc
命令運行輸出的結果不是檔案名稱,而是檔案的路徑
可見,檔案夾符號/ 在此時也可以用 * 萬用字元匹配,而且 * 是可以匹配n多個字元。上面的命令列的意思是是在目前的目錄下及子目錄遞迴尋找檔案路徑 匹配的檔案,所以輸出的是檔案的路徑。
因為是'./sr*sc' ,所以 /root/test/src/misc這個檔案不匹配,因為 ./ 代表目前的目錄 (即/root) ,而 /root/test/src/misc不符合 ./sr的格式。
但是稍微改一下,如下:
[root@dabu.info ~]#find . -path '*/sr*sc*'
./src1misc
./src/misc
./src/test/misc
./test/src/misc
解釋:因為" * " 可以匹配 "." 和 " / ", 我們將上面的'*/sr*sc*' 改為 '*sr*sc*' 。結果也是一樣的。
注意:也可以直接用絕對路徑
[root@dabu.info ~]#find /root -path '*/sr*sc*'
而
[root@dabu.info ~]#find /root -path '/root/sr*sc*'
/root/src1misc
/root/src/misc
/root/src/test/misc
此時, /root/test/src/misc 是不符的'/root/sr*sc*'的格式的。
所以Linux / Unix:遞迴尋找並列出所有(全部)隱藏檔案的方法可以如下:
[root@dabu.info ~]#find /要尋找的文/件/夾/ -path '*/.*' -print
教程完!
-path更多的是配合 -prune -o 來在操作時排除指定的檔案。這裡就不說了,大家自行百度去吧。