What "the command line" is

Tags: 

Most cheap shared hosting accounts run on Unix/Linux servers. There are tools that can help you run your sites, saving you time in the process. Suppose there's an update to Drupal core. How to apply it? You can use FTP to delete the old files, upload the new files, jiggle this, poke that, and get it done in 30 minutes or so. Or you can use the wonderful tool Drush. It will take you 30 seconds, instead of 30 minutes.

Drush is a command line program. That is, you run it by typing commands, like this:

Running Drush

Figure 1. Running Drush

This makes some people nervous. You can damage your site at the command line, just as you can with FTP.

There are good reasons to learn a little about the command line:

  • Sometimes you need to use the command line to fix things.
  • The command line makes you more productive, and a more valuable employee.
  • You can swagger.

This article explains what the command line is. It shows you how to log in to your Web server's command line from Windows. You'll learn a few safe commands, and how to log out.

If you're nervous about the command line, start here. It's like dipping your toe into the water and pulling it out immediately, just to show yourself you can do it.

I'll assume that you know how to use FTP, and know how to use your hosting provider's control panel. The control panel is the software they give you to manage your Web site. cPanel is a common one.

Let's start by comparing how you copy a file, the GUI way, and the command line way.

Copying a file - the GUI way

You're using a computer right now. (If you aren't, pretend you are.) This is you:

You and a computer

Figure 2. You and a computer

Suppose you're copying a file, a drawing of a bug. Your screen might look like this:

Copying a file

Figure 3. Copying a file

You hold down the control key, and drag the file to the window on the right.

These actions are called "gestures." They're things people do to control computers. Typing on a keyboard, moving a mouse or a trackball, pressing a button on a game controller, they're all gestures.

To copy the bug file, a program on your computer had to interpret your gesture (control+dragging), and do some computerish action (copy a file). What program is that? It's Windows Explorer (WE). (Or whatever you use on your computer of choice.)

(Note that Windows and Windows Explorer (WE) are different. Windows is the operating system. WE is a program that runs on a Windows computer.)

Here's what happens when you copy a file:

File copying process

Figure 4. File copying process

  • You click and drag.
  • WE watches you, and figures out that you want to copy a file.
  • WE sends some commands to Windows.
  • Windows sends some commands to the disk drive (actually, the software drivers that control the drive).
  • The disk drive sends a response back to Windows, confirming that everything went OK.
  • Windows sends a response to WE.
  • WE updates the screen, showing you the new state of your computer:

Copy file result

Figure 5. File copied

Where is this happening?

You, the computer, WE, Windows, the hard disk, and the computer's screen are all in the same place.

When you copy a file, many thousands - if not millions - of pixels change. Here's Figure 2 again:

Copying a file

Figure 2 (again). Copying a file

As you drag the bug, you see a copy of the bug (or an outline) changing position as you move the mouse. That's thousands of pixels that need to change. When you let go of the mouse button, the window on the right is redrawn, showing that there's a new file in town.

Drawing all those pixels is not a problem for your computer. It has a fast CPU, a fast video card, a fast disk, and fast connections between them. It can move thousands of bits in a fraction of a second, without raising a sweat.

Now, how does this compare to the command line?

Copying a file - the command line way

When you use the command line, you're usually working on a computer that's far away. Right now, I'm in Michigan, USA. The Web server is in Texas, USA. It's over 1,000 miles away.

The server is just a computer. It has the same types of components as any computer, but the software is different.

Different software

Figure 6. Different software

My PC runs the Windows operating system. The Web server runs Unix (or a variant). My PC has Windows Explorer for letting me mess with files. The Web server has Bash for letting me mess with files. Bash is a "command shell." It knows how to interpret your commands and tell the OS what to do.

Here's the Windows file copy process again:

File copying process

Figure 4 (again). File copying process

It looks the same with Unix:

Copying files in Unix

Figure 7. Copying files in Unix

Oh, so far away...

Here's the important difference:

Far away

Figure 8. Far away

I'm in Michigan, typing, gesturing away. The computer doing what I tell it is in Texas. My gestures have to travel over 1,000 miles before they can be interpreted.

There's another problem as well. The reason shared hosting is so cheap is that many people use the same Unix computer, at the same time:

Many remote users

Figure 9. Many remote users

Using a GUI is not practical. Making changes to many GUI screens at once would overload the server. Sending the bazillons of bits for those screens would make things slow for users.

The solution is to:

  • Limit the input, the gestures people can make. Get rid of the mouse. Keyboard only. Key codes are small and fast.
  • Limit the output, the computer's responses to the gestures. No pictures, no icons, no sound (except for a beep). Only text. Text codes are small and fast.

When you can only use keyboard input and text output, the way you work changes a lot. It's generally more difficult.

How do you copy a file? There are no windows. No icons. Nothing to drag and drop. Nothing to drag and drop with.

Connecting

How do you get to your server's command line? The first thing to know is that sometimes you can't. Some Web hosting companies refuse to give you access.

Avoid those companies. Do business with a company that will give you "shell access" to your server. That refers to a command shell like Bash, that interprets your keyboard-only gestures.

