-
Notifications
You must be signed in to change notification settings - Fork 4
The Command Line
Table of Contents
- Code & Quiz
- What Is This Thing And How Do I Stop It?
- The Original Handshake
- A Program That Runs Programs
- Nomenclature
- Textual Interface
- The Prompt
- You Are Here
- Bash
- The Executioner
- Executing a Command
- Arguments
- Flags
- Command Not Found
- Practical Stuff
- Cursor Movement
- Fixing Mistakes
- Killing a Process
- Navigating the Filesystem
- Command History
- Superpowers
- Become a Superuser
- Finding the Terminal
- Opening a Terminal on Cloud9
- Opening a Terminal on Mac OS X
- Opening a Terminal on Windows
- More Learning Resources
##Code and Quiz
##What Is This Thing And How Do I Stop It?
###The Original Handshake
The command-line interface is a powerful text-only tool for interacting with computers, and until the advent of GUI (Graphical User Interface) based operating systems, like Windows or Mac OS, the command-line was the primary tool for controlling computers.
###A Program That Runs Programs
What is the command-line? The command-line itself is essentially a program, a command interpreter, that on our behalf, executes other programs, often called jobs
. The executable program run by the command-line can be a utility or a server of some sort, or any number of applications.
An operation system is comprised of two main parts: The first main part is the kernel, responsible for allocation of machine resources to the system programs. System resources include like memory, disk space, and CPU cycles. The second part are the system programs, which includes drivers, applications, shells, and any other file.
Falling into the second main part of an operating system, the command-line belongs to the system programs.
###Nomenclature
People often use the terms terminal, shell, and sometimes prompt or command prompt, to mean, the command-line interface. These terms are synonyms, so if you hear or read, go to the prompt, or, open a shell, that is to say, go to your terminal.
###Textual Interface
The command-line is a textual interface, meaning it only deals with text input and displays text output. It's as simple as that: no graphics. In fact, on all GUI operating systems, like Mac OS X or Windows, the terminal is actually a terminal emulator, that is, a GUI application designed to look and act like the terminals that ran on older systems without graphics processors.
###The Prompt
On a Linux operating system, when you launch a terminal, you are often greeted by an issue message, identifying the version of Linux and perhaps the type of device you're using. You'll then be presented with a prompt, often comprised of some information, a prompt symbol, then a cursor at which you can type text to enter and execute commands.
The prompt symbol will often be a $
or #
or sometimes a >
character. The prompt character can be configured, so that's why you sometimes see a variation.
On Cloud9, the bash terminal will print:
- The name of the user with which you've logged in to the system
- Next, an
@
symbol, then a domain name. - Finally, following the username and domain name is the directory path in which you are currently situated.
The following image ends with even more information (master)
, which is the git branch in which we're working. Notice also in this image from the Cloud9 workspace, the cursor is currently black, and this signifies the terminal has the focus.
TIP: In Cloud9, you must always click the terminal window-pane to give it the focus. If the terminal does not have the focus, the terminal cursor will be a non-filled outlined retangle, and not filled-in with the color black.
When we say focus, we're talking about applications who's elements can receive the focus of input or control: sometimes you can tab through focus, like on a web-form, and often times you can also click-in to the element to give it the focus. It can be a bit confusing in Cloud9 to remember to give the terminal focus, because when you're editing code, the text-editor has the focus, and often you'll need to switch between the two panes in the IDE.
##You Are Here
One thing that seems to confused newbies is the fact that when using a terminal, you're always executing your commands from somewhere on the file system.
The idea that you're inside a directory can be a source of confusion, probably because we're so use the the GUI version of seeing the filesystem with folders and files, and we may not be use to the text representation of the filesystem as presented to us in the terminal.
Try to get use to the concept that as you're executing commands, you're executing them from within whatever directory you are currently situated.
Sometimes, you might need to move to another directory to execute a command that needs access to files expected to be in the present working directory
. To this end, navigating the filesystem on the command-line will be something you do often, and you'll see some examples of doing so, shorty.
On Linux distributions, by default, when you start a terminal, you'll start in the user's home directory.
As mentioned, by default, the prompt should show you the path to where you are in the filesystem. In the image below, you'll see in blue, the file path stated in the prompt, it reads ~/workspace
.
The ~
symbol is a shorthand for the user's home directory. On Linux, the home directory for a particular user shares the user's username, so if your user name is jfraboni
, the full path to your home directory will be /Users/jfraboni
. That's alot to type, so thus the shorthand on Linux, ~
.
##Bash
There are many types of command interpreters, and the one we'll be dealing with is bash running on a Unix-like system. Bash stands for Bourne Again Shell, a play on the fact that it is a superlative version of an older command interpreter named for and written by a guy named Bourne.
TIP: If you want to know which command interpreter you're running, type the following command at the prompt:
myuser@myproject:~/workspace (master) $ echo $0
bash
This will expand and return the name of the shell you're using or path to the executable of the shell.
##The Executioner
###Executing Commands
We execute commands on the computer by inputting the name of the command, like ls
or pwd
, sometimes accompanied by one or more arguments, then pressing the Return/Enter
key. If we're adding arguments and or flags, we must put a space between each discrete eletment of the command, that is, a space after the command name, a space between arguments, and a space between flags and their inputs, if any.
The ls
command stands for list services, and will print a list of all files and sub-directories within the current directory. The pwd
command stands for present working directory, which prints the path to your current directory. Both ls
and pwd
are examples of commands that can be executed without arguments.
$ find / -name foo
In example above, the command is find
(a command to search the filesystem for files), and we're passing in the first argument of /
(which here means start the search from the root directory), then we're passing the -name
flag with a value of foo
(which specifies the name on which to perform the search for files).
###Arguments
Command-line arguments are values or information we pass to a command. The command can be written to either expect arguments or optionally handle them.
The list services
command optionally takes a path to a directory for which you want to list all contents, services.
Running the command like so:
$ ls
...lists the services in the present directory. Running the command with an optional path as an argument, like so:
$ ls path/to/some/directory
...lists all of the services in the directory at path/to/some/directory
.
###Flags
Commands can optionally handle flags. Denoted by the -
prefix on a letter or word, flags can behave like switches: if passed, the switch is on, if not off, etc. Again, the ls
command can be executed with the -a
flag, which means, include any hidden files:
jfraboni@shoutout:~/workspace (master) $ ls -a
./ .bowerrc .git/ README.md config.xml hooks/ package.json plugins/ www/
../ .c9/ .gitignore bower.json gulpfile.js ionic.project platforms/ scss/
ls
can be executed with the -l
flag, meaning, show the long form, and will provide extra information on each file, such as permissions, the owner of the file, last edited date. Here's the details of a typical project:
jfraboni@shoutout:~/workspace (master) $ ls -l
total 44
-rw-rw-r-- 1 ubuntu ubuntu 207 Dec 5 16:59 README.md
-rw-rw-r-- 1 ubuntu ubuntu 123 Dec 5 16:59 bower.json
-rw-rw-r-- 1 ubuntu ubuntu 804 Dec 5 16:59 config.xml
-rw-rw-r-- 1 ubuntu ubuntu 1353 Dec 5 16:59 gulpfile.js
drwxrwxr-x 3 ubuntu ubuntu 4096 Dec 5 16:59 hooks/
-rw-rw-r-- 1 ubuntu ubuntu 40 Dec 5 16:59 ionic.project
-rw-rw-r-- 1 ubuntu ubuntu 360 Dec 5 16:59 package.json
drwxrwxr-x 4 ubuntu ubuntu 4096 Dec 5 16:59 platforms/
drwxrwxr-x 5 ubuntu ubuntu 4096 Dec 5 16:59 plugins/
drwxrwxr-x 2 ubuntu ubuntu 4096 Dec 5 16:59 scss/
drwxrwxr-x 8 ubuntu ubuntu 4096 Dec 9 00:34 www/
Some flags can accept values, and as such, are like named arguments, like we saw with the find
command:
$ find / -name foo
Here' -name
takes the value foo
.
Flags don't have to be in any particular order, and single letter flags can be compressed together, like -rf
, which might mean, recursive and force.
###Command Not Found
If the command we're trying to execute is installed on the system, the command-line interpreter will find the application responsible for handling this command, execute it, passing to the application any arguments or flags we've provided. If the command is not install, the terminal will report by saying:
myuser@myproject:~/workspace (master) $ opsparkrocks
bash: opsparkrocks: command not found
myuser@myproject:~/workspace (master) $
TIP: Remember, to execute a command, you first type the command, then you MUST press the Return/Enter key - this gives you the chance to check your typing!
##Practical Stuff
##Cursor Movement
Command | Action | |
---|---|---|
left-arrow |
: | Goes left, one character at a time |
right-arrow |
: | Goes right, one character at a time |
control-a |
: | Goes to the beginning of the line |
control-e |
: | Goes to the end of the line |
control-xx |
: | Toggle between start of line and current cursor position |
###Fixing Mistakes
Nobody is a great typer, that's why it's not an Olympic sport - you'll make mistakes at the command-line often.
Getting use to correcting yourself quickly should be one of the first things you learn:
On a Mac, using the control
modifier key, we have access to several shortcuts to improve our command-line input:
Command | Action | |
---|---|---|
delete |
: | Erase one character at a time |
control-w |
: | Erase one word at at time |
control-u |
: | Erase the whole line from the cursor to the prompt |
control-d |
: | Erase the next character forward |
control-h |
: | Erase the previous character forward, same as delete
|
control-k |
: | Erase to the end of the line |
control-y |
: | Paste the last deleted character, word or line |
##Killing a Process
We use the terms process, job, service, command and application interchangeably - they mean, the program that runs when we execute a command.
You'll often need to kill a service or process that is running, maybe because you've launched it prematurely, launched the wrong command, it's time to shutdown a server, or the process is taking too long.
To kill a process on Linux, we can ask the operating system to send our process the TERM signal by typing control-c
. Here's a list of other handy kill type signals:
Command | Action | |
---|---|---|
control-c |
: | Kills any running process |
control-z |
: | Suspends any program |
fg |
: | Foreground, will resume a suspending program |
###Navigating the Filesystem
As mentined above, when starting a terminal, by default you'll be in the user's home directory. But you'll never stay on one place; you'll need to shuttle around the filesystem to get your work done. To navigate on the filesystem, you'll be using the cd
command, which stands for change directory.
cd
is a command that requires an argument, which must be the path to directory to which you want to change.
cd ~/workspace
Executing the above sequence will bring the user to the Users/myuser/workspace
directory.
You can up one directory by passing ..
to the cd directory:
jfraboni@shoutout:~/workspace (master) $ cd ..
jfraboni@shoutout:~ $
After execuing cd ..
, we can see we're now in the home (~
) directory, and git branch notice, (master)
, has been removed from the prompt because there's not git repository in the home directory.
cd -
will take us back to the previous directory.
jfraboni@shoutout:~ $ cd -
/home/ubuntu/workspace
jfraboni@shoutout:~/workspace (master) $
In this case on Cloud9, the ubuntu directory represents the home directory, as each Cloud9 workspace is spooled-up with it's own home directory, and a user can have multiple workspaces.
##Command History
You can see the history of the commands you've executed by using the up and down arrow keys. Doing so will cycle backwards and forwards through the history of your work, and this is handy for re-executing commands you'll use often, or, if you make a mistake and want to correct and run the command again.
Command | Action | |
---|---|---|
up-arrow |
: | Shows the previous command executed |
down-arrow |
: | Shows the next command executed, if any |
control-p |
: | Shows the previous command executed |
control-n |
: | Shows the next command executed, if any |
##Superpowers
The terminal still offers the most powerful way to interact with Unix-like Systems. As you become proficient as a developer, you will find yourself using the command-line more and more to assist in your work, so it's best to get use to it at the outset. In no time, you'll be controlling your computer like a pro.
While it may seem like clicking buttons or selecting menu options with your mouse is a more effective way of interacting with computers, for many reasons, the command-line is still offers more control.
Some reasons this is true are:
-
Firstly, using a mouse slows you down - you have to take your hand off the keyboard, grab the mouse, and start looking for UI elements that match your needs, and this breaks your focus, taking you away from your active, creative and productive thought stream.
-
Secondly, some commands available to you in a shell outstrip the features of the GUI equivalent.
-
Finally, you can quickly write powerful shell scripts, programs that run in the shell interpreter, that can perform some dazzling computation.
##Become a Superuser
Learning to use the command-line effectively can seem daunting when you're new at it. All these phrases you type into this black window, some take input, how does anyone get good at this? I find the best way to learn is to have a reason to learn, and you'll certainly have that reason as you progress in learning to write apps.
Here's some places to start:
##Finding The Terminal
###Opening a Terminal On Cloud9
On Cloud9, the terminal is usually open by default, and located in a tab of the Console View, usually positioned at the bottom of the screen slightly to the right-side. The command-line tab will say "bash", followed by your username. Your Cloud9 color configuration may be different from these screenshots, but here's an image showing the bash terminal on Cloud9:
If you cannot see the Console View, try selecting from the Cloud9 menu bar View > Console
. Check out this screen shot:
If the Console is open, but your command-line was closed (you can close tabs in the Console View by clicking the "x
" on each tab, and sometimes this confuses newbies), select the "+
" sign to the right of the last open tab on the Console, then select New Terminal
. Here's another screenshot:
###Opening a Terminal on Mac OS X
On Mac OS X, open spotlight with the keystrokes, Command-Space, then start typing "Terminal". The Terminal program will show up in the dialogue box and you can simply select it and hit return - this will launch the terminal.
We highly suggest installing iTerm2 - it's a great terminal program with more advanced features than the stock terminal that comes with OS X.
###Opening a Terminal on Windows
On Windows, press the Windows button, then type cmd
into the prompt dialogue, then hit return, this will launch the terminal on Windows. The commands on Windows are often different from Unix-like systems, so keep that in mind. If commands we discuss are not found on Windows, you can Google for solutions to installing GNU equivalent for Windows. Even if you're working on a Windows machine, the exercise below is meant to be completed in Cloud9, which uses an Ubuntu operating system, which is a Linux distribution, and by default uses a bash
terminal.
##More Learning Resources
One of the most powerful features of the Internet is that others have shared their knowledge, so instead of re-inventing the wheel on this one, I'd like to shout out to Zed A. Shaw, who took the time to write and share an excellent primer on, among other topics, learning the command-line, and so I highly recommend going through these lessons:
© 2014-2015 Operation Spark