歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(22)find命令_命令詳解

每天一個Linux命令(22)find命令_命令詳解

日期:2017/3/3 12:34:11   编辑:Linux技術

[b] find命令的一些常用參數的常用實例和用時的注意事項。[/b][b] 實例:[/b]

[b] (1)-name參數:[/b] 1)[sunjimeng@localhost home]$ find ~ -name "less*" -print       在指定目錄和指定目錄的子目錄中查找與文件名匹配的文件

[sunjimeng@localhost home]$ find ~ -name "less*" -print
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
~是一個代位符,表明的是個人目錄的地址,因為每個用戶都有自己的個人目錄地址,所以用 ~ 作為統一替代這個根據用戶不同而不同但有規可循的地址,來保證某些情況下的兼容性問題。

如果以root登錄,則~代表/home/root,若以username登錄,則~代表/home/username,例如:我的目錄就是/home/sunjimeng。linux 中的$PATH $HOME 是什麼意思?

在linux及unix的sh中,以$開頭的字符串表示的是sh中定義的變量,這些變量可以是系統自動增加的,也可以是用戶自己定義的。 $PATH表示的是系統的命令搜索路徑,和windows的%path%是一樣的;$HOME則表示是用戶的主目錄,也就是用戶登錄後工作目錄。 波浪號~代表了你的$HOME目錄。

2)[sunjimeng@localhost Document]$ find . -name "[A-Z]*[1-9]"        查找當前目錄下以大寫字母開頭,數字結尾的文件。(注意[]符號的用法)

[sunjimeng@localhost Document]$ ll
總用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
[sunjimeng@localhost Document]$ find . -name "[A-Z]*[1-9]"
./B.text3
./C.text6
[sunjimeng@localhost Document]$ find . -name "[A-Z]*"
./B.text3
./C.text6
./D.text

3)[sunjimeng@localhost Document]$ find / -name "*"     查找Linux文件系統中的所有文件,讓系統高負荷運行

[sunjimeng@localhost Document]$ find / -name "*"

由於Linux的find命令是通過後台執行操作,遍歷整個磁盤文件進行文件的查找,不僅包括名稱的查找,還包括其他的例如權限,文件的內容的字符串匹配的查找,因此十分耗費資源,因此這裡會讓系統高負荷運行。而前幾個命令,都是高效查詢命令,例如which,whereis等,因為他們不是直接深入磁盤中查找,而是通過數據庫的鍵值對應進行快速查找。[b] (2)-perm參數[/b]

按照文件權限模式用-perm選項,按文件權限模式來查找文件的話。最好使用八進制的權限表示法。 1)[root@localhost home]# find . -perm 755 |more -5        按權限在目錄中查找文件,並執行分頁顯示

[root@localhost home]# find . -perm 755 |more -5
.
./sunjimeng/.mozilla
./sunjimeng/.mozilla/extensions
./sunjimeng/.mozilla/plugins
./sunjimeng/.config
--more--                         //按q鍵退出more命令 回車下一行,空格下一頁
2)[root@localhost home]# find / -perm -mode |more -5       根據權限查看文件,當權限值前有-號時

[root@localhost home]# find / -perm 555 |more -5
/
/boot
/proc
/proc/asound
/proc/asound/card0
--more--
[root@localhost home]# find / -perm -555 |more -5
/
/boot
/boot/grub
/boot/grub2
/boot/grub2/themes
--more--
[root@localhost home]# find / -perm -005 |more -5
/
/boot
/boot/grub
/boot/grub2
/boot/grub2/themes
--more--
find -perm,根據文件的權限來查找文件,有三種形式:

1.find -perm mode  表示嚴格匹配,也就是你的文件權限位轉換成對應的十進制數字與mode一模一樣,那麼匹配成功,需要注意的是如果mode給的數字不足3位,那麼前面自動添0(嚴格的說是不足4位)

2.find -perm -mode 表示mode中轉換成二進制的1在文件權限位裡面必須匹配,比如mode=644那麼轉換成二進制為110 100 100,而被查找的文件的權限位也可以被轉換成一個二進制數,兩者在位上為1的部分必須完全匹配,而0則不管。例如被查找的文件的權限為轉換成二進制數是111 111 111那麼這個比如被匹配,而假如是100 100 100那麼則不會匹配。所以這個'-'的作用歸結起來就是匹配比mode權限更充足的文件

