Part 1: Using the Command Line for Beginners

Typing the Command Line

We all love shortcuts. CTRL + C to copy some text. Brownie mix. You name it. We find workarounds in all aspects of our lives to save time. For programmers, using the Command Line is a shortcut that makes certain tasks, like creating files and directories, faster and easier. Are you new to coding and looking for a way to up your programming game? Read James’ tips for using the command line for beginners!

Typing the Command Line

Using the Command Line Interface (CLI) or terminal for common operating system tasks can be intimidating. Personally, I was kind of resistant to learning how to do it when I started coding. Once I took the plunge I actually found that it was much faster to complete common operating system tasks, like creating files and directories. Now, doing it the ‘old way,’ like creating a new file in a text editor, feels very slow and clunky.

Getting started with the terminal is as easy as launching the Terminal on your OS X device (found in the Utilities folder in Applications. For this blog we will be using Unix commands. As OS X is based on Unix, all of these commands will work natively on a Mac. Are you a Windows user? Probably the easiest way to follow along would be to install a Unix-like environment such as Cygwin or the Git Bash terminal for Windows. (There’s also a more robust terminal called PowerShell, but including all of its commands is beyond the scope of this blog.)

Let’s get started! At first glance, most terminal commands look like the incoherent scribblings of an over-caffeinated five-year-old. That’s because almost all of the commands are abbreviations of the actual thing you want to do. This will seem clearer as you see a few of the commands and what they mean side by side.

Getting Around the Command Line & Terminal

First, let’s learn a few commands for moving through the file system. All of the mouse clicking through Windows explorer and finder can actually be slower than using a terminal. First we should state that a file system is structured like a tree. So moving around our file system is a task that involves moving up and down that tree as much as anything else.

pwd – Short for Print Working Directory, this simply prints the fully qualified path of your current location in the file system starting from the root directory of the computer. This will tell you your current location. All you have to do is type pwd. Simple. 

Print Working Directory

ls – Short for… I don’t actually know. But it lists the contents of the directory so maybe that’s where the name comes from.

Lists the Contents of the Directory

cd <directory-name> Short for Change Directory. This command will allow you to, stay with me here, change your current directory. The primary difference between cd and the previous commands is that it takes an argument. The argument for the cd command is the directory you want to change to. If you’re not sure what directories there are to choose from, that’s no problem because we now know how to list the contents of a directory using ls to see. There is a not-very-obvious option when you need to go up the file system tree. In that case you enter cd ..  to move to the folder one level above your current position in the file system.

CD - Directory Name

Additionally, you can quickly jump to your Home directory. Your home directory is your main user directory. This is where your Documents, Music, and Photos folders reside. If you ever need to jump to your home directory, you can use the ~ character. This will also work with cd. So typing cd ~ will take you directly to your Home directory in the terminal.

One last thing about cd, you can also move more than a single level in the file structure tree so if you have several nested directories you can use a single cd command to move through them, i.e. cd dir1/nestedDir/nestedDir2 or cd ../../.., for moving up.