zoukankan      html  css  js  c++  java
  • linux学习:find用法整理

    find path -option [ -print ] [ -exec -ok command ] {} ;

    path: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
    -print 将查找到的文件输出到标准输出
    -exec command {} ; 将查到的文件执行command操作,{} 和 ;之间有空格;{}标识匹配到的文件路径或文件名
    -ok 和-exec相同,只不过在操作前要询用户
    ===========================  option  ====================
    -name filename  #查找名为filename的文件
    -perm          #按执行权限来查找
    -user username    #按文件属主来查找
    -group groupname   #按组来查找
    -mtime -n +n   #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
    -atime -n +n    #按文件访问时间来查GIN: 0px">
    -ctime -n +n    #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
    -nogroup    #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser    #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -newer f1 !f2    #找文件,-n指n天以内,+n指n天以前
    -ctime -n +n    #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
    -nogroup    #查无有效属组的文件,即文件的属组在/etc/groups中不存在
    -nouser    #查无有效属主的文件,即文件的属主在/etc/passwd中不存
    -newer f1 !f2     #查更改时间比f1新但比f2旧的文件
    -type b/d/c/p/l/f  #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size n[c]  #查长度为n块[或n字节]的文件
    -depth    #使查找在进入子目录前先行查找完本目录
    -fstype    #查更改时间比f1新但比f2旧的文件
    -type b/d/c/p/l/f    #查是块设备、目录、字符设备、管道、符号链接、普通文件
    -size n[c]   #查长度为n块[或n字节]的文件
    -depth    #使查找在进入子目录前先行查找完本目录
    -fstype    #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
    -mount   #查文件时不跨越文件系统mount点
    -follow    #如果遇到符号链接文件,就跟踪链接所指的文件
    -cpio %;   #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
    -mount   #查文件时不跨越文件系统mount点
    -follow    #如果遇到符号链接文件,就跟踪链接所指的文件
    -cpio      #对匹配的文件使用cpio命令,将他们备份到磁带设备中
    -prune   #忽略某个目录

    -maxdepth 1  #只查找当前目录,不进行递归

    -maxdepth 2  #只查找当前目录和子目录

    -mindepth 1  #查找当前目录及子目录
    ========================================================

    以下总结一些实例:

    基于文件类型:
    find . -type d -print     #只列出当前目录所有的子目录
    find . ! -type d -print  #只列出当前目录的非子目录(文件)
    find . -type f -print     #只列出当前目录所有的文件
    find . -type l -print     #只列出当前目录的所有符号链接
    find . -type c -print    #只列出当前目录的所有字符设备
    find . -type b -print    #只列出当前目录的所有块设备
    find . -type s -print    #只列出当前目录的所有套接字
    find . -type p -print    #只列出当前目录的所有Fifo

    基于路径或文件名:
    find . -name ap* -o -name may*             #查找以ap或may开头的文件
    find . -name "*.txt" -print               #在当前目录中中查.txt文件并显示
    find . -name "[A-Z]*" -print              #在当前目录中查以大写字母开头的文件并显示
    find /etc -name "host*" -print             #在/etc目录中查以host开头的文件并显示
    find . -name "[a-z][a-z][0–9][0–9].txt" -print    #查以两个小写字母和两个数字开头的txt文件
    find . !-name "*.txt" -print             #列出所有不以.txt结尾的文件名
    find /home/user -path "*ttt*" -print         #-path将文件路径和文件名当做一个整体来匹配,所以文件路径或者文件名中含有ttt的都会被匹配到
    find . -name "*.txt" -print              #匹配当前目录下所有.txt文件,并输出其路径
    find . -iname "example*" -print            #匹配当前目录下以example开头的文件名,不区分大小写,并输出其路径
    find . ( -name "*.txt" -o -name "*.pdf" ) -print  #匹配当前目录下所有.txt文件和.pdf文件,并输出其路径
    find . -type f -name "*.swp" -delete         #删除当前目录下所有的.swp文件
    find . -type f -name "*.c" -exec cat {} ;>all_c_files.txt  #找到所有C文件并拼接起来写入单个文件all_c_files.txt。find命令的输出只是一个单数据流,所以不用>>进行追加
    find /mnt -name tom.txt -ftype vfat       #在/mnt下查找名称为tom.txt且文件系统类型为vfat的文件
    find /mnt -name tom.txt ! -ftype vfat       #在/mnt下查找名称为tom.txt且文件系统类型不为vfat的文件

    基于用户文件权限:
    find . -perm 755 -print              #查当前目录下下权限为755的文件
    find / -group cat                  #查找在系统中属于cat组的目录和文件
    find / -nouser                    #查找在系统中属于作废用户的文件
    find / -user fred                   #查找在系统中属于fred这个用户的文件
    find . -type f -user ubuntu -print            #列出当前目录下属于用户ubuntu的文件
    find . -type f -name "*.php" | -perm 644 -print    #列出当前目录下所有权限为644的php文件
    find /home -uid +501                #列出/home目录内用户的识别码大于501的文件或目录
    find /home -gid 501                 #列出/home内组id为501的文件或目录
    find /home -nouser                  #列出/home内不属于本地用户的文件或目录
    find /home -nogroup                  #列出/home内不属于本地组的文件或目录

    基于目录或文件大小:

    find . -name "*" -type f -size 0c | xargs -n 1 rm -f    #批量删除空文件(大小等于0的文件)的方法
    find . -type f -empty            #查找大小为0的文件或空目录
    find . -type f -size +1000000c -print    #查长度大于1Mb的文件
    find . -type f -size 100c -print      # 查长度为100c的文件
    find . -type f -size +10 -print       #查长度超过期作废10块的文件(1块=512字节)
    find . -type f -size +2k            #搜索当前目录下大于2KB的文件
    find . -type f -size -2k          #搜索当前目录下小于2KB的文件
    除了千字节KB(k)之外,还有块(b 512字节),字节(c),字(w),兆字节(M),吉字节(G)

    基于操作时间:
    find / -type f -amin -10            #查找在系统中最后10分钟访问的文件
    find / -type f -atime -2             #查找在系统中最后48小时访问的文件
    find / -type f -empty            #查找在系统中为空的文件或者文件夹
    find / -type f -mmin -5             #查找在系统中最后5分钟里修改过的文件
    find / -type f -mtime -1            #查找在系统中最后24小时里修改过的文件
    find /home -type f -mtime -2       #在/home下查最近两天内改动过的文件
    find /home -type f -atime -1        #在/home下查1天之内被存取过的文件
    find /home -type f -mmin +60       #在/home下查60分钟前改动过的文件
    find /home -type f -amin +30       #在/home下查最近30分钟前被存取过的文件
    find . -type f -atime -7 -print        #列出当前目录在最近7天内被访问过的所有文件
    find .-type f -atime 7 -print        #列出当前目录恰好在第七天前被访问过的所有文件
    find . -type f -atime +7 -print        #列出当前目录访问时间超过七天的所有文件
    find . -type f -newer file.txt -print        #找出当前目录比file.txt修改时间更长的所有文件,使用-newer,可以指定一个用于比较的时间戳参考文件
    find /home -type f -anewer tmp.txt      #在/home下查存取时间比tmp.txt近的文件或目录
    find /home -used -2              #列出文件或目录被改动过之后,在2日内被存取过的文件或目录
    find ./ -type f -mtime -1 -exec ls -l {}    #查询当前目录下当天修改过的文件的详细信息
    find ./ -type f -mtime -1 -ok ls -l {}       #查询当前目录下当天修改过的文件的详细信息并询问是否显示
    (-atime是根据访问时间;-mtime是根据修改时间;-ctime是根据变化时间。三个参数的单位都是天)
    (-amin是根据访问时间;-mmin是根据修改时间;-cmin是根据变化时间。这三个参数的单位是分钟)

    find与-exec参数:
    find . -perm -007 -exec ls -l {} ;                #查所有用户都可读写执行的文件同-perm 777
    find . -type f -user root -exec chown ubuntu {} ;           #将当前目录下所有root的文件改为属于ubuntu,此处{}会替换成每一个匹配的文件名,{}表示匹配,与-exec结合使用
    find . -type f -name "*.c" -exec cat {} ;>all_c_files.txt     #找到所有C文件并拼接起来写入单个文件all_c_files.txt。find命令的输出只是一个单数据流,所以不用>>进行追加
    find . -type f -atime +10 -name "*.txt" -exec cp {} /data ;     #找到10天前访问的.txt文件并复制到/data目录中
    find ./log02* -exec mv {} /data/game ;              #找到当前目录下以log02开头的文件并将其移动到/data/game目录下
    find . -type f -mtime +5 -exec -ok rm {}            #在当前目录中查找更改时间在5日以前的文件并删除它们
    find . -type f -name "*.txt" -exec printf "Text file:%s " {} ;   #-exec结合printf输出信息
    (find 的-exec参数可以接其他任何命令,具体看需求是怎样的)

    find 与 xargs 组合:

    find . -type d -empty | xargs rmdir              #删除当前目录下所有空文件夹

    find . -type f -empty | xargs rm -rf     #递归查找当前目录及子目录下所有空文件并删除,rm 的 -r参数表示递归,-f表示强制删除

    find . -name "*.txt" | xargs rm -rf                #查找当前目录下所有.txt文件并删除

    find . -type f -name "*.txt" -print0 | xargs -0 rm -f        #匹配并删除所有的.txt文件,xargs -0将作为输入定界符
    find /data -type f -name "*.c" -print0 | xargs -0 wc -l       #统计/data目录下所有C文件的行数
    find ./ -name "test*.log" | xargs grep "[AAAA]" | wc –l      #通过第一个管道find查找当前目录下所有以test开头的log文件的内容,并将内容输送到第二个管道xargs grep,过滤并查找出所有含有”[AAAA]”的行,然后再送给wc命令进行统计行数
    find ./ -name "test*.log" | xargs grep "[AAAA]" > re.txt    #同上例,将查找到的行重定向到re.txt文件
    find ./ -name "test*.log" | xargs grep "[AAAA]" | grep ""SVR":1" >re.log                    #同上例,当需要匹配多个字段时:例如此时匹配[AAAA]和 ”SVR”:1 两个字段
    find ./ -name "test*.log" | xargs grep "[AAAA]" | grep ""SVR":1" | grep "[2016-12-27 10:01:59]" > re.log   #同上例,多匹配一个[Y-m-d H:i:s]格式的时间字段
    (xargs命令应该紧跟在管道操作符后面,以标准输入作为主要的源数据流。)

    find /data/logdir -maxdepth 1 -name "test*.log" |  xargs -I file mv file ./test   #查找 /data/logdir 目录中(不递归)所有test开头的log文件,并且移动到当前目录的test子目录中

    其他:
    find . -iregex ".*(.py|.sh)$"               #-iregex忽略正则表达式的大小写 此处为忽略后缀的大小写
    find . -maxdepth 2 -type f -print               #遍历的最大深度距离此目录最多为2层子目录,列出所有普通文件
    find . -mindepth 2 -type f -print                #遍历的深度距离当前目录至少两个子目录,列出所有文件
    find /data ( -name ".git" -prune ) -o ( -type f -print )      #在/data目录下搜索所有文件,搜索时跳过.git子目录。 ( -name ".git" -prune )这里用于排除.git目录
    find /etc -name "passwd*" -exec grep "cnscn" {}       #看是否存在cnscn用户
    find . -type f -name april* fprint file               #在当前目录下查找以april开始的文件,并把结果输出到file中
    find . -links +2                         #查硬连接数大于2的文件或目录

    其他查找的命令:

    whereis:用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。例如:whereis php、whereis grep...

    which :在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。检查系统命令是否存在,以及执行的到底是哪一个位置的命令。例如:which php、which grep...

    locate:是“find -name”的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。

      locate /etc/sh    #搜索etc目录下所有以sh开头的文件。

      locate ~/m       #搜索用户主目录下,所有以m开头的文件。

      locate -i ~/m   #搜索用户主目录下,所有以m开头的文件,并且忽略大小写。

  • 相关阅读:
    Java 简单算法--打印乘法口诀(只使用一次循环)
    Java简单算法--求100以内素数
    ubuntu 16.04 chrome flash player 过期
    java 网络API访问 web 站点
    java scoket (UDP通信模型)简易聊天室
    leetcode1105 Filling Bookcase Shelves
    leetcode1140 Stone Game II
    leetcode1186 Maximum Subarray Sum with One Deletion
    leetcode31 Next Permutation
    leetcode834 Sum of Distances in Tree
  • 原文地址:https://www.cnblogs.com/LO-gin/p/6835494.html
Copyright © 2011-2022 走看看