Taking Command: Three Ways the Command Line Can Help You Write Better Code

The command line is a tool that can get in your way, or make complicated problems easier to solve. How can developers use the command line in their day-to-day development work?

When I code, I often come across problems that aren’t solvable by writing more code (my normal strategy). Instead, these problems tend to require fixing, changing, or updating something about how my computer works. Other times, I accidentally do something that messes with the state of my computer. I need to figure out what I did wrong and how to fix it.

In these cases, the command line is a powerful asset. Today, I’m going to share three situations where the command line can save you time and spare you frustration. Buckle up!

1: Speed Up Your Workflow

When I start coding on a new project, my life is simple: I need one command to run my site, and maybe another to open that site in my browser, to test it out. However, as I build grander, more interesting projects, I find myself needing to chain together several commands every time I want to run my site: maybe using grunt, or gulp, or Sass, or Node, or uglify, or a combination of all those together.

When I find myself repeatedly typing the same four lines in a row on the command line, I know I need a faster process. Some simple scripting and adjustments to the PATH can change my four-line build process into a single command—one that I can send to other developers on my team to also make their build processes more efficient, too.

Try it out in the command line:

  1. Identify the repeated lines of code and put them in a script file (say buildsite.sh).
  2. Add that file somewhere on the PATH (usually, /usr/bin).
  3. Make sure the file is executable with chmod (say, chmod a+x buildsite.sh).

Now I can use the name of the file anywhere on my system to run those commands (so, anywhere I want to build my site, I just type buildsite.sh).

2. Pinpoint Specific Processes

Every once in a while, when I’m running code, I do something that causes a server to silently continue to run in the background, or accidentally leave Postgres thinking it’s running when it’s not. In those cases, knowing how to debug which process is using which port on my machine becomes a major advantage.

In this situation, I know the right command line tools to help me out. I start with lsof -i:3000 to figure out which server is running and keeping me from starting another one. Once I find the correct process, I can kill it without stopping other important processes.

Try it out in the command line (you’ll have to have some servers running for this one):

  1. Run lsof -i:[portnumber].
  2. Read the output and find the PID of the process listening on that port.
  3. Run kill [PID] on that process.

3. Decypher Installation Errors

No matter how excited I am to try a new piece of technology, I tend to get tripped up at some point during the installation process. I recently had an issue where npm couldn’t install any packages globally; I would get “permission denied” errors, and when installing them with sudo meant that I couldn’t run the packages as a normal user.

Luckily, I could read the error messages that npm gave me to figure out what had gone wrong: the directory where it wanted to install its packages had become read-only. In this case, it only took one quick command, chmod, and I was back in business.

Try it out in the command line:

  1. Type which to figure out where on your computer npm was trying to install these programs
  2. ls -l to see at what level that directory had become read-only
  3. chmod g+w and chgrp to give yourself write access to that directory

Next Steps

These are just a few of the ways that knowing your command line can save you time and frustration.

If you want to learn even more about how you can harness the power of the command line for faster dev time, join us on campus for my upcoming Code 501 course! Learn more »

Next PostPrevious Post

About the Author