Linux find 命令参数大全及示例

Linux中find常见用法示例

命令格式:find path -option [-print] [ -exec -ok command] {} ;
  参数说明:
    path:find命令所查找的目录路径。例如用"."来表示当前目录,用"/"来表示系统根目录。注:path 参数可以省略,在当前目录查询
    -print:find命令将匹配的文件输出到标准输出。
    -exec:find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;,注意{ }和;之间的空格。
    -ok:和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
  例:find . -name .svn | xargs rm -rf #删除当前目录下面文件名包含".svn"的文件

  option参数:
    

    -name filename                    #查找名为filename的文件
    -perm                          #按执行权限来查找
    -user username                    #按文件属主来查找
    -nouser                         #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -group groupname                    #按组来查找
    -nogroup                        #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -mtime -n +n                      #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
    -atime -n +n                     #按文件访问时间来查找文件
    -ctime -n +n                     #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
    -newer f1 !f2                     #找文件,f1天以内,f2天以前 
    -type b/d/c/p/l/f                  #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size n[c]                       #查长度为n块[或n字节]的文件
    -depth                          #使查找在进入子目录前先行查找完本目录
    -fstype                         #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
    -mount                          #查文件时不跨越文件系统mount点
    -follow                         #如果遇到符号链接文件,就跟踪链接所指的文件
    -cpio                           #对匹配的文件使用cpio命令,将他们备份到磁带设备中
    -prune                           #忽略某个目录

查询示例:

  $find ~ -name "*.txt" -print               #在$HOME中查.txt文件并显示
  $find . -name "*.txt" -print                #在当前目录下查.txt文件并显示
  $find . -name "[A-Z]*" -print              #查以大写字母开头的文件
  $find /etc -name "host*" -print              #查以host开头的文件
  $find . -name "[a-z][a-z][0–9][0–9].txt" -print     #查以两个小写字母和两个数字开头的txt文件
  $find . -perm 755 -print                   #查所有用户都可读写执行的文件
  $find . -perm -007 -exec ls -l {} ;          #查所有用户都可读写执行的文件同-perm 777
  $find . -type d -print                    #查当前目录下所有文件夹
  $find . ! -type d -print                  #查当前目录下所有非文件夹
  $find . -type l -print                    #查当前目录下所有链接

  $find . -size +1000000c -print              #查长度大于1Mb的文件
  $find . -size 1000c -print                 #查长度为1000c的文件
  $find . -size +10 -print                  #查长度超过10块的文件(1块=512字节)

  $find etc home apps -depth -print|cpio -ivcdC65536 -o /dev/rmt0       #备份etc home apps数据
  $find /etc -name "passwd*" -exec grep "admin" {} ;             #看是否存在admin用户
  $find . -name "*.conf" | xargs file                         #查文件名后缀包含.conf的文件,输出到文件
  $find . -name "*.conf" | xargs echo "" > /tmp/core.log            #查文件名后缀包含.conf的文件,输出到文件
  $find . -name "*.conf" | xargs chmod o-w                       #查文件名后缀包含.conf的文件,赋予只写权限

  $find -name conf*                       #在当前目录下查找以conf开始的文件
  $find -name conf* fprint file                #在当前目录下查找以conf开始的文件,并把结果输出到file中
  $find -name conf* -o -name config*             #查找以conf或config开头的文件
  $find /mnt -name conf.txt -ftype vfat            #在/mnt下查找名称为conf.txt且文件系统类型为vfat的文件
  $find /mnt -name conf.txt ! -ftype vfat          #在/mnt下查找名称为conf.txt且文件系统类型不为vfat的文件
  $find /tmp -name conf* -type l                #在/tmp下查找名为conf开头且类型为符号链接的文件
  $find /home -mtime -2                     #在/home下查最近两天内改动过的文件
  $find /home -atime -1                     #查1天之内被存取过的文件
  $find /home -mmin +60                     #在/home下查60分钟前改动过的文件
  $find /home -amin +30                     #在/home下查最近30分钟前被存取过的文件
  $find /home -newer tmp.txt                  #在/home下查更新时间比tmp.txt近的文件或目录
  $find /home -anewer tmp.txt                  #在/home下查存取时间比tmp.txt近的文件或目录
  $find /home -used -2                       #列出文件或目录被改动过之后,在2日内被存取过的文件或目录
  $find /home -user admin                    #列出/home目录内属于用户admin的文件或目录
  $find /home -uid +501                      #列出/home目录内用户的识别码大于501的文件或目录
  $find /home -group admin                    #列出/home内组为admin的文件或目录
  $find /home -gid 501                       #列出/home内组id为501的文件或目录
  $find /home -nouser                       #列出/home内不属于本地用户的文件或目录
  $find /home -nogroup                       #列出/home内不属于本地组的文件或目录
  $find /home -name tmp.txt -maxdepth 4             #列出/home内的tmp.txt,查时深度最多为3层
  $find /home -name tmp.txt -mindepth 3             #列出/home内的tmp.txt,从第2层开始查
  $find /home -name tmp.txt -exec cat {} ;            #打印/home内的tmp.txt文件内容
  $find /home -name tmp.txt -ok rm {} ;              #删除/home内的tmp.txt文件
  $find /home -size +512k                      #查大于512k的文件
  $find /home -size -512k                      #查小于512k的文件
  $find /home -links +2                       #查硬连接数大于2的文件或目录
  $find /home -perm 0700                       #查权限为700的文件或目录

  $find / -amin -10                         #查找在系统中最后10分钟访问的文件
  $find / -atime -2                         #查找在系统中最后48小时访问的文件
  $find / -empty                           #查找在系统中为空的文件或者文件夹
  $find / -group project                      #查找在系统中属于groupproject的文件
  $find / -mmin -5                          #查找在系统中最后5分钟里修改过的文件
  $find / -mtime -1                          #查找在系统中最后24小时里修改过的文件
  $find / -user admin                         #查找在系统中属于admin用户的文件

  $find . -type f -exec ls -l {} ;                 #查当前目录下的所有普通文件
  $find ./logs -type f -mtime +5 -exec -ok rm {} ;       #查找更改时间在5日以前的日志文件并删除
  $find ./ -mtime -1 -type f -exec ls -l {} ;            #查询当天修改过的文件
  $find ./ -mtime -1 -type f -ok ls -l {} ;            #查询文件并询问是否要显示

实例

1)在./conf中查找所有的*.conf,并在这些文件中查找“DATABASE",最后打印出所有包含"DATABASE"的文件名

  A) find ./conf -name "*.h" | xargs -n50 grep DATABASE
  B) grep DATABASE /conf/*.h | cut -d’:' -f1| uniq > filename
  C) find /conf -name "*.h" -exec grep "DATABASE" {} ; -print

2)删除指定文件名文件

  $find / -name filename -exec rm -rf {} ;
  $find / -name filename -ok rm -rf {} ;

3)查找磁盘中大于10M的文件:

  $find . -size +10000k -exec ls -ld {} ;

4)将find出来的文件备份

  $find *.conf -exec cp ‘{}’ ./backup ‘;’

  如果有特殊文件,可以用cpio:
  $find dir -name filename -print | cpio -pdv newdir

原文地址:https://www.cnblogs.com/zengming/p/10280027.html