Есть ли какой-либо способ, которым я мог использовать grep для игнорирования некоторых файлов при поиске чего-то, чего-то эквивалентного svnignore или gitignore? Я обычно использую что-то вроде этого при поиске исходного кода.
grep -r something * | grep -v ignore_file1 | grep -v ignore_file2
Даже если бы я мог бы настроить псевдоним к grep для игнорирования этих файлов, было бы хорошо.
--exclude
option on grep will also work:
grep perl * --exclude=try* --exclude=tk*
This searches for perl in files in the current directory excluding files beginning with try
or tk
.
Use shell expansion
shopt -s extglob
for file in !(file1_ignore|file2_ignore)
do
grep ..... "$file"
done
Вот минималистичная версия .gitignore. Требуются стандартные утилиты: awk, sed (потому что мой awk настолько хромает), egrep:
cat > ~/bin/grepignore #or anywhere you like in your $PATH
egrep -v "`awk '1' ORS=\| .grepignore | sed -e 's/|$//g' ; echo`"
^D
chmod 755 ~/bin/grepignore
cat >> ./.grepignore #above set to look in cwd
ignorefile_1
...
^D
grep -r something * | grepignore
grepignore
строит простое предложение чередования:
egrep -v ignorefile_one|ignorefile_two
не очень эффективно, но хорошо для ручного использования
Вы также можете взглянуть на ack , который, среди многих других функций, по умолчанию не выполняет поиск в каталогах VCS, таких как .svn и .git.
I thinks grep does not have filename filtering. To accomplish what you are trying to do, you can combine find, xargs, and grep commands. My memory is not good, so the example might not work:
find -name "foo" | xargs grep "pattern"
Find is flexible, you can use wildcards, ignore case, or use regular expressions. You may want to read manual pages for full description.
after reading next post, apparently grep does have filename filtering.
find . -path ./ignore -prune -o -exec grep -r something {} \;
What that does is find all files in your current directory excluding the directory (or file) named "ignore", then executes the command grep -r something on each file found in the non-ignored files.