grep, ale tylko niektóre rozszerzenia plików

Pracuję nad skryptem do grep niektórych katalogów:

{ grep -r -i CP_Image ~/path1/;
grep -r -i CP_Image ~/path2/;
grep -r -i CP_Image ~/path3/;
grep -r -i CP_Image ~/path4/;
grep -r -i CP_Image ~/path5/; } 
| mailx -s GREP [email protected]

Jak mogę ograniczyć wyniki Tylko do rozszerzeń .h oraz .cpp?

Author: ashleedawg, 2012-09-20

12 answers

Po prostu użyj parametru --include, tak:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP [email protected]
To powinno robić, co chcesz.

Aby wziąć Wyjaśnienie z HoldOffHunger ' s odpowiedź poniżej:

  • grep: polecenie

  • -r: rekurencyjnie

  • -i: ignore-case

  • -n: każda linia wyjściowa jest poprzedzona względnym numerem linii w pliku

  • --include \*.cpp: Wszystkie*.cpp: C++ files (escape with \ just in case you have a katalog z gwiazdkami w nazwach plików)

  • ./: zacznij od bieżącego katalogu.

 1488
Author: Nelson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-12-27 03:45:28

Niektóre z tych odpowiedzi wydawały się zbyt ciężkie lub powodowały problemy na moim serwerze Debiana. To działało idealnie dla mnie:

grep -r --include=\*.txt 'searchterm' ./

...lub bez rozróżniania wielkości liter...

grep -r -i --include=\*.txt 'searchterm' ./
  • grep: polecenie

  • -r: rekurencyjnie

  • -i: ignoruj-case

  • --include: Wszystkie*.txt: Pliki Tekstowe (escape za pomocą \ na wypadek, gdybyś miał katalog z gwiazdkami w nazwach plików)

  • 'searchterm': co do Szukaj

  • ./: zacznij od bieżącego katalogu.

Źródło: rewolucja PHP: jak Grepować pliki w Linuksie, ale tylko niektóre rozszerzenia plików?

 323
Author: HoldOffHunger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-17 04:04:32
grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./
 64
Author: Yuri Malov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-23 15:15:46

A może:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print
 53
Author: Amir Afghani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-20 16:33:57

Nie ma opcji-r na serwerach HP i Sun, ten sposób działał u mnie na moim serwerze HP

find . -name "*.c" | xargs grep -i "my great text"

- i jest dla niewrażliwego na wielkość liter wyszukiwania ciągu

 23
Author: Sam B,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-11 15:14:33

Ponieważ jest to kwestia znalezienia plików, użyjmy find!

Używając GNU find możesz użyć opcji -regex, aby znaleźć te pliki w drzewie katalogów, których rozszerzenie to .h lub .cpp:

find -type f -regex ".*\.\(h\|cpp\)"
#            ^^^^^^^^^^^^^^^^^^^^^^^

To tylko kwestia wykonania grep na każdym z jego wyników:

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

Jeśli nie masz takiej dystrybucji find musisz użyć podejścia takiego jak Amir Afghani ' s, używając -o do łączenia opcji ( nazwa jest albo zakończone na .h lub na .cpp):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A jeśli naprawdę chcesz użyć grep, postępuj zgodnie ze składnią wskazaną do --include:

grep "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^
 13
Author: fedorqui 'SO stop harming',
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:26

Najprostszym sposobem jest

find . -type  f -name '*.extension' 2>/dev/null | xargs grep -i string 

Edytuj: dodaj 2>/dev/null, aby usunąć wyjście błędu

Aby dołączyć więcej rozszerzeń plików i grep dla hasła w całym systemie:

find / -type  f \( -name '*.conf' -o -name "*.log" -o -name "*.bak" \) 2>/dev/null |
xargs grep -i password
 8
Author: user3640130,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-12-31 11:02:56

Poniższa odpowiedź jest dobra:

grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP [email protected]

Ale może być zaktualizowany do:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP [email protected]
Co może być prostsze.
 4
Author: hao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-19 17:00:45

ag (silver searcher) ma dość prostą składnię dla tego

       -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

Więc

ag -G *.h -G *.cpp CP_Image <path>
 3
Author: Eldamir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-10 13:57:11

Powinien napisać "- exec grep "dla każdego" - o-name "

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

Lub pogrupuj je według ()

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

Opcja '- Hn ' pokazuje nazwę pliku i linię.

 1
Author: where23,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-10 02:25:43

Wiem, że to pytanie jest trochę przestarzałe, ale chciałbym podzielić się metodą, której zwykle używam, aby znaleźć .c i .h pliki:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H "#include"

Lub jeśli potrzebujesz również numeru linii:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH "#include"
 1
Author: Tom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-17 13:47:27

Jeśli chcesz odfiltrować rozszerzenia z wyjścia innej komendy np. "git":

files=$(git diff --name-only --diff-filter=d origin/master... | grep -E '\.cpp$|\.h$')

for file in $files; do
    echo "$file"
done
 0
Author: nvd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-10-09 20:44:09