Linux Find 指令操作指南 (find command cheatsheet)

By Long Luo

find 指令是 Unix/Linux 系统中很常用的指令之一,在日常开发维护中常常使用到这个工具。find 是一个很有用的指令,它支援非常多的查找选项,可以依照权限、拥有者、群组、文件类型、日期与大小等条件来查找,这里整理了一些常用的 find 指令的使用技巧。

find –help Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path…] [expression]

default path is the current directory; default expression is -print expression may consist of: operators, options, tests, and actions: operators (decreasing precedence; -and is implicit where no others are given): ( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2 EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2 positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions): -depth –help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf –version -xdev -ignore_readdir_race -noignore_readdir_race tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N -cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME -ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN -links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN -readable -writable -executable -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N -used N -user NAME -xtype [bcdpfls] -context CONTEXT

actions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print -fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit -exec COMMAND ; -exec COMMAND {} + -ok COMMAND ; -execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;

Valid arguments for -D: exec, opt, rates, search, stat, time, tree, all, help Use ‘-D help’ for a description of the options, or see find(1)

Please see also the documentation at https://www.gnu.org/software/findutils/. You can report (and track progress on fixing) bugs in the “find” program via the GNU findutils bug-reporting page at https://savannah.gnu.org/bugs/?group=findutils or, if you have no web access, by sending email to .

  1. Find files by name

$ find /home/user/documents -name “example.txt”

  1. Find files by extension

$ find /var/log -name “*.log”

$ find /etc -mtime -7

$ find /usr/local -mtime +30

$ find /tmp -name “oldfile.txt” -delete

$ find /var/www -empty

$ find /home/user/downloads -size +100M

$ find /home -user username

$ find /etc -perm 0644

$ find /var/log -name “*.log” -exec {} ;

$ find /home/user/documents -type f -empty -exec {} ;

$ find /home/user/documents -type f -exec {} ;

$ find / -path “/proc” -prune -o -name “*.conf -print

$ find /var/www -mmin -60

$ find /home/user/pictures -name “*.jpg” | xargs tar -czvf archive.tar.gz

$ find /usr/bin -type I

  1. Find Files by Inode Number

$ find / -inum 456332

$ find /home/user -not -name “*.txt”

$ find /var/log -group syslog

$ find /home/user/downloads -size +50M -size -100M

$ find /var/log -type f -exec s {} +

$ find /var/log -mmin -120

$ find /home -user username -group groupname

$ find /var/log -perm 600

$ find /var/log -size +1G -exec {} ;

$ find /home/user -maxdepth -name “*txt”

$ find /var/log -atime +90

$ find /home/user -name “.*”

$ find /home/user -ctime +1

$ find /dev -type b

$ find / -perm /a=r -not -perm /a=w

$ find /home/user -name “config

参考文献

  1. find (Unix)
  2. find (Windows)
  3. findstr