0
点赞
收藏
分享

微信扫一扫

【command】find、grep、正则表达式

年迈的代码机器 2022-02-15 阅读 31

fine

find [path] [options] [tests] [actions]

options:

  • depth: 在查看目录之前先搜索目录的内容;
    在这里插入图片描述

  • -follow: 跟随符号链接;

  • -maxdepth N: 最多搜索N层目录;

  • -mount: 不搜索其它文件系统中的目录;

在这里插入图片描述
tests:

在这里插入图片描述

  • -atime N: 文件在N天之前被最后访问过;
  • -mtime N: 文件在N天之前被最后修改过;
  • -name pattern: 文件名,不包括路径名;
  • -newer otherfile: 文件比 otherfile 文件要新;
  • -type c: d:目录,f:普通文件;
  • -user name: 文件的拥有者是指定的用户 username;
  1. 搜索当前目录下比文件while2要新的文件:find . -newer while2 -print,可能还会包括当前目录;
  2. 如果仅要普通文件结果:find . -newer while2 -type f print
  3. 查找以下划线开头的文件或者比while2要新的文件:find . \( -name "_*" -or -newer while2 \) -type f -print

actions:

  • exec command: 执行一条命令。这个动作必须使用\;来结束;
  • -ok command: 与上一个类似,但是会针对每个要处理的文件,提示用户进行确认;
  • -print: 打印文件名;
  • -ls: find . -newer while2 -type f -exec ls -l {} \;

grep-(General Regular Expression Parser)

使用find命令在系统中搜索文件,使用grep命令在文件中搜索字符串。

grep [options] PATTERN [FILES]

options:

  • -c: 输出匹配行的数目,而不是输出匹配的行;

  • -E: 启用扩展表达式;

  • -h: 取消每个输出行的普通前缀,即匹配查询模式的文件名;

  • -i: 忽略大小写;

  • -l: 只列出包含匹配行的文件名,而不输出真正的匹配行;

  • -v: 对匹配模式取反,即搜索不匹配行而不是匹配行;

  • grep in words.txt 查询字符 in

  • grep -c in words.txt words2.txt

  • grep -c -v in words.txt words2.txt

正则表达式

  • ^: 指向一行的开头;
  • $: 指向一行的结尾;
  • .: 任意单个字符;
  • []: 方括号内包含一个字符范围,其中任何一个字符都可以被匹配,例如字符范围a~e,或在字符范围前面加上^符号表示使用反向字符范围,即不匹配指定范围内的字符;

举报

相关推荐

0 条评论