In the course of using smooth GUIs and colorful web front ends, you may not have known or cared that there is an entire other environment that undergirds your computer’s operating system. Beneath all that chrome and shine, there is a raw system of words and shells that can be a really powerful ally if you make full use of it. At first it will seem clunky, typing cryptic commands with no glitz and little feedback, but if you really embrace it, you can often times do things quicker and cleaner than you could using a GUI.

Welcome to your computer

Have you ever seen one of those hacker movies like The Net or Hackers or even The Matrix? Remember when they’re typing all those cryptic commands into computer terminals and things happen with lots of text scrolling, or how about those scenes where they typed in a few obvious commands and got very convenient feedback such as “System successfully hacked!”? Or maybe you’ve seen pictures or commercials from the 70’s with guys in polyester and bad beards typing random stuff into a computer that has a black screen and green text. In all of these situations, they’re using a shell, but fortunately none of these depictions is close to current reality.

If you’re on Mac OS X, dig into your Applications folder for the Utilities folder, then open the Terminal application. You should get a window that’s black with white text and has some sort of strange looking prompt on it (see the figure below).

Figure 1: Figure: A standard Mac terminal window

Go ahead and type something in and hit enter. It’s likely the system will tell you something like: -bash: whatever: command not found (unless you’re lucky and happened to type in a real command!).

Wherever you see the $ symbol in a figure, it means that you type the command into your terminal window (without the $).

– Probably discuss what’s in the prompt here…

The string bash there is the name of the shell that we’re using; there are several shell versions available but we’re using bash since it’s the standard on Mac OS X and most Linux systems. And bash is saying, “Hey! You tried to run something that doesn’t exist!” How about running something that does exist?

your-computer:~ yourusername$ whoami

This obviously named command simply tells you what your username on the system is (probably something like like firstnamelastname). So what just happened there? Well, the shell isn’t just a collection of magical commands. The shell environment came before these fancy GUIs, and it was how people ran applications: it’s where people edited text, played games, and so on. So, typing a shell command is like typing what you would normally double click. For example, execute the following sequence of commands:

your-computer:~ yourusername$ open /Applications/Safari.app

Did Safari pop up? If you’re not on Mac OS X, it won’t (try something like /usr/bin/firefox instead if you’re on Linux or iexplore if you’re on Windows), but if you are on Mac OS X, you’ve opened Safari from the command line, just as if you had clicked its icon on the GUI.

– Probably want to add a section here discussing the usage of parameters and arguments, otherwise it might be a little weird and they won’t get the concept.

The shell gives you access to a lot of powerful utilities that aren’t available or would be really clunky to run from a GUI. In this next section, we’ll take a look at some of these utilities and how to use them.

Moving about your system

To get you a little more used to using the shell to run applications, let’s take a look at an easy, practical example: navigating folders on your hard drive. Typically you navigate through something like the figure below.

Figure: Browsing folders with a GUI

You (single or double) click a folder to move to another one until you find the file you want, then you open it. You probably do it every day. Now, to do the same thing in the shell, you use the cd (change directory) command followed by the folder you want to move to next; then you use the open command we used earlier to open whatever file you want.

So, open a Terminal and execute the following commands:

your-computer:~ yourusername$ cd Documents
your-computer:Documents yourusername$ mkdir new_files
your-computer:Documents yourusername$ cd new_files

Where are we now? You can find your full path name with pwd - print working directory

your-computer:new_files yourusername$ pwd
/Users/yourusername/Documents/new_files    

Let’s play some more. You can use the “open” command for pretty much anything on OSX.

your-computer:new_files yourusername$ touch myfile.txt
your-computer:new_files yourusername$ open myfile.txt

Here we’ve changed our current directory into the Documents folder and created a folder named new_files using the mkdir command (make dirrectory). We then used a utility named touch to create an empty file named myfile.txt, and the opened that text file. The below figure parallels what we just did with the GUI.

Figure: Compare GUI navigation with cd

You can also use “open” on the current directory, which is denoted by a period (.)

    your-computer:Documents yourusername$ open .

This will open up a Finder window in the current directory.

The cd command also has a few tricks you can use to save time. For example, to move to the parent directory (for example, the new_files directory’s parent is Documents) you can use the .. parameter.

your-computer:Documents yourusername$ cd new_files
your-computer:new_files yourusername$ cd ..
your-computer:Documents yourusername$ cd ..
your-computer:~ yourusername$ 

You can also move into a nested directory (for example, rather than moving into Documents then new_files, you can move straight into new_files) by separating the parts of the hierarchy with a /.

your-computer:~ yourusername$ cd Documents/new_files

Before moving on, we need to look at a few more commands. What if you wanted to see what was in a folder you had used cd to reach? You’d need to use the ls (list) command.

your-computer:new_files yourusername$ touch myotherfile.txt
your-computer:new_files yourusername$ ls
myfile.txt  myotherfile.txt

If you wanted to remove any of these files, you’d need to use the rm command.

