1. find命令

    Find命令用来搜索Linux中的文件和文件夹。根据搜索条件递归地遍历其子目录。它允许用户根据大小、名称、所有者、组、类型、权限、日期和其他条件执行所有类型的文件搜索

    # 文件或者目录
    find -name "文件名"
    # 忽略大小写查找
    find -iname "文件名"
    find / -iname "文件名"
    # 筛选
    find /home/app -iname "*.conf" | less
    # 通过扩展名查找
    find -name "*.txt"
    # 通过文件类型查找
    find . -type f -name "*.txt"
    find . -type d -name "tt"
    # 查找1M以内的文件
    find . -type f -size 1M 
    # 通过文件权限查询
    find . -type f -perm 1M
    # 查找空文件以及空目录
    find . -empty
    # 查找特定内容的文件
    find . -type f -exec grep "Hello" '{}' \; -print
    find . -type f -exec grep -H 'Hello' {} \;
    find . -type f -print | xargs grep "Hello"
    find . -type f | xargs grep 'Hello'
    
  2. grep命令

    # 搜索某个文件里面是否包含字符串
    grep "0101034175" /data/transaction.20170118.log
    grep "被查找的字符串t" filename1 filename2 filename3 ...
    grep "被查找的字符串" *.log
    # 显示行号
    grep -n "被查找的字符串" *.log
    # 忽略大小写
    grep -i "被查找的字符串" *.log
    # 查找不匹配指定字符串的行
    grep –v "被查找的字符串" 文件名
    # 递归搜索某个目录以及子目录下的所有文件
    grep –r "被查找的字符串" 文件目录
    # 查找哪些文件包含搜索的内容,并列出文件名
    grep -H –r "被查找的字符串" 文件目录 | cut -d: -f1 [| uniq]
    # 搜索字符完全匹配的内容
    grep –w "被查找的字符串" 文件名
    # 搜索、查找匹配的行数
    grep –c "被查找的字符串" 文件名
    grep "被查找的字符串" 文件名 | wc -l
    
    find . -name '*.sql' -exec grep -i '被检索内容 ' {} \; -print
    
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