Crash Course in the Command Line

Throughout this book, you will be instructed to use the command line or terminal. Many of the tools you will be using run exclusively as command-line programs.

To access the command line on a Mac, open Finder and go to the Applications folder, then the Utilities folder. Find and open the program named Terminal (Figure 1.15).

Figure 1.15  Finding the Terminal app on a Mac

Finding the Terminal app on a Mac

You should see a window that looks like Figure 1.16.

Figure 1.16  Mac command line

Mac command line

To access the command line on Windows, go to the Start menu and search for “cmd.” Find and open the program named Command Prompt (Figure 1.17).

Figure 1.17  Finding the Command Prompt program on Windows

Finding the Command Prompt program on Windows

Click it to run the standard Windows command-line interface, which looks like Figure 1.18.

Figure 1.18  Windows command line

Windows command line

From now on, we will refer to “the terminal” or “the command line” to mean both the Mac Terminal and the Windows Command Prompt. If you are unfamiliar with using the command line, here is a short walkthrough of some common tasks. All commands are entered by typing at the prompt and pressing the Return key.

Finding out what directory you are in

The command line is location based. That means that at any given time it is in a particular directory within the file structure, and any commands you enter will be applied within that directory. The command-line prompt shows an abbreviated version of the directory it is in. To see the whole path on a Mac, enter the command pwd (which stands for print working directory), as in Figure 1.19.

Figure 1.19  Showing the current path using pwd on a Mac

Showing the current path using pwd on a Mac

On Windows, use the command echo %cd% to see the path, as in Figure 1.20.

Figure 1.20  Showing the current path using echo %cd% on Windows

Showing the current path using echo %cd% on Windows

Creating a directory

The directory structure of front-end projects is important. Your projects can grow quickly, and it is best to keep them organized from the beginning. You will create new directories regularly during your development. This is done using the mkdir or make directory command followed by the name of the new directory.

To see this command in action, set up a directory for the projects you will build as you work through this book. Enter this command:

mkdir front-end-dev-book

Next, create a new directory for your first project, Ottergram, which you will begin in the next chapter. You want this new directory to be a subdirectory of the front-end-dev-book directory you just created. You can do this from your home directory by prefacing the new directory name with the name of the projects directory and, on a Mac, a slash:

mkdir front-end-dev-book/ottergram

On Windows, you use the backslash instead:

mkdir front-end-dev-bookottergram

Changing directories

To move around the file structure, you use the command cd, or change directory, followed by the path of the directory you want to move into.

You do not always need to use the complete directory path in your cd command. For example, to move down into any subdirectory of the directory you are in, you simply use the name of the subdirectory. So when you are in the front-end-dev-book directory, the path of the ottergram folder is just ottergram.

Move into your new project directory:

cd front-end-dev-book

Now, you can move into the ottergram directory:

cd ottergram

To move up to the parent directory, use the command cd .. (that is, cd followed by a space and two periods). The pair of periods represents the path of the parent directory.

cd ..

Remember that you can check your current directory by using the pwd command (or echo %cd% on Windows). Figure 1.21 shows the author creating directories, moving between them, and checking the current directory.

Figure 1.21  Changing and checking directories

Changing and checking directories

You are not limited to moving up or down one directory at a time. Let’s say that you had a more complex directory structure, like the one shown in Figure 1.22.

Figure 1.22  An example file structure

An example file structure

Suppose you are in the ottergram directory and you want to go directly to the stylesheets directory inside of coffeerun. You would do this with cd followed by a path that means the stylesheets directory inside the coffeerun directory inside the parent directory of where I am now:

cd ../coffeerun/stylesheets

On Windows, you would use the same command but with backslashes:

cd ..coffeerunstylesheets

Listing files in a directory

You may need to see a list of files in your current directory. On a Mac, you use the ls command for that (Figure 1.23). If you want to list the files in another directory, you can supply a path:

ls
ls ottergram

Figure 1.23  Using ls to list files in a directory

Using ls to list files in a directory

By default, ls will not print anything if a directory is empty.

On Windows, the command is dir (Figure 1.24), which you can optionally give a path:

dir
dir ottergram

Figure 1.24  Using dir to list files in a directory

Using dir to list files in a directory

By default, the dir command will print information about dates, times, and file sizes.

Getting administrator privileges

On some versions of OS X and Windows, you will need superuser or administrator privileges in order to run some commands, such as commands that install software or make changes to protected files.

On a Mac, you can give yourself privileges by prefixing a command with sudo. The first time you use sudo on a Mac, it will give you a stern warning, shown in Figure 1.25.

Figure 1.25  sudo warning

sudo warning

sudo will prompt you for your password before it runs the command as the superuser. As you type, your keystrokes will not be echoed back, so type carefully.

On Windows, if you need to give yourself privileges you do so in the process of opening the command-line interface. Find the command prompt in the Windows Start Menu, right-click it, and choose Run as Administrator (Figure 1.26). Any commands you run in this command prompt will be run as the superuser, so be careful.

Figure 1.26  Opening the command prompt as an administrator

Opening the command prompt as an administrator

Quitting a program

As you proceed through the book, you will run many apps from the command line. Some of them will do their job and quit automatically, but others will run until you stop them. To quit a command-line program, press Control-C.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.135.175