All my accounts are with Hostgator; I've used them for years. They give you shell access, but you need to ask for it. Use the online chat support.

If you buy a Hostgator account using this link, this site gets a referral fee.

So you have shell access. Now you need to run a program on your computer that can connect. Most Windows users use PuTTY, a free tool. It will send your guestures (keystrokes only) to the remote computer, and show its responses (text only).

Download PuTTY. You'll get a single .exe file. It's not an installation program, it's the entire tool. Run it.

(Hint: You might want to create a directory like C:\Program files\PuTTY, put putty.exe in there, and make a shortcut to it on your Start menu.)

PuTTY will show you a configuration screen. The most important piece is this:

Starting PuTTY

Figure 10. Starting PuTTY

Type in your connection address, which is usually the same as your Web site's URL, without the http:// part. So if your site is at http://example.com, you would type:

Connecting

Figure 11. Connecting

Use SSH as the connection type. It encrypts everything, including the username and password that you're about to enter.

22 is the standard SSH port. Almost all hosting companies use that. Hostgator uses 2222 instead for its shared hosting plans:

Hostgator SSH port

Figure 12. Hostgator SSH port

You could just hit the Open button at this point, but I recommend making one more change. Click on "Colours" in the left window:

Figure 13. PuTTY colors

Then select "Use system colours":

Figure 14. Use system colours

This will make text easier to see.

Now click on the Open button to open a connection. You might see a message about a new key, if this is the first time you've used PuTTY with your server. The key is part of the encryption system used to protect your data as it goes flying about the Internet. Go ahead and tell PuTTY that the key is OK.

Logging in

Next you'll see a login screen:

Login

Figure 15. Login

The prompt ("login as:" etc.) comes from your server. If you see that, congrats! You've got some text from the server! W00f!

Login with the username and password associated with your account. It's usually the same info you use to login to your FTP account, when you upload things.

Next you'll see something like this:

[jsmith@server666 ~]$

This is the "command prompt." It tells you some useful things.

jsmithis the username, that you typed when logging in. server666 is the name of the server. Hosting companies have lots of servers. This is the name of your server, within their server herd. The $ is server-speak for "I'm ready."

The ~ (tilde, pronounced TIL-da) is important. It's your current directory. The concept of a "current directory" doesn't come up for most GUI users, so this idea takes getting used to.

In Windows, you have directories and subdirectories (or folders and subfolders, same thing). It's the same in Unix. There are directories with subdirectories, and so on.

Files and directories

Type ls and press Enter. ls is the command to list file names. You'll see something like this:

[jsmith@server666 ~]$ ls
logs  mail  public_html  welcome.txt

This is the contents of a directory. But which directory? There are thousands of directories on a typical server. Which directory has these four items?

The answer: the current directory. You can change the current directory up and down the directory tree. The same command - ls - shows you the contents of the current directory, whatever it is. Some examples in a moment.

What about that ~? That's Unix-speak for your home directory. Every Unix user has his/her own home directory. When you log in through PuTTY, that's where you start.

The command prompt usually tells you what the current directory is. Here's the first one again:

[jsmith@server666 ~]$

The ~ says "you are in your home directory."

Here's that ls command again:

[jsmith@server666 ~]$ ls
logs  mail  public_html  welcome.txt

These are the things in the home directory. The first three are subdirectories. The last one is a file. PuTTY might color-code them for you, depending on how things are set up.

public_html is a subdirectory of ~. It's actually the root of your Web site. So if someone types http://example.com/fetch.html in their browser, your server will send them the file ~/public_html/fetch.html

IMPORTANT! Not all servers are organized this way, though most I've seen are. YMMV.

Type cd public_html and press Enter:

[jsmith@server666 ~]$ cd public_html
[jsmith@server666 ~/public_html]$

cdis the command "change directory."

Look at the new command prompt. It's telling you that the current directory is now ~/public_html. So if you type ls, you'll see the files in that directory:

[jsmith@server666 ~/public_html]$ ls
CHANGELOG.txt    LICENSE.txt    includes

profiles         update.php     COPYRIGHT.txt
MAINTAINERS.txt  index.php      robots.txt

(more)

These are Drupal files. If you use WordPress or something else, you'll see different files.

Try this: type cd .. and press Enter. Then send the ls command:

[jsmith@server666 ~/public_html]$ cd ..
[jsmith@server666 ~]$ ls

logs  mail  public_html  welcome.txt

cd ..says to "go up one level." The prompt changes to ~, and ls shows you the files in the current directory, your home directory.

If you get lost in your directory tree, this:

[jsmith@server666 ~]$ cd ~

will always take you back to your home directory.

Logging out

The easiest way is to type Ctrl+D. That's right, just one character. Or you can type:

[jsmith@server666 ~]$ logout

Either works.

Bonus tip: Copy and paste shortcut keys

You're used to Ctrl+C and Ctrl+V. That won't work at the command line. Use Ctrl+Insert and Shift+Insert instead.

To sum up:

  • Use PuTTY or similar to connect to your far away Web server.
  • Commands like ls show you what's in the current directory.
  • You can change the current directory with the cd command.
  • If you get lost, cd ~ will take you to your home directory.

W00f! You're getting your geek on now!