Command-line helpers

Here are two helpful command-line (Linux) tools which are immensely helpful when working on large themes, particularly if you’re modifying someone else’s work. Both require you to first navigate to the directory in which your files reside (see below for a primer).

1. Find all instances of something.

grep -ri "something" *

This will respond with all files in which your “something” resides (quotes not needed if “something” is one word). Incidentally, the ‘r’ displays all results from directories inside the one you’re in, and the ‘i’ makes the search case-insensitive. The ‘*’ simply means to search all files (you could instead specify certain filenames).

Want to view the line number of each result within the file? Add the ‘n’ flag:

grep -rin "something" *

Now, you may find your output is too hard to read, because some results display really long, wrapped lines. Clean up by shortening (“cutting”) each line to some number of characters, say 99:

grep -rin "something" * | cut -c -99

That’s a ‘pipe’ character (vertical line, above the backslash in U.S. keyboards); notice the hyphen before the cut number 99.

If you’re comfortable with regular expressions, add an ‘E’, and enter your regex:

grep -Erin 'function\s+\w' *

The above example is a good way to list all named functions (“function SomeName…”) in your files.

2. Compare two files.

There are many tools which do the same, but when I’m not able to use some project-management utility (e.g. GitHub) I use this to show only the differences between two files. No matter how organized you are, it seems you’ll always come to this point where you have to check:

sdiff -sb file1.php file2.php

Primer: Basic directory navigation

Use pwd to find out which directory you’re in,
cd to change it,
../ to move up one, ../../to move up two (and so on),
and ls to list the files and directories.
Example (your prompts likely look different than the > shown here):

> pwd
/home/projects/folder1
> ls
archive.php
article-header.php
article-hero.php
author.php
css
images
> cd css
> ls
article.css
hero.css
post-additional.css
> cd ../