ת£ºzhyfly
Ç°ÑÔ£º¹ØÓÚfindÃüÁî
Ò»¡¢find ÃüÁî¸ñʽ
1¡¢findÃüÁîµÄÒ»°ãÐÎʽΪ£»
2¡¢findÃüÁîµÄ²ÎÊý£»
3¡¢findÃüÁîÑ¡Ï
4¡¢Ê¹ÓÃexec»òokÀ´Ö´ÐÐshellÃüÁ
¶þ¡¢findÃüÁîµÄÀý×Ó£»
1¡¢²éÕÒµ±Ç°Óû§Ö÷Ŀ¼ÏµÄËùÓÐÎļþ£»
2¡¢ÎªÁËÔÚµ±Ç°Ä¿Â¼ÖÐÎļþÊôÖ÷¾ßÓжÁ¡¢Ð´È¨ÏÞ£¬²¢ÇÒÎļþËùÊô×éµÄÓû§ºÍÆäËûÓû§¾ßÓжÁȨÏÞµÄÎļþ£»
3¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓÐÎļþ³¤¶ÈΪ0µÄÆÕͨÎļþ£¬²¢ÁгöËüÃǵÄÍêÕû·¾¶£»
4¡¢²éÕÒ/var/logsĿ¼Öиü¸Äʱ¼äÔÚ7ÈÕÒÔÇ°µÄÆÕͨÎļþ£¬²¢ÔÚɾ³ý֮ǰѯÎÊËüÃÇ£»
5¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓÐÊôÓÚroot×éµÄÎļþ£»
6¡¢findÃüÁɾ³ýµ±Ä¿Â¼ÖзÃÎÊʱ¼äÔÚ7ÈÕÒÔÀ´¡¢º¬ÓÐÊý×Öºó׺µÄadmin.logÎļþ
7¡¢ÎªÁ˲éÕÒµ±Ç°ÎļþϵͳÖеÄËùÓÐĿ¼²¢ÅÅÐò£»
8¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓеÄrmt´Å´øÉ豸£»
Èý¡¢xargs
ËÄ¡¢find ÃüÁîµÄ²ÎÊý£»
1¡¢Ê¹ÓÃnameÑ¡Ïî
2¡¢ÓÃpermÑ¡Ïî
3¡¢ºöÂÔij¸öĿ¼
4¡¢Ê¹ÓÃfind²éÕÒÎļþµÄʱºòÔõô±Ü¿ªÄ³¸öÎļþĿ¼
5¡¢Ê¹ÓÃuserºÍnouserÑ¡Ïî
6¡¢Ê¹ÓÃgroupºÍnogroupÑ¡Ïî
7¡¢°´ÕÕ¸ü¸Äʱ¼ä»ò·ÃÎÊʱ¼äµÈ²éÕÒÎļþ
8¡¢²éÕÒ±Èij¸öÎļþлò¾ÉµÄÎļþ
9¡¢Ê¹ÓÃtypeÑ¡Ïî
10¡¢Ê¹ÓÃsizeÑ¡Ïî
11¡¢Ê¹ÓÃdepthÑ¡Ïî
12¡¢Ê¹ÓÃmountÑ¡Ïî
Ç°ÑÔ£º¹ØÓÚfindÃüÁî
ÓÉÓÚfind¾ßÓÐÇ¿´óµÄ¹¦ÄÜ£¬ËùÒÔËüµÄÑ¡ÏîÒ²ºÜ¶à£¬ÆäÖд󲿷ÖÑ¡ÏֵµÃÎÒÃÇ»¨Ê±¼äÀ´Á˽âһϡ£¼´Ê¹ÏµÍ³Öк¬ÓÐÍøÂçÎļþϵͳ( NFS)£¬findÃüÁîÔÚ¸ÃÎļþϵͳÖÐͬÑùÓÐЧ£¬Ö»Äã¾ßÓÐÏàÓ¦µÄȨÏÞ¡£
ÔÚÔËÐÐÒ»¸ö·Ç³£ÏûºÄ×ÊÔ´µÄfindÃüÁîʱ£¬ºÜ¶àÈ˶¼ÇãÏòÓÚ°ÑËü·ÅÔÚºǫִ́ÐУ¬ÒòΪ±éÀúÒ»¸ö´óµÄÎļþϵͳ¿ÉÄܻỨ·ÑºÜ³¤µÄʱ¼ä(ÕâÀïÊÇÖ¸30G×Ö½ÚÒÔÉϵÄÎļþϵͳ)¡£
Ò»¡¢find ÃüÁî¸ñʽ
1¡¢findÃüÁîµÄÒ»°ãÐÎʽΪ£»
find pathname -options [-print -exec -ok ...]
2¡¢findÃüÁîµÄ²ÎÊý£»
pathname: findÃüÁîËù²éÕÒµÄĿ¼·¾¶¡£ÀýÈçÓÃ.À´±íʾµ±Ç°Ä¿Â¼£¬ÓÃ/À´±íʾϵͳ¸ùĿ¼¡£
-print£º findÃüÁƥÅäµÄÎļþÊä³öµ½±ê×¼Êä³ö¡£
-exec£º findÃüÁî¶ÔÆ¥ÅäµÄÎļþÖ´ÐиòÎÊýËù¸ø³öµÄshellÃüÁî¡£ÏàÓ¦ÃüÁîµÄÐÎʽΪ'command' { } \;£¬×¢Òâ{ }ºÍ\£»Ö®¼äµÄ¿Õ¸ñ¡£
-ok£º ºÍ-execµÄ×÷ÓÃÏàͬ£¬Ö»²»¹ýÒÔÒ»ÖÖ¸üΪ°²È«µÄģʽÀ´Ö´ÐиòÎÊýËù¸ø³öµÄshellÃüÁÔÚÖ´ÐÐÿһ¸öÃüÁî֮ǰ£¬¶¼»á¸ø³öÌáʾ£¬ÈÃÓû§À´È·¶¨ÊÇ·ñÖ´ÐС£
3¡¢findÃüÁîÑ¡Ïî
-name
°´ÕÕÎļþÃû²éÕÒÎļþ¡£
-perm
°´ÕÕÎļþȨÏÞÀ´²éÕÒÎļþ¡£
-prune
ʹÓÃÕâһѡÏî¿ÉÒÔʹfindÃüÁî²»ÔÚµ±Ç°Ö¸¶¨µÄĿ¼ÖвéÕÒ£¬Èç¹ûͬʱʹÓÃ-depthÑ¡ÏÄÇô-prune½«±»findÃüÁîºöÂÔ¡£
-user
°´ÕÕÎļþÊôÖ÷À´²éÕÒÎļþ¡£
-group
°´ÕÕÎļþËùÊôµÄ×éÀ´²éÕÒÎļþ¡£
-mtime -n +n
°´ÕÕÎļþµÄ¸ü¸Äʱ¼äÀ´²éÕÒÎļþ£¬ - n±íʾÎļþ¸ü¸Äʱ¼ä¾àÏÖÔÚnÌìÒÔÄÚ£¬+ n±íʾÎļþ¸ü¸Äʱ¼ä¾àÏÖÔÚnÌìÒÔÇ°¡£findÃüÁÓÐ-atimeºÍ-ctime Ñ¡Ïµ«ËüÃǶ¼ºÍ-m timeÑ¡Ïî¡£
-nogroup
²éÕÒÎÞÓÐЧËùÊô×éµÄÎļþ£¬¼´¸ÃÎļþËùÊôµÄ×éÔÚ/etc/groupsÖв»´æÔÚ¡£
-nouser
²éÕÒÎÞÓÐЧÊôÖ÷µÄÎļþ£¬¼´¸ÃÎļþµÄÊôÖ÷ÔÚ/etc/passwdÖв»´æÔÚ¡£
-newer file1 ! file2
²éÕÒ¸ü¸Äʱ¼ä±ÈÎļþfile1е«±ÈÎļþfile2¾ÉµÄÎļþ¡£
-type
²éÕÒijһÀàÐ͵ÄÎļþ£¬ÖîÈ磺
b - ¿éÉ豸Îļþ¡£
d - Ŀ¼¡£
c - ×Ö·ûÉ豸Îļþ¡£
p - ¹ÜµÀÎļþ¡£
l - ·ûºÅÁ´½ÓÎļþ¡£
f - ÆÕͨÎļþ¡£
-size n£º[c] ²éÕÒÎļþ³¤¶ÈΪn¿éµÄÎļþ£¬´øÓÐcʱ±íʾÎļþ³¤¶ÈÒÔ×ֽڼơ£
-depth£ºÔÚ²éÕÒÎļþʱ£¬Ê×ÏȲéÕÒµ±Ç°Ä¿Â¼ÖеÄÎļþ£¬È»ºóÔÙÔÚÆä×ÓĿ¼ÖвéÕÒ¡£
-fstype£º²éÕÒλÓÚijһÀàÐÍÎļþϵͳÖеÄÎļþ£¬ÕâЩÎļþϵͳÀàÐÍͨ³£¿ÉÒÔÔÚÅäÖÃÎļþ/etc/fstabÖÐÕÒµ½£¬¸ÃÅäÖÃÎļþÖаüº¬Á˱¾ÏµÍ³ÖÐÓйØÎļþϵͳµÄÐÅÏ¢¡£
-mount£ºÔÚ²éÕÒÎļþʱ²»¿çÔ½Îļþϵͳmountµã¡£
-follow£ºÈç¹ûfindÃüÁîÓöµ½·ûºÅÁ´½ÓÎļþ£¬¾Í¸ú×ÙÖÁÁ´½ÓËùÖ¸ÏòµÄÎļþ¡£
-cpio£º¶ÔÆ¥ÅäµÄÎļþʹÓÃcpioÃüÁ½«ÕâЩÎļþ±¸·Ýµ½´Å´øÉ豸ÖС£
ÁíÍâ,ÏÂÃæÈý¸öµÄÇø±ð:
-amin n
¡¡¡¡²éÕÒϵͳÖÐ×îºóN·ÖÖÓ·ÃÎʵÄÎļþ
¡¡¡¡-atime n
¡¡¡¡²éÕÒϵͳÖÐ×îºón*24Сʱ·ÃÎʵÄÎļþ
¡¡¡¡-cmin n
¡¡¡¡²éÕÒϵͳÖÐ×îºóN·ÖÖÓ±»¸Ä±äÎļþ״̬µÄÎļþ
¡¡¡¡-ctime n
¡¡¡¡²éÕÒϵͳÖÐ×îºón*24Сʱ±»¸Ä±äÎļþ״̬µÄÎļþ
¡¡-mmin n
¡¡¡¡²éÕÒϵͳÖÐ×îºóN·ÖÖÓ±»¸Ä±äÎļþÊý¾ÝµÄÎļþ
¡¡¡¡-mtime n
¡¡¡¡²éÕÒϵͳÖÐ×îºón*24Сʱ±»¸Ä±äÎļþÊý¾ÝµÄÎļþ
4¡¢Ê¹ÓÃexec»òokÀ´Ö´ÐÐshellÃüÁî
ʹÓÃfindʱ£¬Ö»Òª°ÑÏëÒªµÄ²Ù×÷дÔÚÒ»¸öÎļþÀ¾Í¿ÉÒÔÓÃexecÀ´ÅäºÏfind²éÕÒ£¬ºÜ·½±ãµÄ
ÔÚÓÐЩ²Ù×÷ϵͳÖÐÖ»ÔÊÐí-execÑ¡ÏîÖ´ÐÐÖîÈçl s»òls -lÕâÑùµÄÃüÁî¡£´ó¶àÊýÓû§Ê¹ÓÃÕâһѡÏîÊÇΪÁ˲éÕÒ¾ÉÎļþ²¢É¾³ýËüÃÇ¡£½¨ÒéÔÚÕæÕýÖ´ÐÐrmÃüÁîɾ³ýÎļþ֮ǰ£¬×îºÃÏÈÓÃlsÃüÁһϣ¬È·ÈÏËüÃÇÊÇËùҪɾ³ýµÄÎļþ¡£
execÑ¡ÏîºóÃæ¸úËæ×ÅËùÒªÖ´ÐеÄÃüÁî»ò½Å±¾£¬È»ºóÊÇÒ»¶Ô¶ù{ }£¬Ò»¸ö¿Õ¸ñºÍÒ»¸ö\£¬×îºóÊÇÒ»¸ö·ÖºÅ¡£ÎªÁËʹÓÃexecÑ¡Ï±ØÐëҪͬʱʹÓÃprintÑ¡Ïî¡£Èç¹ûÑéÖ¤Ò»ÏÂfindÃüÁ»á·¢ÏÖ¸ÃÃüÁîÖ»Êä³ö´Óµ±Ç°Â·¾¶ÆðµÄÏà¶Ô·¾¶¼°ÎļþÃû¡£
ÀýÈ磺ΪÁËÓÃls -lÃüÁîÁгöËùÆ¥Åäµ½µÄÎļþ£¬¿ÉÒÔ°Ñls -lÃüÁî·ÅÔÚfindÃüÁîµÄ-execÑ¡ÏîÖÐ
# find . -type f -exec ls -l { } \;
-rw-r--r-- 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r--r-- 1 root root 12959 2003-02-25 ./conf/magic
-rw-r--r-- 1 root root 180 2003-02-25 ./conf.d/README
ÉÏÃæµÄÀý×ÓÖУ¬findÃüÁîÆ¥Åäµ½Á˵±Ç°Ä¿Â¼ÏµÄËùÓÐÆÕͨÎļþ£¬²¢ÔÚ-execÑ¡ÏîÖÐʹÓÃls -lÃüÁËüÃÇÁгö¡£
ÔÚ/logsĿ¼ÖвéÕÒ¸ü¸Äʱ¼äÔÚ5ÈÕÒÔÇ°µÄÎļþ²¢É¾³ýËüÃÇ£º
$ find logs -type f -mtime +5 -exec rm { } \;
¼Çס£ºÔÚshellÖÐÓÃÈκη½Ê½É¾³ýÎļþ֮ǰ£¬Ó¦µ±ÏȲ鿴ÏàÓ¦µÄÎļþ£¬Ò»¶¨ÒªÐ¡ÐÄ£¡µ±Ê¹ÓÃÖîÈçmv»òrmÃüÁîʱ£¬¿ÉÒÔʹÓÃ-execÑ¡ÏîµÄ°²È«Ä£Ê½¡£Ëü½«ÔÚ¶Ôÿ¸öÆ¥Åäµ½µÄÎļþ½øÐвÙ×÷֮ǰÌáʾÄã¡£
ÔÚÏÂÃæµÄÀý×ÓÖУ¬ findÃüÁîÔÚµ±Ç°Ä¿Â¼ÖвéÕÒËùÓÐÎļþÃûÒÔ.LOG½áβ¡¢¸ü¸Äʱ¼äÔÚ5ÈÕÒÔÉϵÄÎļþ£¬²¢É¾³ýËüÃÇ£¬Ö»²»¹ýÔÚɾ³ý֮ǰÏȸø³öÌáʾ¡£
$ find . -name "*.conf" -mtime +5 -ok rm { } \;
< rm ... ./conf/httpd.conf > ? n
°´y¼üɾ³ýÎļþ£¬°´n¼ü²»É¾³ý¡£
ÈκÎÐÎʽµÄÃüÁ¿ÉÒÔÔÚ-execÑ¡ÏîÖÐʹÓá£
ÔÚÏÂÃæµÄÀý×ÓÖÐÎÒÃÇʹÓÃgrepÃüÁî¡£findÃüÁîÊ×ÏÈÆ¥ÅäËùÓÐÎļþÃûΪ¡° passwd*¡±µÄÎļþ£¬ÀýÈçpasswd¡¢passwd.old¡¢passwd.bak£¬È»ºóÖ´ÐÐgrepÃüÁî¿´¿´ÔÚÕâЩÎļþÖÐÊÇ·ñ´æÔÚÒ»¸ösamÓû§¡£
# find /etc -name "passwd*" -exec grep "sam" { } \;
sam:x:501:501::/usr/sam:/bin/bash
¶þ¡¢findÃüÁîµÄÀý×Ó£»
1¡¢²éÕÒµ±Ç°Óû§Ö÷Ŀ¼ÏµÄËùÓÐÎļþ£º
ÏÂÃæÁ½ÖÖ·½·¨¶¼¿ÉÒÔʹÓÃ
$ find $HOME -print
$ find ~ -print
2¡¢Èõ±Ç°Ä¿Â¼ÖÐÎļþÊôÖ÷¾ßÓжÁ¡¢Ð´È¨ÏÞ£¬²¢ÇÒÎļþËùÊô×éµÄÓû§ºÍÆäËûÓû§¾ßÓжÁȨÏÞµÄÎļþ£»
$ find . -type f -perm 644 -exec ls -l { } \;
3¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓÐÎļþ³¤¶ÈΪ0µÄÆÕͨÎļþ£¬²¢ÁгöËüÃǵÄÍêÕû·¾¶£»
$ find / -type f -size 0 -exec ls -l { } \;
4¡¢²éÕÒ/var/logsĿ¼Öиü¸Äʱ¼äÔÚ7ÈÕÒÔÇ°µÄÆÕͨÎļþ£¬²¢ÔÚɾ³ý֮ǰѯÎÊËüÃÇ£»
$ find /var/logs -type f -mtime +7 -ok rm { } \;
5¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓÐÊôÓÚroot×éµÄÎļþ£»
$find . -group root -exec ls -l { } \;
-rw-r--r-- 1 root root 595 10ÔÂ 31 01:09 ./fie1
6¡¢findÃüÁɾ³ýµ±Ä¿Â¼ÖзÃÎÊʱ¼äÔÚ7ÈÕÒÔÀ´¡¢º¬ÓÐÊý×Öºó׺µÄadmin.logÎļþ¡£
¸ÃÃüÁîÖ»¼ì²éÈýλÊý×Ö£¬ËùÒÔÏàÓ¦ÎļþµÄºó׺²»Òª³¬¹ý999¡£ÏȽ¨¼¸¸öadmin.log*µÄÎļþ £¬²ÅÄÜʹÓÃÏÂÃæÕâ¸öÃüÁî
$ find . -name "admin.log[0-9][0-9][0-9]" -atime -7 -ok
rm { } \;
< rm ... ./admin.log001 > ? n
< rm ... ./admin.log002 > ? n
< rm ... ./admin.log042 > ? n
< rm ... ./admin.log942 > ? n
7¡¢ÎªÁ˲éÕÒµ±Ç°ÎļþϵͳÖеÄËùÓÐĿ¼²¢ÅÅÐò£»
$ find . -type d | sort
8¡¢ÎªÁ˲éÕÒϵͳÖÐËùÓеÄrmt´Å´øÉ豸£»
$ find /dev/rmt -print
Èý¡¢xargs
xargs - build and execute command lines from standard input
ÔÚʹÓÃfindÃüÁîµÄ-execÑ¡Ïî´¦ÀíÆ¥Åäµ½µÄÎļþʱ£¬ findÃüÁËùÓÐÆ¥Åäµ½µÄÎļþÒ»Æ𴫵ݸøexecÖ´ÐС£µ«ÓÐЩϵͳ¶ÔÄܹ»´«µÝ¸øexecµÄÃüÁ¶ÈÓÐÏÞÖÆ£¬ÕâÑùÔÚfindÃüÁîÔËÐм¸·ÖÖÓÖ®ºó£¬¾Í»á³öÏÖÒç³ö´íÎó¡£´íÎóÐÅϢͨ³£ÊÇ¡°²ÎÊýÁÐÌ«³¤¡±»ò¡°²ÎÊýÁÐÒç³ö¡±¡£Õâ¾ÍÊÇxargsÃüÁîµÄÓô¦ËùÔÚ£¬ÌرðÊÇÓëfindÃüÁîÒ»ÆðʹÓá£
findÃüÁî°ÑÆ¥Åäµ½µÄÎļþ´«µÝ¸øxargsÃüÁ¶øxargsÃüÁîÿ´ÎÖ»»ñÈ¡Ò»²¿·ÖÎļþ¶ø²»ÊÇÈ«²¿£¬²»Ïñ-execÑ¡ÏîÄÇÑù¡£ÕâÑùËü¿ÉÒÔÏÈ´¦Àí×îÏÈ»ñÈ¡µÄÒ»²¿·ÖÎļþ£¬È»ºóÊÇÏÂÒ»Åú£¬²¢Èç´Ë¼ÌÐøÏÂÈ¥¡£
ÔÚÓÐЩϵͳÖУ¬Ê¹ÓÃ-execÑ¡Ïî»áΪ´¦Àíÿһ¸öÆ¥Åäµ½µÄÎļþ¶ø·¢ÆðÒ»¸öÏàÓ¦µÄ½ø³Ì£¬²¢·Ç½«Æ¥Åäµ½µÄÎļþÈ«²¿×÷Ϊ²ÎÊýÒ»´ÎÖ´ÐУ»ÕâÑùÔÚÓÐЩÇé¿öϾͻá³öÏÖ½ø³Ì¹ý¶à£¬ÏµÍ³ÐÔÄÜϽµµÄÎÊÌ⣬Òò¶øЧÂʲ»¸ß£»
¶øʹÓÃxargsÃüÁîÔòÖ»ÓÐÒ»¸ö½ø³Ì¡£ÁíÍ⣬ÔÚʹÓÃxargsÃüÁîʱ£¬¾¿¾¹ÊÇÒ»´Î»ñÈ¡ËùÓеIJÎÊý£¬»¹ÊÇ·ÖÅúÈ¡µÃ²ÎÊý£¬ÒÔ¼°Ã¿Ò»´Î»ñÈ¡²ÎÊýµÄÊýÄ¿¶¼»á¸ù¾Ý¸ÃÃüÁîµÄÑ¡ÏϵͳÄÚºËÖÐÏàÓ¦µÄ¿Éµ÷²ÎÊýÀ´È·¶¨¡£
À´¿´¿´xargsÃüÁîÊÇÈçºÎͬfindÃüÁîÒ»ÆðʹÓõģ¬²¢¸ø³öһЩÀý×Ó¡£
ÏÂÃæµÄÀý×Ó²éÕÒϵͳÖеÄÿһ¸öÆÕͨÎļþ£¬È»ºóʹÓÃxargsÃüÁîÀ´²âÊÔËüÃÇ·Ö±ðÊôÓÚÄÄÀàÎļþ
#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory: ISO-8859 text\
......
ÔÚÕû¸öϵͳÖвéÕÒÄÚ´æÐÅϢת´¢Îļþ(core dump) £¬È»ºó°Ñ½á¹û±£´æµ½/tmp/core.log ÎļþÖУº
$ find / -name "core" -print | xargs echo "" >/tmp/core.log
ÉÏÃæÕâ¸öÖ´ÐÐÌ«Âý£¬ÎҸijÉÔÚµ±Ç°Ä¿Â¼Ï²éÕÒ
#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6
ÔÚµ±Ç°Ä¿Â¼Ï²éÕÒËùÓÐÓû§¾ßÓжÁ¡¢Ð´ºÍÖ´ÐÐȨÏÞµÄÎļþ£¬²¢ÊÕ»ØÏàÓ¦µÄдȨÏÞ£º
# ls -l
drwxrwxrwx 2 sam adm 4096 10ÔÂ 30 20:14 file6
-rwxrwxrwx 2 sam adm 0 10ÔÂ 31 01:01 http3.conf
-rwxrwxrwx 2 sam adm 0 10ÔÂ 31 01:01 httpd.conf
# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x 2 sam adm 4096 10ÔÂ 30 20:14 file6
-rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 http3.conf
-rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 httpd.conf
ÓÃgrepÃüÁîÔÚËùÓеÄÆÕͨÎļþÖÐËÑË÷hostnameÕâ¸ö´Ê£º
# find . -type f -print | xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your
ÓÃgrepÃüÁîÔÚµ±Ç°Ä¿Â¼ÏµÄËùÓÐÆÕͨÎļþÖÐËÑË÷hostnamesÕâ¸ö´Ê£º
# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:# different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your
×¢Ò⣬ÔÚÉÏÃæµÄÀý×ÓÖУ¬ \ÓÃÀ´È¡ÏûfindÃüÁîÖеÄ*ÔÚshellÖеÄÌØÊ⺬Òå¡£
findÃüÁîÅäºÏʹÓÃexecºÍxargs¿ÉÒÔʹÓû§¶ÔËùÆ¥Åäµ½µÄÎļþÖ´Ðм¸ºõËùÓеÄÃüÁî¡£
ËÄ¡¢find ÃüÁîµÄ²ÎÊý
ÏÂÃæÊÇfindһЩ³£ÓòÎÊýµÄÀý×Ó£¬ÓÐÓõ½µÄʱºò²é²é¾ÍÐÐÁË£¬ÏñÉÏÃæÇ°¼¸¸öÌù×Ó£¬¶¼Óõ½ÁËÆäÖеĵÄһЩ²ÎÊý£¬Ò²¿ÉÒÔÓÃman»ò²é¿´ÂÛ̳ÀïÆäËüÌù×ÓÓÐfindµÄÃüÁîÊÖ²á
1¡¢Ê¹ÓÃnameÑ¡Ïî
ÎļþÃûÑ¡ÏîÊÇfindÃüÁî×î³£ÓõÄÑ¡ÏҪôµ¥¶ÀʹÓøÃÑ¡ÏҪôºÍÆäËûÑ¡ÏîÒ»ÆðʹÓá£
¿ÉÒÔʹÓÃijÖÖÎļþÃûģʽÀ´Æ¥ÅäÎļþ£¬¼ÇסҪÓÃÒýºÅ½«ÎļþÃûģʽÒýÆðÀ´¡£
²»¹Üµ±Ç°Â·¾¶ÊÇʲô£¬Èç¹ûÏëÒªÔÚ×Ô¼ºµÄ¸ùĿ¼$HOMEÖвéÕÒÎļþÃû·ûºÏ*.txtµÄÎļþ£¬Ê¹ÓÃ~×÷Ϊ 'pathname'²ÎÊý£¬²¨À˺Å~´ú±íÁËÄãµÄ$HOMEĿ¼¡£
$ find ~ -name "*.txt" -print
ÏëÒªÔÚµ±Ç°Ä¿Â¼¼°×ÓĿ¼ÖвéÕÒËùÓеġ® *.txt¡¯Îļþ£¬¿ÉÒÔÓãº
$ find . -name "*.txt" -print
ÏëÒªµÄµ±Ç°Ä¿Â¼¼°×ÓĿ¼ÖвéÕÒÎļþÃûÒÔÒ»¸ö´óд×Öĸ¿ªÍ·µÄÎļþ£¬¿ÉÒÔÓãº
$ find . -name "[A-Z]*" -print
ÏëÒªÔÚ/etcĿ¼ÖвéÕÒÎļþÃûÒÔhost¿ªÍ·µÄÎļþ£¬¿ÉÒÔÓãº
$ find /etc -name "host*" -print
ÏëÒª²éÕÒ$HOMEĿ¼ÖеÄÎļþ£¬¿ÉÒÔÓãº
$ find ~ -name "*" -print »òfind . -print
ÒªÏëÈÃϵͳ¸ß¸ººÉÔËÐУ¬¾Í´Ó¸ùĿ¼¿ªÊ¼²éÕÒËùÓеÄÎļþ¡£
$ find / -name "*" -print
Èç¹ûÏëÔÚµ±Ç°Ä¿Â¼²éÕÒÎļþÃûÒÔÁ½¸öСд×Öĸ¿ªÍ·£¬¸ú×ÅÊÇÁ½¸öÊý×Ö£¬×îºóÊÇ.txtµÄÎļþ£¬ÏÂÃæµÄÃüÁî¾ÍÄܹ»·µ»ØÃûΪax37.txtµÄÎļþ£º
$find . -name "[a-z][a-z][0--9][0--9].txt" -print
2¡¢ÓÃpermÑ¡Ïî
°´ÕÕÎļþȨÏÞģʽÓÃ-permÑ¡Ïî,°´ÎļþȨÏÞģʽÀ´²éÕÒÎļþµÄ»°¡£×îºÃʹÓð˽øÖƵÄȨÏÞ±íʾ·¨¡£
ÈçÔÚµ±Ç°Ä¿Â¼Ï²éÕÒÎļþȨÏÞλΪ755µÄÎļþ£¬¼´ÎļþÊôÖ÷¿ÉÒÔ¶Á¡¢Ð´¡¢Ö´ÐУ¬ÆäËûÓû§¿ÉÒÔ¶Á¡¢Ö´ÐеÄÎļþ£¬¿ÉÒÔÓãº
$ find . -perm 755 -print
»¹ÓÐÒ»ÖÖ±í´ï·½·¨£ºÔڰ˽øÖÆÊý×ÖÇ°ÃæÒª¼ÓÒ»¸öºá¸Ü-£¬±íʾ¶¼Æ¥Å䣬Èç-007¾ÍÏ൱ÓÚ777£¬-006Ï൱ÓÚ666
# ls -l
-rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 http3.conf
-rw-rw-rw- 1 sam adm 34890 10ÔÂ 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10ÔÂ 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10ÔÂ 31 20:19 temp
# find . -perm 006
# find . -perm -006
./sam
./httpd1.conf
./temp
-perm mode:ÎļþÐí¿ÉÕýºÃ·ûºÏmode
-perm +mode:ÎļþÐí¿É²¿·Ö·ûºÏmode
-perm -mode: ÎļþÐí¿ÉÍêÈ«·ûºÏmode
3¡¢ºöÂÔij¸öĿ¼
Èç¹ûÔÚ²éÕÒÎļþʱϣÍûºöÂÔij¸öĿ¼£¬ÒòΪÄãÖªµÀÄǸöĿ¼ÖÐûÓÐÄãËùÒª²éÕÒµÄÎļþ£¬ÄÇô¿ÉÒÔʹÓÃ-pruneÑ¡ÏîÀ´Ö¸³öÐèÒªºöÂÔµÄĿ¼¡£ÔÚʹÓÃ-pruneÑ¡ÏîʱҪµ±ÐÄ£¬ÒòΪÈç¹ûÄãͬʱʹÓÃÁË-depthÑ¡ÏÄÇô-pruneÑ¡Ïî¾Í»á±»findÃüÁîºöÂÔ¡£
Èç¹ûÏ£ÍûÔÚ/appsĿ¼Ï²éÕÒÎļþ£¬µ«²»Ï£ÍûÔÚ/apps/binĿ¼Ï²éÕÒ£¬¿ÉÒÔÓãº
$ find /apps -path "/apps/bin" -prune -o -print
4¡¢Ê¹ÓÃfind²éÕÒÎļþµÄʱºòÔõô±Ü¿ªÄ³¸öÎļþĿ¼
±ÈÈçÒªÔÚ/usr/samĿ¼Ï²éÕÒ²»ÔÚdir1×ÓĿ¼֮ÄÚµÄËùÓÐÎļþ
find /usr/sam -path "/usr/sam/dir1" -prune -o -print
find [-path ..] [expression] ÔÚ·¾¶ÁбíµÄºóÃæµÄÊDZí´ïʽ
-path "/usr/sam" -prune -o -print ÊÇ -path "/usr/sam" -a -prune -o
-print µÄ¼òд±í´ïʽ°´Ë³ÐòÇóÖµ, -a ºÍ -o ¶¼ÊǶÌ·ÇóÖµ£¬Óë shell µÄ && ºÍ || ÀàËÆÈç¹û -path "/usr/sam" ΪÕ棬ÔòÇóÖµ -prune , -prune ·µ»ØÕ棬ÓëÂß¼±í´ïʽΪÕ棻·ñÔò²»ÇóÖµ -prune£¬ÓëÂß¼±í´ïʽΪ¼Ù¡£Èç¹û -path "/usr/sam" -a -prune Ϊ¼Ù£¬ÔòÇóÖµ -print £¬-print·µ»ØÕ棬»òÂß¼±í´ïʽΪÕ棻·ñÔò²»ÇóÖµ -print£¬»òÂß¼±í´ïʽΪÕæ¡£
Õâ¸ö±í´ïʽ×éºÏÌØÀý¿ÉÒÔÓÃαÂëдΪ
if -path "/usr/sam" then
-prune
else
-print
±Ü¿ª¶à¸öÎļþ¼Ð
find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print
Ô²À¨ºÅ±íʾ±í´ïʽµÄ½áºÏ¡£
\ ±íʾÒýÓ㬼´Ö¸Ê¾ shell ²»¶ÔºóÃæµÄ×Ö·û×÷ÌØÊâ½âÊÍ£¬¶øÁô¸ø find ÃüÁîÈ¥½âÊÍÆäÒâÒå¡£
²éÕÒijһȷ¶¨Îļþ£¬-nameµÈÑ¡Ïî¼ÓÔÚ-o Ö®ºó
#find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print
5¡¢Ê¹ÓÃuserºÍnouserÑ¡Ïî
°´ÎļþÊôÖ÷²éÕÒÎļþ£¬ÈçÔÚ$HOMEĿ¼ÖвéÕÒÎļþÊôÖ÷ΪsamµÄÎļþ£¬¿ÉÒÔÓãº
$ find ~ -user sam -print
ÔÚ/etcĿ¼Ï²éÕÒÎļþÊôÖ÷ΪuucpµÄÎļþ£º
$ find /etc -user uucp -print
ΪÁ˲éÕÒÊôÖ÷ÕÊ»§ÒѾ±»É¾³ýµÄÎļþ£¬¿ÉÒÔʹÓÃ-nouserÑ¡Ïî¡£ÕâÑù¾ÍÄܹ»ÕÒµ½ÄÇЩÊôÖ÷ÔÚ/etc/passwdÎļþÖÐûÓÐÓÐЧÕÊ»§µÄÎļþ¡£ÔÚʹÓÃ-nouserÑ¡Ïîʱ£¬²»±Ø¸ø³öÓû§Ãû£» findÃüÁîÄܹ»ÎªÄãÍê³ÉÏàÓ¦µÄ¹¤×÷¡£
ÀýÈ磬ϣÍûÔÚ/homeĿ¼Ï²éÕÒËùÓеÄÕâÀàÎļþ£¬¿ÉÒÔÓãº
$ find /home -nouser -print
6¡¢Ê¹ÓÃgroupºÍnogroupÑ¡Ïî
¾ÍÏñuserºÍnouserÑ¡ÏîÒ»Ñù£¬Õë¶ÔÎļþËùÊôÓÚµÄÓû§×飬 findÃüÁîÒ²¾ßÓÐͬÑùµÄÑ¡ÏΪÁËÔÚ/appsĿ¼Ï²éÕÒÊôÓÚgemÓû§×éµÄÎļþ£¬¿ÉÒÔÓãº
$ find /apps -group gem -print
Òª²éÕÒûÓÐÓÐЧËùÊôÓû§×éµÄËùÓÐÎļþ£¬¿ÉÒÔʹÓÃnogroupÑ¡Ïî¡£ÏÂÃæµÄfindÃüÁî´ÓÎļþϵͳµÄ¸ùĿ¼´¦²éÕÒÕâÑùµÄÎļþ
$ find / -nogroup-print
7¡¢°´ÕÕ¸ü¸Äʱ¼ä»ò·ÃÎÊʱ¼äµÈ²éÕÒÎļþ
Èç¹ûÏ£Íû°´ÕÕ¸ü¸Äʱ¼äÀ´²éÕÒÎļþ£¬¿ÉÒÔʹÓÃmtime,atime»òctimeÑ¡Ïî¡£Èç¹ûϵͳͻȻûÓпÉÓÿռäÁË£¬ºÜÓпÉÄÜijһ¸öÎļþµÄ³¤¶ÈÔÚ´ËÆÚ¼äÔö³¤Ñ¸ËÙ£¬Õâʱ¾Í¿ÉÒÔÓÃmtimeÑ¡ÏîÀ´²éÕÒÕâÑùµÄÎļþ¡£
ÓüõºÅ-À´ÏÞ¶¨¸ü¸Äʱ¼äÔÚ¾à½ñnÈÕÒÔÄÚµÄÎļþ£¬¶øÓüӺÅ+À´ÏÞ¶¨¸ü¸Äʱ¼äÔÚ¾à½ñnÈÕÒÔÇ°µÄÎļþ¡£
Ï£ÍûÔÚϵͳ¸ùĿ¼Ï²éÕÒ¸ü¸Äʱ¼äÔÚ5ÈÕÒÔÄÚµÄÎļþ£¬¿ÉÒÔÓãº
$ find / -mtime -5 -print
ΪÁËÔÚ/var/admĿ¼Ï²éÕÒ¸ü¸Äʱ¼äÔÚ3ÈÕÒÔÇ°µÄÎļþ£¬¿ÉÒÔÓãº
$ find /var/adm -mtime +3 -print
8¡¢²éÕÒ±Èij¸öÎļþлò¾ÉµÄÎļþ
Èç¹ûÏ£Íû²éÕÒ¸ü¸Äʱ¼ä±Èij¸öÎļþе«±ÈÁíÒ»¸öÎļþ¾ÉµÄËùÓÐÎļþ£¬¿ÉÒÔʹÓÃ-newerÑ¡Ïî¡£ËüµÄÒ»°ãÐÎʽΪ£º
newest_file_name ! oldest_file_name
ÆäÖУ¬£¡ÊÇÂß¼·Ç·ûºÅ¡£
²éÕÒ¸ü¸Äʱ¼ä±ÈÎļþsamе«±ÈÎļþtemp¾ÉµÄÎļþ£º
Àý£ºÓÐÁ½¸öÎļþ
-rw-r--r-- 1 sam adm 0 10ÔÂ 31 01:07 fiel
-rw-rw-rw- 1 sam adm 34890 10ÔÂ 31 00:57 httpd1.conf
-rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 httpd.conf
drw-rw-rw- 2 gem group 4096 10ÔÂ 26 19:48 sam
-rw-rw-rw- 1 root root 2792 10ÔÂ 31 20:19 temp
# find -newer httpd1.conf ! -newer temp -ls
1077669 0 -rwxrwxr-x 2 sam adm 0 10ÔÂ 31 01:01 ./httpd.conf
1077671 4 -rw-rw-rw- 1 root root 2792 10ÔÂ 31 20:19 ./temp
1077673 0 -rw-r--r-- 1 sam adm 0 10ÔÂ 31 01:07 ./fiel
²éÕÒ¸ü¸Äʱ¼äÔÚ±ÈtempÎļþеÄÎļþ£º
$ find . -newer temp -print
9¡¢Ê¹ÓÃtypeÑ¡Ïî
ÔÚ/etcĿ¼Ï²éÕÒËùÓеÄĿ¼£¬¿ÉÒÔÓãº
$ find /etc -type d -print
ÔÚµ±Ç°Ä¿Â¼Ï²éÕÒ³ýĿ¼ÒÔÍâµÄËùÓÐÀàÐ͵ÄÎļþ£¬¿ÉÒÔÓãº
$ find . ! -type d -print
ÔÚ/etcĿ¼Ï²éÕÒËùÓеķûºÅÁ´½ÓÎļþ£¬¿ÉÒÔÓÃ
$ find /etc -type l -print
10¡¢Ê¹ÓÃsizeÑ¡Ïî
¿ÉÒÔ°´ÕÕÎļþ³¤¶ÈÀ´²éÕÒÎļþ£¬ÕâÀïËùÖ¸µÄÎļþ³¤¶È¼È¿ÉÒÔÓÿ飨block£©À´¼ÆÁ¿£¬Ò²¿ÉÒÔÓÃ×Ö½ÚÀ´¼ÆÁ¿¡£ÒÔ×Ö½Ú¼ÆÁ¿Îļþ³¤¶ÈµÄ±í´ïÐÎʽΪN c£»ÒÔ¿é¼ÆÁ¿Îļþ³¤¶ÈÖ»ÓÃÊý×Ö±íʾ¼´¿É¡£
ÔÚ°´ÕÕÎļþ³¤¶È²éÕÒÎļþʱ£¬Ò»°ãʹÓÃÕâÖÖÒÔ×Ö½Ú±íʾµÄÎļþ³¤¶È£¬Ôڲ鿴ÎļþϵͳµÄ´óС£¬ÒòΪÕâʱʹÓÿéÀ´¼ÆÁ¿¸üÈÝÒ×ת»»¡£
ÔÚµ±Ç°Ä¿Â¼Ï²éÕÒÎļþ³¤¶È´óÓÚ1 M×Ö½ÚµÄÎļþ£º
$ find . -size +1000000c -print
ÔÚ/home/apacheĿ¼Ï²éÕÒÎļþ³¤¶ÈÇ¡ºÃΪ100×Ö½ÚµÄÎļþ£º
$ find /home/apache -size 100c -print
ÔÚµ±Ç°Ä¿Â¼Ï²éÕÒ³¤¶È³¬¹ý10¿éµÄÎļþ£¨Ò»¿éµÈÓÚ512×Ö½Ú£©£º
$ find . -size +10 -print
11¡¢Ê¹ÓÃdepthÑ¡Ïî
ÔÚʹÓÃfindÃüÁîʱ£¬¿ÉÄÜÏ£ÍûÏÈÆ¥ÅäËùÓеÄÎļþ£¬ÔÙÔÚ×ÓĿ¼ÖвéÕÒ¡£Ê¹ÓÃdepthÑ¡Ïî¾Í¿ÉÒÔʹfindÃüÁîÕâÑù×ö¡£ÕâÑù×öµÄÒ»¸öÔÒò¾ÍÊÇ£¬µ±ÔÚʹÓÃfindÃüÁîÏò´Å´øÉϱ¸·ÝÎļþϵͳʱ£¬Ï£ÍûÊ×Ïȱ¸·ÝËùÓеÄÎļþ£¬Æä´ÎÔÙ±¸·Ý×ÓĿ¼ÖеÄÎļþ¡£
ÔÚÏÂÃæµÄÀý×ÓÖУ¬ findÃüÁî´ÓÎļþϵͳµÄ¸ùĿ¼¿ªÊ¼£¬²éÕÒÒ»¸öÃûΪCON.FILEµÄÎļþ¡£
Ëü½«Ê×ÏÈÆ¥ÅäËùÓеÄÎļþÈ»ºóÔÙ½øÈë×ÓĿ¼ÖвéÕÒ¡£
$ find / -name "CON.FILE" -depth -print
12¡¢Ê¹ÓÃmountÑ¡Ïî
ÔÚµ±Ç°µÄÎļþϵͳÖвéÕÒÎļþ£¨²»½øÈëÆäËûÎļþϵͳ£©£¬¿ÉÒÔʹÓÃfindÃüÁîµÄmountÑ¡Ïî¡£
´Óµ±Ç°Ä¿Â¼¿ªÊ¼²éÕÒλÓÚ±¾ÎļþϵͳÖÐÎļþÃûÒÔXC½áβµÄÎļþ£º
$ find . -name "*.XC" -mount -print