歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux基礎知識 >> Linux中的find命令

Linux中的find命令

日期:2017/3/2 17:14:31   编辑:Linux基礎知識

Linux是一個“一切皆文件的系統”,Linux中標識文件不通過後綴,find命令若不指定查找目錄的情況下是針對整個系統進行查找。

一.命令格式

find pathname _options[-print -exec -ok....]

pathname:find命令所查找的根目錄。例如用.來表示當前目錄,/來表示根目錄。

例:

root@ubuntu:~# find /root
/root
/root/file
/root/Test2.txt
...

-print:find命令將所匹配的文件輸出到標准輸出。(系統默認)

-exec commend { } \; :find命令對所匹配的文件執行該參數所給出的shell命令

例:root@ubuntu:~# find /root -type f -exec ls -l {} \;

-rw-r--r-- 1 root root 1490 May 16 05:08 /root/file
-rw-r--r-- 1 root root 226 May 16 06:40 /root/cmd
-rw-r--r-- 1 root root 0 May 20 18:57 /root/Test2.txt

-ok commend { } \;:和-exec的作用相同,但執行每個命令之前都會提示,讓用戶確定是否執行,安全性強

二.-options:查找規則

1.-name:按文件名查找文件 //精確查找

-iname:按文件名查找文件(不區分大小寫)

例:root@ubuntu:~# find /root -name Test.txt
/root/Test.txt
root@ubuntu:~# find /root -iname test.txt
/root/Test.txt

2.-perm按照文件權限來查找文件

例:-rw-r--r-- 1 root root 0 May 20 18:57 Test2.txt

root@ubuntu:~# find /root -perm 644

/root/Test2.txt

-perm +權限(一類用戶匹配寫權限就可以)

-perm -權限(所有類別用戶滿足寫權限)

3.-user和-group:按照文件所屬主和所屬組來查找文件

例:root@ubuntu:~# find /root -user root
/root
/root/file
/root/cmd
/root/Test2.txt
...

4.-mtime -n +n:根據文件的更改時間來查找文件,-n表示文件更改時間到現在n天以內,+n表示文件更改時間到現在n天以前

例:-rw-r--r-- 1 root root 1490 May 16 05:08 file

root@ubuntu:~# find /root -mtime +2
/root/file

5.-nouser和-nogroup:該文件所屬主在/etc/passwd中不存在和該文件所屬的組在/ect、groups中不存在

6.-type 查找某種類型的文件,例如: ?
b - 塊設備文件。 ?
d - 目錄。 ?
c - 字符設備文件。 ?
p - 管道文件。 ?
l - 符號鏈接文件。 ?
f - 普通文件

例:

root@ubuntu:~# find /root -type f
/root/file
/root/cmd
/root/Test2.txt
...

7:-size n: [c] 查找文件長度為n塊的文件,帶有c時表示文件長度以字節計算
-size +n或-size -n:大於或小於n的文件

例:

root@ubuntu:~# find /root -size -226
/root
/root/file
/root/cmd
/root/Test2.txt

...

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

9:-fstype:查找位於某一類型文件系統中的文件,這些文件系統類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。 ?
10:-mount:在查找文件時不跨越文件系統mount點。 ?
11:-follow:如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件。
12:-cpio:對匹配的文件使用cpio命令,將這些文件備份到磁帶設備中。

Copyright © Linux教程網 All Rights Reserved