How to write bash alias / function to grep all files in all subdirectories for a string?

I used the following command for grep for a string in all python source files in and below my current directory:

find . -name '*.py' -exec grep -nHr <string> {} \;

I would like to simplify everything so that I can just type something like

findpy <string>

And get the same result. The aliases do not seem sufficient, as they only build the extension, and the argument I need to specify is not the last argument. Functions seem to be suitable for the task, so I have a few questions:

  • How do i write?
  • Where can I put it?
+3
source share
9 answers

script , :

findpy() { find . -name '*.py' -exec grep -nHr "$1" {} \; ; }

... , , ~/.bashrc, ~/.bash_profile, , (. INVOCATION w50 > ).

+10

"find... -exec" , , , . , *.py . xargs (1) grep ( ):

#! /bin/sh
find . -name \*.py -type f | xargs grep -nHr "$1"

:

$ time sh -c 'find . -name \*.cpp -type f -exec grep foo {} \; >/dev/null'
real    0m3.747s
$ time sh -c 'find . -name \*.cpp -type f | xargs grep foo >/dev/null'
real    0m0.278s
+6

Ack , . Grep, Perl. .svn ..

( Trac):

$ ack --python foo ./mysource
ticket/tests/wikisyntax.py
139:milestone:foo
144:<a class="missing milestone" href="/milestone/foo" rel="nofollow">milestone:foo</a>

ticket/tests/conversion.py
34:        ticket['foo'] = 'This is a custom field'

ticket/query.py
239:        count_sql = 'SELECT COUNT(*) FROM (' + sql + ') AS foo'
+6

- , Idelic xargs: . , , , " " ( , , grep).

bash_aliases:

alias findpy = "find. -type f -name '*.py' | xargs grep"

, findpy WORD findpy -e REGEX findpy -il WORD - , grep.

+2

findpy

#!/bin/bash

find . -name '*.py' -exec grep -nHr $1 {} \;

chmod u+x findpy

bin , , . PATH.

+1

script:

#!/bin/bash
find . -name '*.py' -exec grep -nHr "$1" {} ';'

.

vim - . , ~/bin , .profile ( ) :

PATH=$PATH:~/bin
+1

grep , ..

grep --perl-regexp --recursive --include='*.py' --regexp="$1" .

, (.), , 'py', Perl.

grep --recursive --include, find xargs, , -print0 - -null xargs .

find . -type f -name '*.py' -print0 | xargs --null grep "$1"

.

+1

Add the following line to your ~ / .bashrc or ~ / .bash_profile or ~ / .profile

alias findpy='find . -type f -name "*.py" -print0 | xargs -0 grep'

then you can use it like that

findpy def

or with grep options

findpy -i class

the following alias will ignore the git and svn version control meta directory

alias findpy='find . -type f -not -path "*/.git/*" -a -not -path "*/.svn/*" -name "*.py" -print0 | xargs -0 grep'
+1
source
#######################################################################################
#
# Function to search all files (including sub-directories) that match a given file
# extension ($2) looking for an indicated string ($1) - in a case insensitive manner.
#
# For Example:
#
# -> findfile AllowNegativePayments cpp
#
#
#######################################################################################
findfile ()
{
    find . -iname "*.$2*" -type f -print0 | xargs -0 grep -i "$1" {} \; 2> /dev/nul
}

alias _ff='findfile'
0
source

Source: https://habr.com/ru/post/1709356/


All Articles