Current Goal: Making Websites

The Command Line

What it is

The Command Line, or Shell (same thing), is the interface for operating your computer via text. It was the standard way to use a computer before graphical UI’s like Windows came along. It is a powerful, highly programmable tool that gives you direct control over what your computer does.

Why it matters

Mastering the command line is absolutely essential because it is one of the primary ways a developer runs and manages their code. Here's some of the most common things a developer uses the shell for day to day:

Operating Source Control

Source control is a vital tool that lets you keep track of the changes made to your code. We’ll cover it in the next lesson. You could theoretically use git with a GUI tool, but git can be quite complex and you’ll find its easier to make it do what you want from the shell.

Running programs

Unlike consumer apps, which come nicely bundled and can be run with a double click, most programs you’ll write need to run through an interpreter via the command line. Additionally, the shell's text interface provides an easy way to see what the program’s output and prevents you from having to build a whole messy GUI to test your little 10 line script.

Troubleshooting

What filled up all of your disc space? What program is taking up all of the CPU and slowing your computer to a crawl? Your operating system has some kind of graphical tool for diagnosing these issues (Ctrl Alt Delete), but again, this becomes easier to handle when you can harness the powerful command line tools. Are you seeing the pattern?

Logging in to a remote server

One of the most important shell commands you’ll use is SSH, or secure shell. This allows you to log in to another computer over the internet and access it via its shell. It’s possible to load up a GUI for a remote machine, but these are complicated tools tacked on top of regular ssh. Why bother when the shell is more powerful than the GUI anyway?

Though initially daunting, mastering the shell will make your day to day tasks much easier. In the short term, our objective is to set up our development environment enough to write websites. We need the command line in order to run a local web server and use git for source control. By the end of the lesson, we’ll set up a projects folder for our google site code and launch a local web server to host it.

What you'll learn

  • How to navigate the filesystem from the shell.
  • How to manage files and run programs from the shell.

How to learn it

"Learning the shell” is an ambiguous task because it is a general tool that encompasses many different use cases. It is a bit like saying learning Windows: there’s a big difference between a power user modifying their registry and a standard user who mostly just manages their pictures and runs the web browser.

The design of the shell is based on the Unix Philosophy: its better make lots and lots of little tools that each do one task well than to make a giant monolithic tool that does everything. Once you get the basics, gaining skill with the shell entails learning more and more little commands.

So, even though shell commands often seem confusing and terse, it is quite possible to master it in stages by learning just a few commands at a time to achieve basic tasks.

Up front, we need to learn enough to be able to move around the file system, run programs, and use git. There’s a great tutorial for this called Learn Enough Command Line to Be Dangerous.

Work through all four sections and do the exercises.

A note for Windows users

The Windows PowerShell uses a completely different vocabulary of commands than the Unix-based shell used on Macs and Linux machines. Learning the Unix commands isn’t really optional: servers generally run Linux, and you’ll need to be able to work on them. This is why many developers prefer to use Macs even though they are more expensive: they only have to learn the shell once.

Don’t worry, you can still proceed with Windows: the tutorial will have you set up a Linux Virtual Machine, basically a simulation of a Linux computer running within your computer, which will allow you to develop on Linux without having to ditch Windows for your other needs.

As it promises, the Learn Enough tutorial will cover enough to last you a while. However, there’s more to learn to become a shell master. We’ll come back to the command line down the road, but if you want to dig in you can check out this free online book, Conquering the Command Line.

Comprehension

  • In the following line, which components are the prompt, command, options and argument? [projects]$ rm -f foo.txt
  • What is a string?
  • What does the echo command do?
  • What is the hotkey for canceling a program in the shell?
  • What command lets you see the instructions for a shell command?
  • What does Learn Enough mean by technical sophistication?
  • How can you rerun the previous command in bash shell?
  • How can you write a string of characters into a file with bash?
  • What does the cat command do?
  • How can you append text to a file in Unix?
  • How can you list the files in your current directory?
  • How can you create an empty file with Unix?
  • What is a hidden file in Unix? How do you make one?
  • How can you list the hidden files in a Unix directory?
  • What Unix command allows you to rename a file?
  • What Unix command allows you to copy a file?
  • What Unix command allows you to delete a file?
  • What Unix command can tell you if, e.g. the program curl is available on your machine?
  • What Unix command lets you read the contents of a file?
  • How can you search a file with the Unix command less?
  • How do you quit less?
  • What is grep?
  • How is the file system represented in the shell?
  • What command tells you your current directory?
  • What Unix command lets you change directories?
  • What Unix directory is represented by a tilde (~)?
  • What is the Unix command to make a directory?
  • What is the Unix command to copy a directory?
  • How do you write a relative path in a Unix filesystem? How is it different than an absolute path?
  • How do you delete a directory full of files in Unix?
  • What is the Unix Philosophy?

Tasks

For more CLI practice, work through the Code Academy Command Line Tutorial.

In the shell, use pwd, ls, and cd to find the directory containing your Google homepage code (assuming you did the project from Part 1.

Use mkdir to make a Projects folder in your home directory, and use cp to copy the Google page project into a folder in the new Projects directory. Put all of your projects here from now on.

Use ls to figure out the size of the index.html file from the project. Search the man page of the ls command to figure out the option flags you need to make the file size human readable.

What's Next?

This site relies on your feedback! These lessons are free. Please take this 2 minute survey when you finish each lesson so I can make them better.

Current Goal: Making WebsitesNext Lesson: Running a Server