I do a lot of things by command line. Still. It’s faster, it’s easier, and in many cases, gives me more control. And as I always mention, people who use command line are people who really lazy. We don’t like sixteen clicks. If we can copy/paste and change one thing, we’re happy.
For ages, when I wanted to search my local repository of plugins, I’d whip out something like this:
grep -R "base64" ~/Development/WP-Plugin-Dir/* > ~/Development/WP-Plugin-Greps/base64-grep.txt
This works, but it’s slow and it’s not very pretty. The file output is a mess and it’s painstaking to sort through and understand.
/home/me/Development/WP-Plugin-Dir/jetpack/class.jetpack-post-images.php: ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that /home/me/Development/WP-Plugin-Dir/jetpack/modules/comments/comments.php: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/modules/contact-form/grunion-contact-form.php: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/modules/custom-css/custom-css.php: ob_start('safecss_buffer'); /home/me/Development/WP-Plugin-Dir/jetpack/jetpack.php: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/jetpack.php: ob_start();
On the other hand, there’s this:
ack --php 'ob_start' ~/Development/WP-Plugin-Dir/ > ~/obstart.txt
That actually gives a rather similar output:
/home/me/Development/WP-Plugin-Dir/jetpack/class.jetpack-post-images.php:36: ob_start(); // The slideshow shortcode handler calls wp_print_scripts and wp_print_styles... not too happy about that /home/me/Development/WP-Plugin-Dir/jetpack/modules/comments/comments.php:138: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/modules/contact-form/grunion-contact-form.php:264: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/modules/custom-css/custom-css.php:350: ob_start('safecss_buffer'); /home/me/Development/WP-Plugin-Dir/jetpack/jetpack.php:872: ob_start(); /home/me/Development/WP-Plugin-Dir/jetpack/jetpack.php:928: ob_start();
That’s ack, which claims to be better than grep, and I’m kind of agreeing. Let’s look at the small differences.
- Line numbers. That will help me find the code later.
- Only searching PHP files
- Recursive by default
- Ignores SVN and other similar folders.
How do you do only PHP files in grep?
grep pattern $(find . -name '*.php' -or -name '*.phpt' -or -name '*.php3' -or -name '*.php4' -or -name '*.php5' -or -name '*.phtml' )
Right. Like I’m going to remember that.
And we can make ack better. Let’s ignore a folder:
ack --ignore-dir=akismet 'string'
How about customizing my output so I can check how often a plugin is doing_it_wrong()?
ack --php --group 'ob_start' ~/Development/WP-Plugin-Dir/ > ~/obstart.txt
That’s a little easier to read.
/home/me/Development/WP-Plugin-Dir/jetpack/modules/custom-css/custom-css.php 350: ob_start('safecss_buffer'); /home/me/Development/WP-Plugin-Dir/jetpack/jetpack.php 872: ob_start(); 928: ob_start();
Just want a list of the filenames?
ack --php -l 'ob_start' ~/Development/WP-Plugin-Dir/ > ~/obstart.txt
Or what if I want to search all instances of
jetpack/jetpack.php? You can make ack sit up and beg.
You can see that ack is a lot more powerful right away when it comes to being able to quickly use the data without a lot of parsing. There are some catches with ack, though, like it has a whitelist of file types that it will search, so if you don’t tell it to search .html, it won’t. That’s a small price to pay for me.
The documentation is written in nerd, so I generally find looking at concrete examples is more helpful. Do you have tricks with ack (or even grep) that save you time and money?