Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

find 命令實戰練習:六道經典題目與解答

把 find 學會最快的方式就是動手做題。以下六題涵蓋所有者、群組、時間、類型與大小等常見條件組合。

1. 找出 /var 下所有者為 root、群組為 mail 的檔案

[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

2. 找出 /usr 下不屬於 root、bin 或 hadoop 的檔案

[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

3. 找出 /etc 下一週內修改過、且所有者不是 root 或 hadoop 的檔案

[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

4. 找出全系統沒有所有者或群組、且一週內被存取代過的檔案

[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/... 底下「沒有那個檔案或目錄」的訊息,那是因為行程已結束、目錄隨之消失,屬正常現象,可忽略。

5. 找出 /etc 下大於 20K 的一般檔案

[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 搭配 {} \; 則是把結果丟給後續指令處理的通用寫法。

Copyright © Linux教程網 All Rights Reserved