Skip to content

Instantly share code, notes, and snippets.

@tcpdump-examples
Last active February 1, 2022 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcpdump-examples/f44c04105a85ade9d89407edc83436ef to your computer and use it in GitHub Desktop.
Save tcpdump-examples/f44c04105a85ade9d89407edc83436ef to your computer and use it in GitHub Desktop.

find命令特点:文件查找,实时查找,速度略慢,精确匹配

使用格式:# find [options] [查找路径] [查找条件] [处理动作]

查找路径:默认为当前目录

查找条件:默认为查找指定路径下的所有文件

处理动作:默认为显示

· 查找/tmp目录下文件名为passwd的文件:# find /tmp -name”passwd”

· 查找/tmp目录下文件名以passwd开头的文件,passwd不区分大小写:# find /tmp -iname “passwd*”

· 查找/tmp目录下属主为linuxmi的文件:# find /tmp -user linuxmi

· 查找/tmp目录下属组为linuxmi的文件:# find /tmp -grouplinuxmi

· 查找/tmp目录下UID为502的文件:# find /tmp -uid 502

· 查找/tmp目录下GID为502的文件:# find /tmp -gid 502

一个普通用户例如linuxmi创建了/tmp/linuxmi.txt,该文件属主和属组都是linuxmi,但当keyso用户被删除之后,/tmp/linuxmi.txt文件的属主和属组会发生变化,此种情况无法再根据属主为linuxmi这个被删除用户查找到/tmp/linuxmi.txt文件

· 查找/tmp目录下没有属主的文件:# find /tmp -nouser

· 查找/tmp目录下没有属组的文件:# find /tmp -nogroup

· 查找/tmp目录下的目录:# find /tmp -type d

-type文件类型包括:f(普通文件)、d(目录)、b(块设备)、c(字符设备)、l(符号链接文件)、p(命名管道)和s(套接字)等

· 查找/tmp目录下大于100M的文件:# find /tmp -size +100M

-size:根据文件大小查找,-size [+ | -]#[k | M | G],+(大于),-(小于),没有+和-,就是精确匹配

how to find file in Linux #实际的取值范围:(#-1)<x<=#

· 查找/tmp目录下一天之内被访问过的文件:# find /tmp -atime -1

格式:-[a | m | c][time | min] [+ | -]#

根据time时间戳查找(以天为单位):

-atime:访问时间,就是一个文件最后一次被访问的时间

-mtime:修改时间,就是文件的内容最后被修改的时间

-ctime:改变时间,就是文件或者目录的属性(属主、属组、权限等等)被改变的时间

根据min时间戳查找(以分钟为单位):

-amin:意义同-atime

-mmin:意义同-mtime

-cmin:意义同-ctime

+#:表示(#+1)天之外被访问过

-#:表示#天之内被访问过

#:表示在#<=x<(#+1)天的时间段内被访问过

· 查找/tmp目录下权限为644的文件:# find /tmp -perm 644

根据权限查找,格式:-perm [+ | -]MODE

+MODE:任何一类用户的任何一位权限匹配,常用于查找某类用户的某特定权限是否存在

-MODE:每类用户的指定要检查的权限位都要匹配

MODE:精确匹配所给的权限

示例: //6=4+2=r+w

Find exec examples · 查找/tmp目录下权限为666的文件(精确匹配):# find /tmp -perm 666

· 查找/tmp目录下其他用户有读写权限的文件:# find /tmp -perm -006

· 查找/tmp目录下其他用户有读或者写权限的文件:# find /tmp -perm +006

· 查找/tmp目录下至少有一类用户有写权限的文件:# find /tmp -perm +222

· 查找/tmp目录下所有用户都有写权限的文件:# find /tmp -perm -222

· 查找/tmp目录下所有用户都没有写权限的文件:# find /tmp -not -perm +222

//“所有用户都没有”和“只要有一类用户有”意思相反

//只确定不能有写权限,并不能确定有没有读和执行权限

· 查找/tmp目录下至少有一类用户没有写权限的文件:# find /tmp -not -perm -222

//“至少有一类用户没有”和“所有用户都有”意思相反

· 查找/tmp目录下属组或者其他用户有读写权限的文件:# find /tmp -perm -060 -o -perm -006

· 查找/tmp目录下属组和其他用户都有读写权限的文件:# find /tmp -perm -066

· 查找/tmp目录下属组有读或者写权限,或者其他用户有读或者写权限的文件(4选1):

find /tmp -perm +066

· 查找/tmp目录下的普通文件,并删除:# find /tmp -type f -exec rm -rf {} ;

· 查找/tmp目录下大于10M的文件,并以长格式显示文件信息:# find /tmp -size +10M -ls

· 查找/tmp目录下以.doc结尾的文件,并重命名为.docx:# find /tmp -iname “*.doc” -exec mv {} {}x ;

处理动作:

-ls:以长格式显示文件信息

-exec COMMAND {} ; –> 对查找到的文件执行指定的命令,{}为占位符,指代find查找到的文件

-ok COMMAND {} ; –> 与-exec COMMAND {} ;类似,交互式的-exec

|xargs COMMAND:对查找到的文件执行指定的命令,功能与-exec COMMAND {} ;类似

备注:-exec与xargs的区别

-exec:find将查找到的所有文件一次性全部传递给-exec所指定的命令,容易出现溢出错误。-exec为处理每一个匹配到的文件而发起一个相应的进程,会导致进程过多,系统性能下降

xargs:对find传递给xargs所指定的命令的文件,每次只获取一部分而不是全部,不会出现溢出错误。另外xargs只会发起一个进程,不会导致系统性能下降

· 查找/tmp目录下没有属主或属组,且最近1个月内曾被访问过的文件:

find /tmp ( -nouser -o -nogroup ) -a -atime -30 //括号内侧的两端都需要加空格

组合条件:

-a:与,同时满足,如果组合条件中只有-a,可以省略 –> # find /tmp -user hadoop [-a] -name “*.txt”

-o:或,只需满足其一即可,优先级比-a低,如果需要先执行-o条件,需要加(),()需要转义

-not | !:非,取反

非A且非B <==> 非(A或B) -not A -a -not B <==> -not ( A -o B )

非A或非B <==> 非(A且B) -not A -o -not B <==> -not ( A -a B )

how to find file by name in Linux

find files in linux with find command examples

linux find depth maxdepth mindepth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment