Home | Mirror | Search

14. find - search for files in a directory hierarchy

14.1. name

Find every file under directory /usr ending in "stat".

$ find /usr -name *stat
/usr/src/linux-headers-2.6.24-22-generic/include/config/cpu/freq/stat
/usr/bin/lnstat
/usr/bin/sar.sysstat
/usr/bin/mpstat
/usr/bin/rtstat
/usr/bin/nstat
/usr/bin/lpstat
/usr/bin/ctstat
/usr/bin/stat
/usr/bin/kpsestat
/usr/bin/pidstat
/usr/bin/iostat
/usr/bin/vmstat
/usr/lib/sysstat
/usr/share/doc/sysstat
/usr/share/gnome/help/battstat
/usr/share/omf/battstat
/usr/share/zsh/help/stat
/usr/share/zsh/4.3.4/functions/Completion/Unix/_diffstat
/usr/share/zsh/4.3.4/functions/Completion/Zsh/_stat
/usr/share/zsh/4.3.4/functions/Zftp/zfstat
		
find  \( -iname '*.jpg' -o -iname '*.png' -o -iname '*.gif'  \)

find /www/images -type f \( -iname '*.js' -o -iname '*.css' -o -iname '*.html' \) | xargs tar -czf ~/images.tgz
		

14.2. regex

find . -regex ".*\.\(jpg\|png\)"
		

下面regex與name作用相同

find . -regex ".*\.\(txt\|sh\)"
find . -name "*.sh" -o -name "*.txt"
		

14.3. user

Find every file under /home and /var/www owned by the user neo.

$ find /home -user neo
$ find /var/www -user neo
$ find . -user nobody -iname '*.php'
		

14.4. perm

find ./ -perm -7 -print | xargs chmod o-w
find . -perm -o=w
		

14.5. type

14.5.1. 分別設置檔案與目錄的權限
find /usr/www/phpmyadmin -type d -exec chmod 755 {} \;
find /usr/www/phpmyadmin -type f -exec chmod 644 {} \;
			

14.6. -delete

# find /var/spool/clientmqueue/ -type f -delete
		

保留最近7天的問題,其他全部刪除

find . -type f -mtime +7 -delete
		

14.7. exec

替換文本

# find ./ -exec grep str1 ‘{}’ \; -exec sed -i.bak s/str1/str2/g ‘{}’ \;
		
find -exec ls -l {} \; | grep '2011-01-18'
		

14.8. xargs

find /etc -type f|xargs md5sum
		

sha1sum

find /etc -type f|xargs sha1sum
		
find ./ -name "*html" | xargs -n 1 sed -i -e 's/aaa/bbb/g'
		

find /tmp -name core -type f -print | xargs /bin/rm -f
find . -type f -exec file '{}' \;
		

14.9. 排除目錄

find /usr/local -path "/usr/local/share" -prune -o -print

find /usr/local \( -path /usr/local/bin -o -path /usr/local/sbin \) -prune -o -print

find /usr/local \(-path /usr/local/dir1 -o -path /usr/local/file1 \) -prune -o -name "temp" -print
		

查找當前目錄下的php檔案,排除子目錄templates_c, caches

find . \( -path ./templates_c -o -path ./caches \) -prune -o -name "*.php" -print
		

14.10. -mtime / -mmin

查詢最近3天前內修改的檔案

find . -type f -mtime -3
		

3天前

find . -type f -mtime +3
		
# find . -mmin +5 -mmin -10
		

例 22.1. backup(find + tar)

find / -type f -mtime -7 | xargs tar -rf weekly_incremental.tar
gzip weekly_incremental.tar
			

14.11. --newer

tar --newer="2011-07-04" -zcvf backup.tar.gz /var/www/
tar cvzf foo.tgz /bak -N "2004-03-03 16:49:17"
		

14.12. -print / -printf

[root@scientific ~]# find / -maxdepth 1 -name '[!.]*' -printf 'Name: %16f Size: %6s\n'
Name:                / Size:   4096
Name:             misc Size:      0
Name:            media Size:   4096
Name:             home Size:   4096
Name:              dev Size:   3840
Name:              net Size:      0
Name:             proc Size:      0
Name:             sbin Size:  12288
Name:             root Size:   4096
Name:              lib Size:   4096
Name:           cgroup Size:   4096
Name:              srv Size:   4096
Name:              mnt Size:   4096
Name:              etc Size:  12288
Name:              usr Size:   4096
Name:            lib64 Size:  12288
Name:             boot Size:   1024
Name:              var Size:   4096
Name:          selinux Size:      0
Name:              opt Size:   4096
Name:              tmp Size:   4096
Name:       lost+found Size:  16384
Name:              sys Size:      0
Name:              bin Size:   4096

# find /etc/ -type f -printf "%CY-%Cm-%Cd %Cr %8s %f\n"
		
comments powered by Disqus