1. 程式人生 > >find命令

find命令

name 使用 blog spa hosts 文件的 localhost logo key

--本文原創

命令格式

find /path/to/somewhere/ - options [- print -exec -ok ...]

1 /path/to/somewhere/ 是find命令所查找的路徑,例如"."表示當前路徑,可使用絕對或者相對路徑

2 -print 表示將匹配到的文件輸出到標準輸出中,默認是當前屏幕,可重定向輸出流

3 -exec 表示對find命令查找匹配到的文件執行exec給出的shell 命令,相應的命令形式為 ‘command‘ {} \; 註意{}和\;之間的空格

4 -ok 作用和-exec相同,只不過是一種更安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,會給出提示,讓用戶來確定是否執行

查找模式

1 -name 按文件名查找

2 -perm 按照文件權限查找

3 -prune 不在當前指定的目錄查找,如果同時使用-dept那麽-prune將被忽略

4 -user 按文件屬主查找

5 -group 按文件屬組查找

6 -mtime -n +n 按文件更改時間查找,-n 表示從此刻算起,文件的更改時間是在n天以內,+n表示文件的更改時間在n天之前

7 -ctime 文件改變時間change time 選項-n,+n意義如上

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

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

10 -newer file1 file2 查找更改時間比file1新但比文件file2舊的文件,即file1<查找目標<file2

11 -type 按文件類型查找 b , d , c , p , l , f, s

12 -size n 表示查找大小為n的文件 帶c表示大小以字節計算

13 -depth 表示查找文件時,首先查找當前目錄中的文件,然後在其子目錄中查找

14 -fstype 表示查找位於某一類文件系統中的文件,這些文件系統通常可以在配置文件/etc/fstab中找到

15 -mount 表示查找文件時不跨越文件系統mount點

16 -follow 表示如果find命令遇到符號鏈接文件,就跟蹤至鏈接所指向的文件

17 -cpio 表示對匹配的文件使用cpio命令,將這些文件備份到磁帶設備中

一般用-exec或者-ok來執行shell 命令,任何形式的命令都可以在-exec中使用

實例

列出當前目錄所有普通文件

[[email protected] ~]# find . -type f -exec ls -l {} \;
-rw-r--r--. 1 root root 18 12月 29 2013 ./.bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 ./.bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 ./.cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 ./.tcshrc
-rw-------. 1 root root 1382 11月 13 20:14 ./anaconda-ks.cfg
-rw-------. 1 root root 2 11月 13 12:15 ./.cache/dconf/user
-rw-------. 1 root root 11 5月  13 13:03 ./.cache/abrt/lastnotification
-rw-r--r--. 1 root root 463 11月 13 12:15 ./.dbus/session-bus/9ef6301b72a14a6e8a38e7e00cf69136-9
-rw-r--r--. 1 root root 1433 11月 13 12:15 ./initial-setup-ks.cfg
-rw-------. 1 root root 3212 5月  13 12:40 ./.bash_history
-rw-------. 1 root root 66 11月 13 14:24 ./.xauth3HNIPU
-rw-r--r--. 1 root root 397 5月  10 16:58 ./.ssh/known_hosts
-rw-------. 1 root root 408 5月  10 17:02 ./.ssh/authorized_keys
-rw-r--r--. 1 root root 408 5月  10 17:19 ./.ssh/my_rsa.pub
-rw-------. 1 root root 1675 5月  10 17:19 ./.ssh/id_rsa
-rw-------. 1 root root 4123 5月  13 12:16 ./.viminfo
-rw-r--r--. 1 root root 16 5月  13 12:46 ./test

查找passwd文件中某個用戶是否存在

[[email protected] ~]# find /etc/ -name passwd -exec grep gandefeng {} \;
gandefeng:x:1002:1002::/home/gandefeng:/bin/bash

查找當前用戶家目錄中所有的文件,包含隱藏文件

[[email protected] ~]$ find ~ -print
/home/gandefeng
/home/gandefeng/.mozilla
/home/gandefeng/.mozilla/extensions
/home/gandefeng/.mozilla/plugins
/home/gandefeng/.bash_logout
/home/gandefeng/.bash_profile
/home/gandefeng/.bashrc
/home/gandefeng/.cache
/home/gandefeng/.cache/abrt
/home/gandefeng/.cache/abrt/lastnotification
/home/gandefeng/.config
/home/gandefeng/.config/abrt
/home/gandefeng/test
/home/gandefeng/.viminfo
/home/gandefeng/.bash_history

xargs對find的配合使用

在find和-exec 處理匹配文件時,find命令將所匹配到的文件一起傳遞給exec執行,但是有的系統傳遞給exec的命令長度是有限制的,find命令運行幾分鐘後會溢出錯誤,通常是參數太長或參數列溢出,所以,find把匹配到的文件傳遞給xargs 而xargs每次只獲取一部分而不是全部,分批次處理,有的系統在使用-exec會為每一個匹配到的文件發起一個相應的進程,並非將所匹配到的文件全部作為參數一次執行,這樣會出現過多的進程,系統性能下降,而xargs只有一個進程,而且還可以根據參數來調整xargs接受的量

例:查找當前目錄中每一個普通文件,然後測試屬於的文件類型

[[email protected] ~]$ find . -type f |xargs file
./.bash_logout:                 ASCII text
./.bash_profile:                ASCII text
./.bashrc:                      ASCII text
./.cache/abrt/lastnotification: ASCII text
./test:                         ASCII text
./.viminfo:                     UTF-8 Unicode text
./.bash_history:                ASCII text


to be continued..

find命令