原创 grep 常用技能

grep 过滤文件仅显示文件名

grep -l ,仅显示匹配的文件名

-l, --files-with-matches Only the names of files containing selected lines are written to standard output. grep will only search a file until a match has been found, making searches potentially less expensive. Pathnames are listed once per file searched. If the standard input is searched, the string ``(standard input)'' is written.

如果使用 find 查找文件时需要绝对路径名,则 find 之后的路径为 绝对路径名即可。

以下是查找 /Users/xxx/book/docs 目录下的 Markdown 文件,把以 <query 开头的文件的绝对路径列出来。

find /Users/xxx/book/docs  -type f -name "*.md" -exec grep -El '^<query' {} \;
1