your-computer:new_files yourusername$ rm myfile.txt
your-computer:new_files yourusername$ ls
myotherfile.txt

If you need to remove a folder, then use the -rf (remove folder) parameter set for rm (for example, rm -rf myfolder). Keep in mind that rm is irreversible and files deleted with it can not be recovered.

If you need to move a file, you need to use the mv (move) command, but if you just want to copy a file to somewhere else, then you need the cp (copy) command.

your-computer:new_files yourusername$ mv myotherfile.txt ..
your-computer:new_files yourusername$ ls
your-computer:new_files yourusername$ cd ..
your-computer:Documents yourusername$ ls
myotherfile.txt
your-computer:Documents yourusername$ cp myotherfile.txt new_files/
your-computer:Documents yourusername$ cd new_files
your-computer:new_files yourusername$ ls
myotherfile.txt
your-computer:new_files yourusername$ ls ..
myotherfile.txt
your-computer:new_files yourusername$ cp myotherfile.txt myotherotherfile.txt
your-computer:new_files yourusername$ ls
myotherfile.txt   myotherotherfile.txt

– Break all this up…

Now that you have a fairly good grasp of simple filesystem operations, let’s move on to a few other, more useful utilities.

Doing things

The shell isn’t just useful for moving stuff around or navigating in and out of folders. You can actually do a lot of the same stuff that you can in a GUI, only sometimes it’s faster or easier (and sometimes it’s not…). Learning to do everyday tasks in the shell can be of use on your computer, but it’s essential if you plan on logging in to a web server or some other machine remotely (which, in web development, is fairly common).

Editing text with nano

There are a lot of text editors on systems like Linux or Mac OS X: vi, emacs, joe, ed, kate, gvim, gedit, TextEdit, TextMate, TextPad, and so on. In this section we’re going to take a look at the easiest to use shell-based text editor: nano (or, on your system, it may be pico). The nano and pico applications are essentially the same, except that nano is a much improved version (with better search and some other bells and whistles).

Editing files with nano

Opening a file with nano is as easy as typing nano followed by the filename.

your-computer:new_files yourusername$ nano myotherfile.txt

You can also create new files in this same manner:

your-computer:new_files yourusername$ nano nonexistentfile.txt

This will open nano and create a new file only if you save it (i.e., if you don’t write the file to disk, it won’t actually be created). When you open nano you should see something like the following figure (which is an annotated screenshot for your reference).

Figure: nano screenshot with keystrokes annotated

So your basic workflow in nano should be something like:

  1. Open file using nano <yourfile>
  2. Make your edits
  3. (optional) Write the file out to disk using Control + o
  4. Exit nano using Control + x

As you can see, your workflow is basically the same as a normal text editor, except it’s not as pretty and doesn’t use the mouse.

While editing, there are a few keystrokes you can use that can be really helpful. For example, Control + k will cut text and Control + u will “uncut” the text. This means you can cut a section of text and paste it back in wherever you want. The only downside to this feature is that you can only cut whole lines rather than pieces of arbitrary text like you can with a GUI editor. To cut and paste text, place the cursor on a line where you want to cut and press Control + k. You can cut multiple lines by repeating this step so long as you don’t move the cursor to another line and cut some text. Doing so will end the current “cut session” and start another, losing what you cut the first time (this is like pressing “Copy” again in a GUI editor). Then, move the cursor where you want to paste the text and press Control + u. The lines you cut will be inserted below the cursor.

The Control + c shortcut is a convenience for finding out where you are in a file: pressing it will show the line and column number you’re currently at. This is useful if you’re following a code or markup error to a specific line in a file. If you need to scroll through a file quickly, then Control + y and Control + v will page up and down. To find out more about keyboard shortcuts, you can read the nano manual by issuing the command man nano (man is also available for most commands on your system).

Searching

Searching in nano isn’t much different than what you’re use to in a GUI editor, but has a few extra features that make it far more powerful than what a GUI editor offers. To start a search in nano, simply press Control + w. A field will appear at the bottom of the screen where you type your search query. Pressing Enter will execute the search and place the cursor at the first result. If there is no result, then nano will tell you as such (or, to be more precise, it will tell you "<your search query>" not found). If there are more results, then you can use Control + w to hop to the next one (you’re executing another search but it will hop to the next result).

This search scenario is simple, but the Control + w key sequence actually opens up a whole world of file navigation and searching…

– Come back and fill in the other ^W commands

Finding files

Finding files in the path (whereis/which)

Using find (very basic; that command is crazy)

Chaining commands together

You can actually chain commands together. So, to save the output of a command to a file, you can use the > character.

$ ls -al > directory.txt

This will create or overwrite a file called “directory.txt” with the current directory listing.

$ ls .. >> directory.txt

This will list the parent directory and append the output to the text file.

Searching in files with grep

grep "<%= username %>" app/views/* -R

Searching in existing files

Piping into grep

Killing a process

Using killall

Using ps and kill