Working on the Command Line

Getting to know your way around

Your First Terminal Session

Locate and open your terminal

  • Linux Users
    • Use the keyboard shortcut Ctrl-Alt-T
  • OSX Users
    • launch Finder
    • go to Applications > Utilities
    • click on Terminal
    • right-click on dock icon, click Options > Keep In Dock
  • Windows Users
    • you'll use git-bash instead of the windows command prompt
    • right-click on desktop
    • select Git Bash
    • if not already set, right-click taskbar icon and select 'pin this program to the taskbar'

Reading the Prompt

The first stuff you see in your terminal is called the prompt.

It will include your username, where you are, and what machine this terminal is on.

By default:

  • OSX:
machine_name:current_directory username$
  • Linux:
username@machine_name:current_path$
  • Windows (git-bash):
username@machine_name current_path
$

Home Is Where Your Stuff Is

In general, most systems will open a terminal in your home directory

This is a folder on the computer filesystem that belongs to you.

You put your work here, configure settings for many programs and so on.

You can see where you are using the shell command pwd

Type that command into your terminal now

COMMAND: pwd

The pwd command shows the present working directory

OSX

$ pwd
/Users/cewing

Linux

$ pwd
/home/cewing

Windows (git-bash)

$ pwd
/c/Users/Cris Ewing

CONCEPT: The Path

In any computer system, a path represents a location in the filesystem.

Paths are like addresses, listing a location from the general to the specific.

A bit like addressing an envelope backwards:

USA
Seattle, WA 98105
123 Somestreet
Cris Ewing

vs.

/home/cewing/projects/someproject

A path is absolute when it starts with /

A path is relative when it does not

QUESTION

Is the path returned by the pwd command absolute or relative?

Moving Around

Movement is basic to life.

All operating systems distribute resources among many directories

To be a power user of the command line, you must be able to move around.

The command for moving from one directory to another is called cd

You can supply a path to tell the terminal the address where you want to go

This path can be absolute or relative.

COMMAND: cd

The cd command allows you to change directories

It can take a path as an argument (absolute or relative)

QUESTION

What happens if you do not supply a path to indicate where you want to go?

To the Root

Before we begin, use pwd again to show the path to your home directory

Write this value down

You'll use it in a moment to get back

Now type the following command at your prompt:

OSX/Linux:

$ cd /

Windows:

$ cd /c

To the Root

You've just changed directories.

The address you supplied to the cd command was for the root of your computer

(Windows users, this isn't exactly true, but it's true enough for today)

This is the very topmost container in your filesystem

Everything else is located inside this directory, or in a directory whose path starts here.

paths that start with the root are absolute because there can be no ambiguity about where to start looking

CONCEPT: The Root

The root is the topmost container of a computer filesystem

Everything on the computer can be said to be contained in the root or in a container that is contained there

Absolute paths always begin with the root, so that there is no doubt about where to begin moving through the filesystem

The root is generally restricted to administrative users

You should never delete anything located directly in the root

Looking Around

Now that you're here at the root of your computer filesystem, you might want to see what's here.

To list the contents of your present working directory, you'll use the ls command.

Type this command at your prompt:

$ ls

Looking Around

You should see something like this:

OSX:

Applications    Users       dev     net     tmp
Library         Volumes     etc     opt     usr

Linux:

bin   etc   lib    lost+found  opt   run      srv  usr
boot  home  lib64  media       proc  sbin     sys  var

Windows:

$Recycle.Bin  Documents and Settings  Recovery                   autoexec.bat
BOOTSECT.BAK  PerfLogs                System Volume Information  bootmgr

Take a Closer Look

Like many unix-style shell command, the behavior of ls can be modified

By default, it shows the names of items in your present working directory in alphabetical order

By providing the command with flags, we can modify that

For example, the t flag changes the order of the listing so that the most recently modified items are first.

You can supply a flag to a command with the - sign, like so:

$ ls -t

Try it out now.

Prove it works

Clearly, the ordering changes, but is it really sorting by modification time?

The l flag changes the listing shown to long format

This will display permission, ownership and modification information about each item in the directory:

$ ls -l
drwxr-xr-x  2 root root  4096 Sep 23 12:08 bin
drwxr-xr-x  3 root root  4096 Nov  5 15:25 boot
...

You can combine flags by adding each to a single -

Try this:

$ ls -lt

Turn it Around

We have a command that lists items alphabetically

We can modify it to list them by modification date, with the most recent first.

What if we want to reverse the ordering?

The r flag (note that that is a lower-case r) does this.

Try using that flag by itself

Try combining it with the t flag

Try combining it with l and t

COMMAND: ls

The ls command shows you a listing of the contents of your present working directory

The behavior of this command can be altered by flags

The l flag returns the listing in long format

The t flag returns the listing ordered by modification time

The r flag reverses the order of the listing

CONCEPT: flags

Unix commands may be modified by providing flags

Each flag changes the behavior of a command in a single, simple way

The flags can be combined to produce more complex changes

Flags are provided by using the - sign after the command

QUESTION

How can you know what flags may be used for a given command?

RTFM

Unix-like systems provide a system command called man (short for manual)

You provide this command the name of another command

It will return a manual explaining how that command works and what options it provides

Type the following command at your prompt:

$ man ls

Windows users, this will not work for you

Instead, use your web browser and type the same command into your search bar

Teh Google will provide

COMMAND: man

The man command provides access to the built-in manual for all unix commands

Providing the command with the name of some other command will print detailed information about how that command may be used

Often these manual pages include useful examples for common and advanced usage patterns

Test it out

Spend the next five to ten minutes trying out different flags to the ls command

