文件查找find,locate,命令传递参数xargs

发布时间:2026/7/17 11:59:14
文件查找find,locate,命令传递参数xargs 非实时查找(数据库查找)locatelocate特点:查找速度快模糊查找非实时查找搜索的是文件的全路径不仅仅是文件名locate用法-i 不区分大小写的搜索-n N 只列举前N个匹配项目-r 使用基本正则表达式搜索名称或路径中包含“conf的文件locate conf#使用Regex来搜索以“.conf结尾的文件locate -r \.conf$[rootcentos8 ~]#touch test2.log [rootcentos8 ~]#locate test2.log [rootcentos8 ~]#updatedb #更新数据库 [rootcentos8 ~]#locate test2.log /root/test2.log实时查找find1find特点查找速度略慢精确查找实时查找查找条件丰富可能只搜索用户具备读取和执行权限的目录2查找指定路径下的所有文件和子目录包括隐藏文件find /path/to/directory -name .*这将列出路径下所有隐藏文件以点号开头的文件。(3)按文件名查找使用-name选项可以根据文件名进行查找。find /path/to/directory -name file.txt查找名为file.txt的文件。支持通配符可以使用*匹配多个字符?匹配单个字符。例如find /path/to/directory -name *.txt查找所有.txt后缀的文件。(4)按文件类型查找使用-type选项可以按文件类型查找。常见的文件类型有f普通文件d目录l符号链接c字符设备文件b块设备文件例如bash复制代码find /path/to/directory -type f查找指定目录下的所有普通文件。find /path/to/directory -type d查找指定目录下的所有目录。(5)按文件大小查找使用-size选项根据文件大小查找文件。可以使用c字节默认为字节k千字节KBM兆字节MBG千兆字节GB例如查找大于 100MB 的文件find /path/to/directory -size 100M查找小于 1KB 的文件find /path/to/directory -size -1k(6)按文件修改时间查找使用-mtime选项按修改时间查找文件。mtime的单位是天支持以下格式n查找 n 天前修改的文件。-n查找 n 天内修改的文件。n查找正好在 n 天前修改的文件。例如find /path/to/directory -mtime 7查找 7 天前修改的文件。(7)按文件权限查找使用-perm选项按文件权限查找文件。例如查找权限为755的文件find /path/to/directory -perm 755查找权限为777的文件即所有人都具有读、写、执行权限find /path/to/directory -perm 777(8)按文件所有者或所属组查找使用-user和-group选项根据文件的所有者或所属组来查找文件。例如find /path/to/directory -user username查找属于username用户的文件。find /path/to/directory -group groupname查找属于groupname组的文件。(9)按文件内容查找使用-exec选项可以执行其他命令来处理查找到的文件。例如查找包含某个文本字符串的文件find /path/to/directory -type f -exec grep -l search_string {} \;其中{}表示find查找到的文件\;用于结束-exec参数。(10)查找并删除文件使用-exec选项结合rm命令删除文件find /path/to/directory -type f -name *.tmp -exec rm -f {} \;该命令会查找所有.tmp文件并删除它们。或者使用-delete选项直接删除文件不需要-execfind /path/to/directory -type f -name *.log -delete该命令会删除所有.log文件。3.组合条件find命令支持多个条件的组合使用-and默认或-or来组合条件-not用来排除某些条件。例如查找最近 7 天内修改且大小大于 10MB 的文件find /path/to/directory -mtime -7 -size 10M查找所有.txt或.md文件find /path/to/directory \( -name *.txt -or -name *.md \)注意\( \)是用来组合法条件的。4.查找并统计文件数量使用wc -l命令统计找到的文件数量find /path/to/directory -type f | wc -l5.find命令与管道结合find命令非常适合与其他命令如grep、xargs结合使用。例如查找包含特定字符串的文件并查看其内容find /path/to/directory -type f -exec grep -l pattern {} \;或者使用xargs将结果传递给其他命令find /path/to/directory -type f -name *.log | xargs cat6.排除特定目录使用-path和-prune排除某些目录find /path/to/directory -path /path/to/directory/exclude -prune -o -name *.txt -print该命令会查找.txt文件但排除/path/to/directory/exclude目录中的文件。xargs 给其他命令传递参数的一个过滤器cat test.txt | xargs 多行输入单行输出 cat test.txt | xargs -n3 -n 选项 多行输出 a b c d e f g h i j k l echo nameXnameXnameXname | xargs -dX -d 选项 可以自定义一个定界符 name name name name #用 rm 删除太多的文件时候可能得到一个错误信息/bin/rm Argument list too long. 用 xargs 去避免这个问题 find . -type f -name *.log -print0 | xargs -0 rm -f #统计一个源代码目录中所有 php 文件的行数 find . -type f -name *.php -print0 | xargs -0 wc -l #查找所有的 jpg 文件并且压缩它们 find . -type f -name *.jpg -print | xargs tar -czvf images.tar.gz #结合 -t 选项可以打印出 xargs 执行的命令 ls | xargs -t -I{} echo {} #-p 选项会在执行每一个命令时弹出确认当你需要非常准确的确认每一次操作时可以使用 -p 参数比##如查找当前目录下 .log 文件每一次删除都需要确认 find . -maxdepth 1 -name *.log | xargs -p -I{} rm {}多行输入单行输出