3.find -perm +mode 與 -mode的區別是+mode只需其中的任意一個1的部分被匹配,-mode是所有1的部分都必須被匹配,同樣+mode也不管0位。

在linux中文件或目錄有三者權限r,w,x,代表的含義分別是讀、寫、可執行。而一個文件或目錄的屬性中又包括所屬用戶u、所屬組g、其他o三個部分的屬性,分別表示所屬用戶、所屬組、其他用戶對這個文件所擁有的權限。看起來大概是這個樣子:所屬用戶 所屬組 其他

rwx rwx rwx

用戶在其擁有權限的位上設置1,沒有權限的位設置0。如果將每個部分的這些權限位看成二進制數,每個部分可以用3位二進制數表示,最大值為7(2^3-1),表示可讀、可寫、可執行。[b] (3) -prune參數:[/b]

如果在查找文件時希望忽略某個目錄,因為你知道那個目錄中沒有你所要查找的文件,那麼可以使用-prune選項來指出需要忽略的目錄。在使用-prune選項時要當心,因為如果你同時使用了-depth選項,那麼-prune選項就會被find命令忽略。 1)[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o -print     查詢文件時忽略特定目錄

[root@localhost sunjimeng]# find Document
Document
Document/newDir
Document/newDir/mvt1.txt
Document/newDir/mvt2.txt
Document/newDir/mvt3.txt
Document/t1.txt
Document/t2.txt
Document/all.txt
Document/B.text3
Document/C.text6
Document/D.text
[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o -print
Document
Document/t1.txt
Document/t2.txt
Document/all.txt
Document/B.text3
Document/C.text6
Document/D.text
2)[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune        find命令後默認只能跟一個參數命令,如果還需要執行其他的命令,需要-o命令連接符

[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune        //-prune不加-print命令的話,輸出的是要忽略的文件夾及其路徑
Document/newDir
[root@localhost sunjimeng]# find Document -path "Document/newDir" -prune -o  -exec ls -l {} \; -o用於設置執行的兩個連續的命令,這裡執行-exec命令,上一個命令執行的是-print命令,也可以換成其他的
總用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
-rw-r--r--. 1 root root 42 5月  18 02:53 Document/t1.txt
-rw-r--r--. 1 root root 43 5月  18 02:54 Document/t2.txt
-rw-r--r--. 1 root root 85 5月  18 02:58 Document/all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 Document/B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 Document/C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:28 Document/D.text
3)[root@localhost Documents]# find . \( -path "./Dir" -o -path "./findDir" \) -prune -o -exec ls -l {} \;      忽略多個文件夾

[root@localhost Documents]# ll
總用量 28
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir    //是文件夾
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
[root@localhost Documents]# mkdir Dir              //新建一個文件夾
[root@localhost Documents]# mv {head_text,less1,less2} Dir      //將幾個文件移到裡面
[root@localhost Documents]# ll
總用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
[root@localhost Documents]# find .  -exec ls -l {} \;    //執行一次查詢當前Documents文件夾下的所有目錄及文件操作
總用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
總用量 0
--w-------. 1 root root 0 5月  17 04:18 p1.pdf
--w-------. 1 root root 0 5月  17 04:18 p2.pdf
--w-------. 1 root root 0 5月  17 03:50 t1.txt
--w-------. 1 root root 0 5月  17 04:02 T1.txt
-rw-r--r--. 1 root root 0 5月  19 04:58 t2.txt
--w-------. 1 root root 0 5月  17 04:02 T2.txt
--w-------. 1 root root 0 5月  17 03:50 ./findDir/t1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T2.txt
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p1.pdf
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root 0 5月  19 04:58 ./findDir/t2.txt
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt
總用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
--w-------. 1 root root 664 5月   9 07:59 ./Dir/head_text
--w-------. 1 root root 45 5月   9 08:15 ./Dir/less1
--w-------. 1 root root 57 5月   9 08:16 ./Dir/less2
[root@localhost Documents]# find . \( -path "./Dir" -o -path "./findDir" \) -prune -o -exec ls -l {} \;    //忽略兩個文件夾,執行操作
總用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt
[root@localhost Documents]# find . \( -path "./Dir" -o -path "findDir" \) -prune -o -exec ls -l {} \;不加./是無法進行匹配的,-path “findDir”無意義(根據結果可知)
總用量 16
-rw-r--r--. 1 root root  27 5月  19 04:21 core.log
drwxr-xr-x. 2 root root  46 5月  19 23:29 Dir
-rw-r--r--. 1 root root   0 5月  19 04:16 find
drwxr-xr-x. 2 root root  84 5月  19 04:57 findDir
--w-------. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root  85 5月  19 04:25 t3.txt
--w-------. 1 root root 259 5月  12 21:53 tail_text
--w-------. 1 root root 216 5月  12 22:24 tempory
--w-------. 1 root root   0 5月  15 18:34 uText
--w-------. 1 root root 259 5月  12 21:53 ./tail_text
--w-------. 1 root root 216 5月  12 22:24 ./tempory
--w-------. 1 root root 0 5月  15 18:21 ./newlocate
--w-------. 1 root root 0 5月  15 18:34 ./uText
總用量 0
--w-------. 1 root root 0 5月  17 04:18 p1.pdf
--w-------. 1 root root 0 5月  17 04:18 p2.pdf
--w-------. 1 root root 0 5月  17 03:50 t1.txt
--w-------. 1 root root 0 5月  17 04:02 T1.txt
-rw-r--r--. 1 root root 0 5月  19 04:58 t2.txt
--w-------. 1 root root 0 5月  17 04:02 T2.txt
--w-------. 1 root root 0 5月  17 03:50 ./findDir/t1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T1.txt
--w-------. 1 root root 0 5月  17 04:02 ./findDir/T2.txt
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p1.pdf
--w-------. 1 root root 0 5月  17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root 0 5月  19 04:58 ./findDir/t2.txt
-rw-r--r--. 1 root root 0 5月  19 04:16 ./find
-rw-r--r--. 1 root root 27 5月  19 04:21 ./core.log
-rw-r--r--. 1 root root 85 5月  19 04:25 ./t3.txt
[b] (4)-user參數[/b]

1)[root@localhost Document]# find . -user sunjimeng -exec ls -l {} \;        查找屬於特定用戶的文件及文件夾

[root@localhost Document]# find . -user sunjimeng -exec ls -l {} \;
總用量 12
-rw-r--r--. 1 root      root      85 5月  18 02:58 all.txt
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:27 C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng  0 5月  19 22:28 D.text
drwxr-xr-x. 2 root      root      51 5月  18 02:47 newDir
-rw-r--r--. 1 root      root      42 5月  18 02:53 t1.txt
-rw-r--r--. 1 root      root      43 5月  18 02:54 t2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 ./B.text3
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:27 ./C.text6
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月  19 22:28 ./D.text
[root@localhost Document]# find . -user root -exec ls -l {} \;
總用量 0
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt1.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt2.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 mvt3.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt1.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt2.txt
-rw-r--r--. 1 root root 0 5月  18 02:46 ./newDir/mvt3.txt
-rw-r--r--. 1 root root 42 5月  18 02:53 ./t1.txt
-rw-r--r--. 1 root root 43 5月  18 02:54 ./t2.txt
-rw-r--r--. 1 root root 85 5月  18 02:58 ./all.txt

2)[root@localhost /]# find . -nouser -exec ls -l {} \;      查找不屬於任何用戶的文件

[root@localhost /]# find . -nouser -exec ls -l {} \;                  //這裡沒找著
find: ‘./proc/37913/task/37913/fd/6’: 沒有那個文件或目錄
find: ‘./proc/37913/task/37913/fdinfo/6’: 沒有那個文件或目錄
find: ‘./proc/37913/fd/6’: 沒有那個文件或目錄
find: ‘./proc/37913/fdinfo/6’: 沒有那個文件或目錄
find: ‘./run/user/1000/gvfs’: 權限不夠

[b]另外,還有其他的參數,例如按文件的大小-size,按查詢路徑的深度-depth,按文件的新舊程度-newer,按更改時間或訪問時間-mtime,按類型-type等等。[/b][b] 在此並不一一舉例。況且前兩篇也有一些示例。[/b]

Copyright © Linux教程網 All Rights Reserved