把 find 學會最快的方式就是動手做題。以下六題涵蓋所有者、群組、時間、類型與大小等常見條件組合。
[root@www ~]# find /var \( -user root -a -group mail \) -ls 132134 4 drwxrwxr-x 2 root mail 4096 9月 12 13:55 /var/spool/mail
[root@www ~]# find /usr ! \( -user root -o -user bin -o -user hadoop \) -exec ls -l {} \;
-rwsr-xr-x. 1 abrt abrt 10296 7月 25 00:08 /usr/libexec/abrt-action-install-debuginfo-to-abrt-cache
[root@www ~]# find /etc ! \( -user root -o -user hadoop \) -mtime -7 -exec ls -l {} \;
-rw-r--r--. 1 bash root 0 9月 12 13:46 /etc/find_lab/find.txt
[root@www ~]# find / -atime -7 -a \( -nouser -o -nogroup \) -ls
131305 0 -rw-rw---- 1 507 mail 0 9月 12 13:55 /var/spool/mail/rainman
430089 4 -rwxr--r-- 1 507 abc 21 9月 11 13:33 /lab/readme.txt
39 4 drwx------ 4 507 507 4096 9月 12 13:55 /home/rainman
47 4 -rw-r--r-- 1 507 507 500 11月 27 2014 /home/rainman/.emacs
過程中若出現 /proc/... 底下「沒有那個檔案或目錄」的訊息,那是因為行程已結束、目錄隨之消失,屬正常現象,可忽略。
[root@www ~]# find /etc -type f -size +20k -exec ls -l {} \; | head -n 10
-rw-r--r--. 1 root root 27223 11月 12 2010 /etc/sound/events/gnome-2.soundlist
-rw-r--r--. 1 root root 34419 3月 4 2015 /etc/httpd/conf/httpd.conf
-rw-r--r--. 1 root root 21214 6月 16 2014 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 45016 11月 11 2010 /etc/gconf/schemas/compiz-decoration.schemas
這幾題把 -a(且)、-o(或)、!(非)與括號分組的邏輯都練到了,而 -exec 搭配 {} \; 則是把結果丟給後續指令處理的通用寫法。