³ýÁËÔÚÒ»¸öĿ¼½á¹¹Ï²éÕÒÎļþÕâÖÖ»ù±¾µÄ²Ù×÷£¬Ä㻹¿ÉÒÔÓÃfindÃüÁîʵÏÖһЩʵÓõIJÙ×÷£¬Ê¹ÄãµÄÃüÁîÐÐÖ®Âøü¼Ó¼òÒס£
±¾ÎĽ«½éÉÜ15ÖÖÎÞÂÛÊÇÓÚÐÂÊÖ»¹ÊÇÀÏÄñ¶¼·Ç³£ÓÐÓõÄLinux findÃüÁî¡£
Ê×ÏÈ£¬ÔÚÄãµÄhomeĿ¼ÏÂÃæ´´½¨ÏÂÃæµÄ¿ÕÎļþ£¬À´²âÊÔÏÂÃæµÄfindÃüÁîʾÀý¡£
01 # vim create_sample_files.sh
02 touch MybashProgram.sh
03 touch mycprogram.c
04 touch MyCProgram.c
05 touch Program.c
06
07 mkdir backup
08 cd backup
09
10 touch MybashProgram.sh
11 touch mycprogram.c
12 touch MyCProgram.c
13 touch Program.c
14
15 # chmod +x create_sample_files.sh
16
17 # ./create_sample_files.sh
18
19 # ls -R
20 .:
21 backup MybashProgram.sh MyCProgram.c
22 create_sample_files.sh mycprogram.c Program.c
23
24 ./backup:
25 MybashProgram.sh mycprogram.c MyCProgram.c Program.c
1. ÓÃÎļþÃû²éÕÒÎļþ
ÕâÊÇfindÃüÁîµÄÒ»¸ö»ù±¾Ó÷¨¡£ÏÂÃæµÄÀý×ÓչʾÁËÓÃMyCProgram.c×÷Ϊ²éÕÒÃûÔÚµ±Ç°Ä¿Â¼¼°Æä×ÓĿ¼ÖвéÕÒÎļþµÄ·½·¨¡£
1 # find -name "MyCProgram.c"
2 ./backup/MyCProgram.c
3 ./MyCProgram.c
2.ÓÃÎļþÃû²éÕÒÎļþ£¬ºöÂÔ´óСд
ÕâÊÇfindÃüÁîµÄÒ»¸ö»ù±¾Ó÷¨¡£ÏÂÃæµÄÀý×ÓչʾÁËÓÃMyCProgram.c×÷Ϊ²éÕÒÃûÔÚµ±Ç°Ä¿Â¼¼°Æä×ÓĿ¼ÖвéÕÒÎļþµÄ·½·¨£¬ºöÂÔÁË´óСд¡£
1 # find -iname "MyCProgram.c"
2 ./mycprogram.c
3 ./backup/mycprogram.c
4 ./backup/MyCProgram.c
5 ./MyCProgram.c
3. ʹÓÃmindepthºÍmaxdepthÏÞ¶¨ËÑË÷Ö¸¶¨Ä¿Â¼µÄÉî¶È
ÔÚrootĿ¼¼°Æä×ÓĿ¼Ï²éÕÒpasswdÎļþ¡£
1 # find / -name passwd
2 ./usr/share/doc/nss_ldap-253/pam.d/passwd
3 ./usr/bin/passwd
4 ./etc/pam.d/passwd
5 ./etc/passwd
ÔÚrootĿ¼¼°Æä1²ãÉîµÄ×ÓĿ¼ÖвéÕÒpasswd. (ÀýÈçroot ¡ª level 1, and one sub-directory ¡ª level 2)
1 # find -maxdepth 2 -name passwd
2 ./etc/passwd
ÔÚrootĿ¼Ï¼°Æä×î´óÁ½²ãÉî¶ÈµÄ×ÓĿ¼ÖвéÕÒpasswdÎļþ. (ÀýÈç root ¡ª level 1, and two sub-directories ¡ª level 2 and 3 )
1 # find / -maxdepth 3 -name passwd
2 ./usr/bin/passwd
3 ./etc/pam.d/passwd
4 ./etc/passwd
ÔÚµÚ¶þ²ã×ÓĿ¼ºÍµÚËIJã×ÓĿ¼֮¼ä²éÕÒpasswdÎļþ¡£
1 # find -mindepth 3 -maxdepth 5 -name passwd
2 ./usr/bin/passwd
3 ./etc/pam.d/passwd
4. ÔÚfindÃüÁî²éÕÒµ½µÄÎļþÉÏÖ´ÐÐÃüÁî
ÏÂÃæµÄÀý×ÓչʾÁËfindÃüÁîÀ´¼ÆËãËùÓв»Çø·Ö´óСдµÄÎļþÃûΪ¡°MyCProgram.c¡±µÄÎļþµÄMD5ÑéÖ¤ºÍ¡£{}½«»á±»µ±Ç°ÎļþÃûÈ¡´ú¡£
1 find -iname "MyCProgram.c" -exec md5sum {} \;
2 d41d8cd98f00b204e9800998ecf8427e ./mycprogram.c
3 d41d8cd98f00b204e9800998ecf8427e ./backup/mycprogram.c
4 d41d8cd98f00b204e9800998ecf8427e ./backup/MyCProgram.c
5 d41d8cd98f00b204e9800998ecf8427e ./MyCProgram.c
5. Ïà·´Æ¥Åä
ÏÔʾËùÓеÄÃû×Ö²»ÊÇMyCProgram.cµÄÎļþ»òÕßĿ¼¡£ÓÉÓÚmaxdepthÊÇ1£¬ËùÒÔÖ»»áÏÔʾµ±Ç°Ä¿Â¼ÏµÄÎļþºÍĿ¼¡£
1 find -maxdepth 1 -not -iname "MyCProgram.c"
2 .
3 ./MybashProgram.sh
4 ./create_sample_files.sh
5 ./backup
6 ./Program.c
6. ʹÓÃinode±àºÅ²éÕÒÎļþ
ÈκÎÒ»¸öÎļþ¶¼ÓÐÒ»¸ö¶ÀÒ»ÎÞ¶þµÄinode±àºÅ£¬½è´ËÎÒÃÇ¿ÉÒÔÇø·ÖÎļþ¡£´´½¨Á½¸öÃû×ÖÏàËƵÄÎļþ£¬ÀýÈçÒ»¸öÓпոñ½á⣬һ¸öûÓС£
1 touch "test-file-name"
2
3 # touch "test-file-name "
4 [Note: There is a space at the end]
5
6 # ls -1 test*
7 test-file-name
8 test-file-name
´ÓlsµÄÊä³ö²»ÄÜÇø·ÖÄĸöÎļþÓпոñ½áβ¡£Ê¹ÓÃÑ¡Ïî-i£¬¿ÉÒÔ¿´µ½ÎļþµÄinode±àºÅ£¬½è´Ë¿ÉÒÔÇø·ÖÕâÁ½¸öÎļþ¡£
1 ls -i1 test*
2 16187429 test-file-name
3 16187430 test-file-name
Äã¿ÉÒÔÈçÏÂÃæËùʾÔÚfindÃüÁîÖÐÖ¸¶¨inode±àºÅ¡£ÔÚ´Ë£¬findÃüÁîÓÃinode±àºÅÖØÃüÃûÁËÒ»¸öÎļþ¡£
1 find -inum 16187430 -exec mv {} new-test-file-name \;
2
3 # ls -i1 *test*
4 16187430 new-test-file-name
5 16187429 test-file-name
Äã¿ÉÒÔÔÚÄãÏë¶ÔÄÇЩÏñÉÏÃæÒ»ÑùµÄÔã¸âÃüÃûµÄÎļþ×öijЩ²Ù×÷ʱʹÓÃÕâÒ»¼¼Êõ¡£ÀýÈ磬ÃûΪfile?.txtµÄÎļþÃû×ÖÖÐÓÐÒ»¸öÌØÊâ×Ö·û¡£ÈôÄãÏëÖ´ÐС°rm file?.txt¡±£¬ÏÂÃæËùʾµÄËùÓÐÈý¸öÎļþ¶¼»á±»É¾³ý¡£ËùÒÔ£¬²ÉÓÃÏÂÃæµÄ²½ÖèÀ´É¾³ý"file?.txt"Îļþ¡£
1 ls
2 file1.txt file2.txt file?.txt
ÕÒµ½Ã¿Ò»¸öÎļþµÄinode±àºÅ¡£
1 ls -i1
2 804178 file1.txt
3 804179 file2.txt
4 804180 file?.txt
ÈçÏÂËùʾ£º ʹÓÃinode±àºÅÀ´É¾³ýÄÇЩ¾ßÓÐÌØÊâ·ûºÅµÄÎļþÃû¡£
1 find -inum 804180 -exec rm {} \;
2
3 # ls
4 file1.txt file2.txt
5 [Note: The file with name "file?.txt" is now removed]
7. ¸ù¾ÝÎļþȨÏÞ²éÕÒÎļþ
ÏÂÃæµÄ²Ù×÷ʱºÏÀíµÄ£º
ÕÒµ½¾ßÓÐÖ¸¶¨È¨ÏÞµÄÎļþ
ºöÂÔÆäËûȨÏÞ룬¼ì²éÊÇ·ñºÍÖ¸¶¨È¨ÏÞÆ¥Åä
¸ù¾Ý¸ø¶¨µÄ°Ë½øÖÆ/·ûºÅ±í´ïµÄȨÏÞËÑË÷
´ËÀýÖУ¬¼ÙÉèĿ¼°üº¬ÒÔÏÂÎļþ¡£×¢ÒâÕâЩÎļþµÄȨÏÞ²»Í¬¡£
1 ls -l
2 total 0
3 -rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
4 -rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
5 ---------- 1 root root 0 2009-02-19 20:31 no_for_all
6 -rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
7 -rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
8 ----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
ÕÒµ½¾ßÓÐ×é¶ÁȨÏÞµÄÎļþ¡£Ê¹ÓÃÏÂÃæµÄÃüÁîÀ´ÕÒµ½µ±Ç°Ä¿Â¼Ï¶Ôͬ×éÓû§¾ßÓжÁȨÏÞµÄÎļþ£¬ºöÂÔ¸ÃÎļþµÄÆäËûȨÏÞ¡£
1 find . -perm -g=r -type f -exec ls -l {} \;
2 -rw-r--r-- 1 root root 0 2009-02-19 20:30 ./everybody_read
3 -rwxrwxrwx 1 root root 0 2009-02-19 20:31 ./all_for_all
4 ----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
5 -rw-r----- 1 root root 0 2009-02-19 20:27 ./others_can_also_read
ÕÒµ½¶Ô×éÓû§¾ßÓÐÖ»¶ÁȨÏÞµÄÎļþ¡£
1 find . -perm g=r -type f -exec ls -l {} \;
2 ----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
ÕÒµ½¶Ô×éÓû§¾ßÓÐÖ»¶ÁȨÏÞµÄÎļþ(ʹÓð˽øÖÆȨÏÞÐÎʽ)¡£
1 find . -perm 040 -type f -exec ls -l {} \;
2 ----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
8. ÕÒµ½homeĿ¼¼°×ÓĿ¼ÏÂËùÓеĿÕÎļþ(0×Ö½ÚÎļþ)
ÏÂÃæÃüÁîµÄÊä³öÎļþ¾ø´ó¶àÊý¶¼ÊÇËø¶¨ÎļþºÐÆäËû³ÌÐò´´½¨µÄplace hoders
1 find ~ -empty
Ö»ÁгöÄãhomeĿ¼ÀïµÄ¿ÕÎļþ¡£
1 find . -maxdepth 1 -empty
Ö»Áгöµ±ÄêĿ¼ÏµķÇÒþ²Ø¿ÕÎļþ¡£
1 find . -maxdepth 1 -empty -not -name ".*"
9. ²éÕÒ5¸ö×î´óµÄÎļþ
ÏÂÃæµÄÃüÁîÁгöµ±Ç°Ä¿Â¼¼°×ÓĿ¼ÏµÄ5¸ö×î´óµÄÎļþ¡£Õâ»áÐèÒªÒ»µãʱ¼ä£¬È¡¾öÓÚÃüÁîÐèÒª´¦ÀíµÄÎļþÊýÁ¿¡£
1 find . -type f -exec ls -s {} \; | sort -n -r | head -5
10. ²éÕÒ5¸ö×îСµÄÎļþ
·½·¨Í¬²éÕÒ5¸ö×î´óµÄÎļþÀàËÆ£¬Çø±ðÖ»ÊÇsortµÄ˳ÐòÊǽµÐò¡£
1 find . -type f -exec ls -s {} \; | sort -n | head -5
ÉÏÃæµÄÃüÁîÖУ¬ºÜ¿ÉÄÜÄã¿´µ½µÄÖ»ÊÇ¿ÕÎļþ(0×Ö½ÚÎļþ)¡£Èç´Ë£¬Äã¿ÉÒÔʹÓÃÏÂÃæµÄÃüÁîÁгö×îСµÄÎļþ£¬¶ø²»ÊÇ0×Ö½ÚÎļþ¡£
1 find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
11. ʹÓÃ-type²éÕÒÖ¸¶¨ÎļþÀàÐ͵ÄÎļþ
Ö»²éÕÒsocketÎļþ
1 find . -type s
²éÕÒËùÓеÄĿ¼
1 find . -type d
²éÕÒËùÓеÄÒ»°ãÎļþ
1 find . -type f
²éÕÒËùÓеÄÒþ²ØÎļþ
1 find . -type f -name ".*"
²éÕÒËùÓеÄÒþ²ØĿ¼
1 find -type d -name ".*"
12. ͨ¹ýºÍÆäËûÎļþ±È½ÏÐÞ¸Äʱ¼ä²éÕÒÎļþ
ÏÔʾÔÚÖ¸¶¨ÎļþÖ®ºó×ö³öÐ޸ĵÄÎļþ¡£ÏÂÃæµÄfindÃüÁÏÔʾËùÓеÄÔÚordinary_fileÖ®ºó´´½¨Ð޸ĵÄÎļþ¡£
01 ls -lrt
02 total 0
03 -rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
04 ----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
05 -rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
06 -rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
07 -rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
08 ---------- 1 root root 0 2009-02-19 20:31 no_for_all
09
10 # find -newer ordinary_file
11 .
12 ./everybody_read
13 ./all_for_all
14 ./no_for_all
13. ͨ¹ýÎļþ´óС²éÕÒÎļþ
ʹÓÃ-sizeÑ¡Ïî¿ÉÒÔͨ¹ýÎļþ´óС²éÕÒÎļþ¡£
²éÕÒ±ÈÖ¸¶¨Îļþ´óµÄÎļþ
1 find ~ -size +100M
²éÕÒ±ÈÖ¸¶¨ÎļþСµÄÎļþ
1 find ~ -size -100M
²éÕÒ·ûºÏ¸ø¶¨´óСµÄÎļþ
1 find ~ -size 100M
×¢Òâ: ¨C Ö¸±È¸ø¶¨³ß´çС£¬+ Ö¸±È¸ø¶¨³ß´ç´ó¡£Ã»ÓзûºÅ´ú±íºÍ¸ø¶¨³ß´çÍêÈ«Ò»Ñù´ó¡£
14. ¸ø³£ÓÃfind²Ù×÷È¡±ðÃû
ÈôÄã·¢ÏÖÓÐЩ¶«Î÷ºÜÓÐÓã¬Äã¿ÉÒÔ¸øËûÈ¡±ðÃû¡£²¢ÇÒÔÚÈκÎÄãÏ£ÍûµÄµØ·½Ö´ÐС£
³£ÓõÄɾ³ýa.outÎļþ¡£
1 alias rmao="find . -iname a.out -exec rm {} \;"
2 # rmao
ɾ³ýc³ÌÐò²úÉúµÄcoreÎļþ¡£
1 alias rmc="find . -iname core -exec rm {} \;"
2 # rmc
15. ÓÃfindÃüÁîɾ³ý´óÐÍ´ò°üÎļþ
ÏÂÃæµÄÃüÁîɾ³ý´óÓÚ100MµÄ*.zipÎļþ¡£
1 find / -type f -name *.zip -size +100M -exec rm -i {} \;"
ÓñðÃûrm100mɾ³ýËùÓдóÓê100MµÄ*.tarÎļþ¡£Ê¹ÓÃͬÑùµÄ˼Ïë¿ÉÒÔ´´½¨rm1g,rm2g,rm5gµÄÒ»Àà±ðÃûÀ´É¾³ýËùÓдóÓÚ1G,2G,5GµÄÎļþ¡£
1 alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
2 # alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
3 # alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
4 # alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
5
6 # rm100m
7 # rm1g
8 # rm2g
9 # rm5g