DavidBaumgold.com

Create

Work

Play

Getting to Know the Command Line

The command line is the ultimate station of power on your computer. With it, you can perform amazing feats of wizardry and speed, taming your computer and getting it to do precisely what you want. Unfortunately, the price of this power is complexity: nobody ever said that ruling your computer would be easy.

The command line is, at its heart, simply a place where you type commands to the computer. The computer is your obedient servant, and will attempt to carry out any command that it understands. Unfortunately, the computer does not speak English, or any other language spoken by humans. In order to give it commands, we must first start learning the language of the computer.

NOTE: The command line, as with all power, has its risks. You have the capability to instruct the computer to do anything it has the capability of doing. If you instruct the computer to erase all of your data, it will cheerfully proceed to do so. Do not run a command just to see what it does. Make sure you understand what the command is supposed to do first, especially if the command involves changing or removing files.

Finding the Command Line

Most people don't use the command line on a regular basis, so it can be a bit difficult to find the first time. The Windows operating system doesn't even have a proper command line built in -- to execute these commands, you will have to install it.

Mac OS X: The Mac command line is a program called Terminal. It lives in the /Applications/Utilities/ folder. To find it, go to your Applications folder. Near the bottom, there is a folder called Utilities. Go inside, and one of the applications listed is called Terminal. Double-click that application to open it.

Linux: The location of the command line depends on whether you are using the Gnome or KDE window manager. (If there is a big K icon on the bottom left of the screen, you are using KDE; if not, you are using Gnome.) If you are using KDE, click the K button, select System, and click on Konsole. If you are using Gnome, click the Applications button at the top left, select System Tools, and click on Terminal.

Windows: Unfortunately, you will have to install your own command line program. Windows comes with a command line, but it is non-standard and more difficult to use. Cygwin is a free, easy to install command line program. Simply download the Cygwin installer, double-click it to install Cygwin, and then move the installer to the recycle bin. To use Cygwin, go to the Start menu, select Programs, and click on Cygwin.

Command Syntax

Nobody likes grammar, so let's get this over with quickly. All commands have three parts: the command itself, the flags, and the arguments. The command itself always comes first. The other two parts have different rules, depending on which command you are using: you may not have to use any flags or arguments at all.

Here is a sample command that you might type into a command line:

$ ls -l ~/Desktop

The $ is a commonly used symbol to indicate the command line. It simply means that the rest of the line is a command, rather than a sentence. When you are typing a command into the command line, you do not type in the $ because it is simply an indication to the reader, rather than part of the command itself.

The ls is the command itself. In this case, the ls command tells the computer that we want to look and see the contents of a folder.

The -l is a flag that alters how the command operates. In this case, we are signifying that we want a lot of data about the contents of the folder, so we want the data in a long format.

The ~/Desktop is an argument that specifies where we want the command to operate. In this case, we want to look at the contents of the desktop.

In all cases, to submit a command to the computer, press enter. Now, let's start learning some useful commands!

Basic Commands

Lets start by looking around your computer. The first command we will learn is ls, which we use to look and see the contents of a folder. Try typing ls into the command line and pressing enter. The computer will reply with a list of names. These names are the names of files and folders in the directory you are currently in. Whenever you open up a new command line, you start in your home directory, which is the directory that generally contains all of your files.

Well, that's nice. But what if we want to go someplace else? The next command we will learn is cd, which will let us change directory to another folder. cd requires an argument: if you tell the computer you want to go somewhere, you also have to tell it where you are going. Try entering this command:

$ cd Documents

Remember, do not type in the $ at the beginning, and press enter once you have finished typing. The computer will not reply, but you are now sitting in your Documents directory. You can test this by running ls again: the list of names will be different.

So where do we go from here? How do we know which of these names are folders (that we can go into) and which are files (that we can't)? For that, we need more information from the ls command. Let's give it the -F flag to tell us about files and folders. Try entering this command:

$ ls -F

You will notice that this time, some of the names that the computer returns to you will have a slash after them. These names are folders: the rest are files. You can always cd into a folder by running cd with the folder name as an argument, as long as you can see that folder with ls -F.

When you're done looking in folders, it's time to go back up. But how? Luckily, every folder contains a hidden link back up. To see these hidden links, we will use the -a flag for ls to see all. There are at least two hidden links in every folder. The . (one period) link takes you back to the same folder you are currently in -- it doesn't take you anywhere. The .. (two periods) link takes you back up to the parent folder. In fact, you can give the ls command multiple flags, like so:

$ ls -aF

If you run this command, you will notice that the . and .. hidden links have slashes next to them, which means you can use them with cd! To go back up a folder, you can always run:

$ cd ..

One last command: If you ever get lost while looking around the command line, you can use the pwd command to see where you are. pwd will print your working directory, giving you a trail of breadcrumbs all the way down from the top level of the computer to where you are.

Neat Tricks

Computer programmers are lazy. Because they are lazy, they invented some techniques to do more with less work. Here are some of those techniques:

Tab Autocompletion

Whenever you need to type out a location in an argument (for example, in the cd command), you don't have to type out the whole thing: the first few letters will do. Once you've typed three or four letters, press the tab key, and the command line will fill in the rest for you! For example, if you are in your home directory, and you type cd Desk and then press the tab key, the command line will automatically complete the command to read cd Desktop! You can also use this if you find yourself mistyping folder names: tab autocompletion will always fill it in correctly.

Shortcuts

The command line has a few shortcuts built in. For example, to see your previously typed command, just press the up button. You can do this to submit the same command multiple times, or to edit a command that you didn't type in quite right. Another shortcut: you can use ~ to refer to your home directory: cd ~ will take you back there.

Warnings

Remember, when you use the command line, the computer will cheerfully do anything it can for you. If you ask it to do something bad, it will try to do so. Some people take advantage of this fact by telling novice command line users to run commands that do nasty things to your computer. Here are a few to watch out for. Never run any of these commands! They can and will destroy your computer!

Delete Everything

The command to remove a file is rm. You can also use rm to remove multiple files at a time. The most extreme case is a command like this:

$ rm -rf /

This command tells the computer to start at the top of the file structure, and delete every single file on the computer without stopping. After this command has run, your computer will not be able to run, and if you turn off your computer, it will not be able to turn back on until you reinstall an operating system on it.