Member preview

Code for Designers, Part 1

We’ll start with a quick command line trick

If you’re on a Mac, there’s a built-in program called Terminal. Do a search for it and launch it. It’ll look something like this.

So that’s the command line. But it’s a scary place at first. It can feel like waking up in an unfamiliar cabin in the woods after blacking out the night before. You might be asking “where am I?” or even “who am I?” before starting to look around and take inventory. So let’s do the same thing now. “who am I?” can be learned by typing whoami:

Makes sense. If your login is turtleflower, that will say turtleflower. In this case, my test account is named steve, so the response is steve. How do we figure out where we are? We type pwd which stands for —hm. I have no idea what it stands for. A lot of commands are like that. It’s just pwd because we decided decades ago to use that name. Try it now.

That makes sense too. There’s a Users folder, and under that there’s a steve (or turtleflower, or whatever your name is) and that’s where we are. At the command line, this is called “home.” Which is why the titlebar has a little icon of a home. Next let’s figure out what files are here. The command for this is ls which stands for list.

This should look familiar. This is the standard home directory structure on the Mac. If you have other files in your home directory, you’ll see them here. Makes sense, right?

Now let’s try typing clear and hitting enter. Here’s how it looks before and after you enter the command.

This can be helpful when the screen is getting full. Or if you’re about to enter a command that’s going to return a bunch of stuff, which we’re about to do. We’re going to do a search for every file in our home directory. Every single one! The way we do it is by typing find .

(notice the dot at the end)

The word “find” means “find” and the dot means “right here.” So the command means “find everything starting right here, even if things are in subdirectories.” Here’s how it looks before and after running the command.

That’s pretty powerful stuff. Siri or Spotlight don’t have ways to say “show me every single file in this entire directory, even if things are squirreled away in folders” even though sometimes that’s exactly what you need.

But what would be even better is combining this with a way to google for particular names. For example, maybe we want to find every file with the word Adobe in it.

The command for that is find . | grep Adobe as shown here.

We’ve introduced two new concepts here. First is that little line, which is called a pipe. On a Mac laptop you type it by holding shift and pressing the |\ key right about the enter/return key. The pipe is a way of passing information from one command to another, and it’s really common.

The second new concept is the word grep which is an all-purpose “needle in a haystack” tool. So when you look at our entire command —

find . | grep Adobe

— in plain English it’s saying “find everything starting right here, even if things are in subdirectories. Once you have that list, I want you to see if the word Adobe appears in any of the lines. If so, show them.”

So if we hit enter on that command, here’s what we’re shown.

Whump. A big list of every single time the word “Adobe” shows up in our home directory. (Note that this is case sensitive, so grepping for “adobe” and “Adobe” will return different results.)

But sometimes a giant list in the command line isn’t as helpful as an actual file. So how can we take this command and put all the results into a file? By using the > sign. When we use “>” we’re saying “and put all these results into a file. So here’s our new command:

find . | grep Adobe > adobeResults.txt

In plain English, our command now says “find everything starting right here, even if things are in subdirectories. Once you have that list, I want you to see if the word Adobe appears in any of the lines. If so, put them into a file called adobeResults.txt.”

Now if we open our home directory, we’ll see that we just made a new file! That’s kind of cool.

And if we open it, we see our big list of every time the word Adobe was found in our home directory.

Knowing this tiny bit of code has helped me many times as a designer. Maybe it will help you someday!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.

Only members of Medium may see responses to this story.