The following grep command will search for any constants such as
define('CONSTANT', 1) within your PHP code base so you can see their values. Useful as many applications store the constant’s value in the database and not the constant’s friendly name.
This requires regular expressions, hence the use of egrep.
egrep --include *.php "(define)(.*)" -r *
Of all the terminal commands, the one I find myself using the most (several times a day) as a developer is this:
$ grep -lr “<search text>” .
It performs a recursive search (-r) from the current location (.) looking for the search text provided and returning a listing of matching files (-l).
Basically it does a “find in files” from the command line. Very useful for quickly finding things, and super-fast (no need to wait for a GUI to load and sluggishly do the search).
After a bit of searching and trying a few different methods, I’ve settled on the following commands to find and replace a text string in all files that match the search string identified through grep.
Here’s the full command (adjust the values of ‘searchtext’, ‘matchtext’ and ‘replacetext’ accordingly).
$ grep -r -l 'searchtext' . | sort | uniq | xargs perl -e "s/matchtext/replacetext/" -pi
Here’s a break down of how it works from left to right:
$ grep -r -l 'searchtext' .
Finds and lists all files in the current directory (and all subdirectories recursively) that match the value of ‘searchtext’.
$ | sort | uniq
This pipes the grep results to sort which sorts the results. The results are then piped to uniq to filter out any duplicates
$ | xargs perl -e "s/matchtext/replacetext/" -pi
Unique grep results from previous commands are piped to xargs so that the perl command can be executed against them.
The perl -e flag executes a regex search for the string ‘matchtext’ and replaces it with ‘replacetext’ using “s/matchtext/replacetext/” on each file it receives from the grep results. The -pi flag edits the file in place – i.e., saves the change of replacing ‘matchtext’ with ‘replacetext’ in each file. Note you can use variations on -pi to save to a new file if that is what you are after.