需要 grep 和 find 组合的别名
Alias for a combination of grep and find is needed
很多时候我需要从一个目录及以下目录中搜索具有特定类型的所有文件中的模式。例如,我需要要求 grep 不要查看 *.h、*.cpp 或 *.c 以外的文件。但是如果我输入:
grep -r pattern .
它查看所有文件。如果我输入:
grep -r pattern *.c
它会尝试当前文件夹中的 *.c 文件(在我的例子中没有文件)和 *.c 文件夹中的文件(在我的例子中没有文件夹)。我也想问它查看所有文件夹,但只查看给定类型的文件。我认为 grep 不足以用于此目的。所以,我也从 find 得到帮助,像这样:
grep pattern `find . -name '*c'`
首先,让我知道我从 find 获得帮助的说法是否正确。 grep 够用吗?其次,我更喜欢为 bash 写一个别名,这样使用:
mygrep pattern c
将被翻译成相同的命令,避免使用 ` 和 ' 并且更简单。我试过了:
alias mygrep="grep `find . -name '*'`"
但它不起作用并发出错误:
grep: c: No such file or directory
我试过改,改不了成功的别名
有什么想法吗?
作为 find
到 grep
的 function than an alias, and using -exec
instead of passing the output 会更好。该输出将受到分词和通配的影响,因此可能会产生令人惊讶的结果。而是尝试:
mygrep () {
find . -name "*" -exec grep "" {} +
}
很多时候我需要从一个目录及以下目录中搜索具有特定类型的所有文件中的模式。例如,我需要要求 grep 不要查看 *.h、*.cpp 或 *.c 以外的文件。但是如果我输入:
grep -r pattern .
它查看所有文件。如果我输入:
grep -r pattern *.c
它会尝试当前文件夹中的 *.c 文件(在我的例子中没有文件)和 *.c 文件夹中的文件(在我的例子中没有文件夹)。我也想问它查看所有文件夹,但只查看给定类型的文件。我认为 grep 不足以用于此目的。所以,我也从 find 得到帮助,像这样:
grep pattern `find . -name '*c'`
首先,让我知道我从 find 获得帮助的说法是否正确。 grep 够用吗?其次,我更喜欢为 bash 写一个别名,这样使用:
mygrep pattern c
将被翻译成相同的命令,避免使用 ` 和 ' 并且更简单。我试过了:
alias mygrep="grep `find . -name '*'`"
但它不起作用并发出错误:
grep: c: No such file or directory
我试过改,改不了成功的别名
有什么想法吗?
作为 find
到 grep
的 function than an alias, and using -exec
instead of passing the output 会更好。该输出将受到分词和通配的影响,因此可能会产生令人惊讶的结果。而是尝试:
mygrep () {
find . -name "*" -exec grep "" {} +
}