For each flag you try, make a prediction about the effect it will have

After trying it, review your prediction

Were you right?

If not, in what way were you wrong?

What happened that surprised you?

These sorts of surprises are the seeds of learning, treasure them

Going Home

Okay. Enough poking about here at the root

Let's head back to our home directory

You wrote down the path returned by the pwd command as we were leaving.

Can you get back there now?

$ cd /Users/cewing

Trouble Spot

Some of you may have a bit of difficulty at this point

For example, Windows users may have a path for their home directory that looks like this:

/c/Users/Cris Ewing
$ cd /c/Users/Cris Ewing
sh.exe": cd: /c/Users/Cris: No such file or directory

The problem is the space between my first and last names

The command line expects paths to be a single continuous string of characters

Spaces are used to delimit one element of the command line from the next

So how do we cope with this?

Escape from Space Mountain

The secret lies in tricking the command line into thinking that the space is just another character like any other letter or number

You can do this by escaping it with the \ character

Try typing this instead:

$ cd /c/Users/Cris\ Ewing

How'd that work?

What's in a Name?

This brings up the idea of unix naming conventions

In general, unix power users tend not to use spaces in the names they give to things.

Use dashes (-) or underscores (_) to separate words in names

This will help as you start working with software systems that don't play nicely with spaces in path names.

It gets easier to do this as you use the command line more

wHat'S IN a nAmE?

One other interesting issue to cover here is the question of case sensitivity

Linux is a case sensitive operating system

This means that the names foo.jpg and FOO.JPG are not the same

OSX and Windows are case insensitive, which means those two names are the same

(however, in terminals, unix command names are case sensitive)

Most unix power users tend to write all names in lower-case letters.

(bonus: you don't have to use the shift key when you do)

CONCEPT: Naming Conventions

Avoid spaces in the names you give to files and directories

Use dashes and underscores to create visual separation between words in names

Prefer lower-case letters in naming files and directories

Cutting Corners

So far, we've used cd to move to the root of our filesystem and back

Each time we've provided an absolute path as an address to where we'll go

But we can also use relative paths as addresses

When we do, they are considered to be relative to where we are now

In other words, relative paths are always construed by the OS as beginning with the value of pwd

Prove It Works

Use the ls command to see what you have here in your home directory

Use the l flag to get the long format

Notice that some of the entries in the list start with a d and some begin with -

Those that begin with d are directories, pick one

Use the cd command, providing the name of your chosen directory as a relative path

Now, make a prediction about the path of your present working directory

Finally, use the pwd command to confirm or reject your prediction

Repeat the exercise, moving further down from your home

Going Up

You've now moved several levels down from your home directory, but how to get home?

You could provide the absolute path of your home directory, you've done that before

But let's mix it up a bit

In unix systems, the symbol .. (two periods) stands for "one filesystem level above where I currently am"

You can use this relative path with the cd command to move up one level

Try it:

$ cd ..

Going Up

You can even chain them together, providing relative paths that go up more than one level:

$ cd ../..

And you can combine these with directory names to go back down into a different branch of your filesystem:

$ cd ../somewhere-else

Look Around You

Take the next five to ten minutes to explore your filesystem

Use the ls command to find directories in your present working directory

Use the cd command to move down into and back up out of various directories

Use relative paths and the .. symbol

The Express Train Home

Finish when you are somewhere not in your home directory

Do you know the exact command to give to get home from wherever you are?

Remember back a while, when you tried the cd command without a path?

Try it again:

$ cd

Where are you now?

Is that what you thought it did before?

Home by Another Name

You've seen how .. is interpreted by the command line to mean "one level up from here"

That's not the only symbol you have to work with

Use cd, ls and .. to move away from your home directory again

When you've moved away three or four steps, try this:

$ ls ~

That squiggle after the ls command is a tilde

You can find it in the upper left corner of your keyboard

Your command line replaces that symbol with the absolute path of your home directory

Listing Somewhere Else

One other item of note in that last command

You provided a path to the ls command, what was the result?

In fact, the ls command can list places other than where you are now, if you provide a path

Try listing the root: ls /

Try listing a directory located at the root

Try combining a path with flags to the ls command

Does the order in which you give the two make a difference?

Short For Here

In point of fact, if you don't give ls a path, it simply assumes you mean "right here"

You can make this explicit by using the . symbol (one period)

Just as .. means "one level up from here", . means "right here"

QUESTION

Does ... mean "two levels up from here"?

CONCEPT: shortcuts

You can use the .. symbol as an element in a path (absolute or relative) as a shortcut for "one level up"

You can use the . symbol as an element in a path as a shortcut for "right here"

You can use the ~ (tilde) as a shortcut for the absolute path of your home directory

You can use the cd command without an argument to return to your home directory immediately from anywhere

Review

Take a moment to reflect

COMMANDS

You've learned and used the following commands:

command purpose
pwd obtain the absolute path of the present working directory
cd change directories to the path provided (or home if none)
ls list the contents of the provided path (or here if none)
man get information about the options and usage for any command

CONCEPTS

You've also learned the following concepts:

The Path
An address for a filesystem location, either absolute or relative
The Root
The location at the top of the filesystem which contains everything else
Flags
Symbols provided at the command line to alter the behavior of commands. They may be combined to achieve complex changes
Naming Conventions
Common practices around the naming of things adopted by power users, usually in accomodation of some aspect of the command line
Shortcuts
Ways to provide potentially long values by using a much shorter symbolic representation

IMPORTANT NOTICE

Don't close your terminals yet!

Take a Break

You've earned it

Next Up

<Thank You!>