Git Product home page Git Product logo

cli-essentials-bash-navigation-sfo-web-021720's Introduction

Navigation Commands in Bash and Zsh

Learning Goals

  • Demonstrate How to Navigate with bash
  • Identify our "home directory"
  • Identify my logged-in username with whoami
  • Identify the "current working directory" with pwd ("print working directory")
  • Navigate up one directory in the file system
  • Change directories using cd

Introduction

Using the CLI (command line interface) might seem like a big challenge to first- time users who are afraid of making mistakes that could break their computers or ruin their files. Fear not! We'll step you through it.

The command-line interfaces, or "shells", used on OSX, Linux, and the "Windows Subsystem for Linux (WSL)," are called bash. We'll document how to use the bash CLI in this module.

Demonstrate How to Navigate with bash

To review: bash is a text-based interpreter that provides a command-line interface for controlling your computer (or operating system).

ASIDE: Bash is actually an acronym which stands for Bourne-Again SHell. As the word "again" suggests, there are other shells, some of which came before bash. There are also shells that have come along since bash. Nevertheless most programmers use bash or something very similar.

A great place to start learning about the CLI is by using it to do a task you're already familiar with: looking to see what's inside folders like your directories and desktop. Programmers call this activity: navigating. In the CLI we imagine that we're "traveling" to different places. We'll use metaphors like "go into the folder" or "go up one folder" or "visit the location at such-and-such path."

As you learn to navigate with the shell and get to used to it, you'll see that it's friendlier than it might seem at first. In no time, you'll be looking like this typing-machine from "Ghost in the Shell:"

"Ghost in the Shell"

Identify Our "Home Directory"

Whenever you open a terminal session (new window, new terminal tab, launching the program for the first time after a reboot), you will be placed in (remember our "travel" metaphor!) your home directory. Directory names on Unix systems are written like:

/parentdirectory/subdirectory/another/subdirectory/

We call a bit of text meant to communicate location on a file system a path. Paths use / to show levels of "nesting."

For example, a user's home directory is often written like: /Users/username or /home/username. The / on the far left of the path name means the very top of the file system "tree." The / directory is also called the "root" directory. It contains all the "top-level directories" that can contain sub-directories (...which can contain sub-directories, which can contain sub-directories, on and on).

NOTE: When navigating through a directory, it often helps if we picture the file structure like a tree. With this visualization, we can refer to "moving up" or "moving down" between directory levels, and keep better track of not only where our files are but where we are among our files. Here's an example:

"Directory tree structure diagram"

Let's take a second to look at some typical home directories:

/Users/username

This means the "root" contains a directory called Users and Users contains username. Paths like this are typical for Mac OSX.

/home/username

This means the "root" contains a directory called home and home contains username. Paths like this are typical for Linux.

Obviously, our names (well, most of our names) are not username. Instead we log into our systems as Byron Poodle or Nancy the Cat. How can we find out what our logged-in-user name is?

Let's ask the shell to tell us who it thinks we are. We'll use whoami to do just this in the next section.

Identify My Logged-In Username with whoami

Let's start simply. Let's ask the computer who I am logged in as:

$ whoami

"whoami"

The whoami command lets you see which user account you're logged in to from the CLI. This might seem obvious, especially if you're logged in on your personal computer. But Unix machines have multiple accounts by default (though you may not have seen them yet).

My system says I am kellyegreene. Based on what we learned about home directories, what do you think my home directory path is? In the next section, we'll ask the shell to tell us what our home directory's path is.

Identify the "Current Working Directory" With pwd ("print working directory")

$ pwd

Note: Any time you see the $ character, you shouldn't type it in. This is just a standard way to represent a bash prompt. Yours may or may not be a $.

You should see some output describing the directory you are currently in. It's probably something like /Users/byron_the_poodle.

The pwd command stands for "print working directory". As you "navigate" your file system, you might get lost. Just like wandering in a big city, you can look for street signs to find out where you are. The pwd command acts like those street signs. You'll never be lost again!

We've just used pwd to demonstrate that when we open a bash session the operating system automatically "puts" us in our home directory. Let's now do some real "navigation." In the next section we'll learn to move "up" one containing directory.

Navigate Up One Directory in the File System

Try typing this in the command line:

$ cd ..
$ pwd

You "moved up" one level of nesting, so you should now see that you are one level up from where you were and one level closer to the "root" directory. In your terminal see this by using pwd:

/Users

The cd command stands for "change directory".

The .. is a shortcut for the directory above the working directory. The bash shell provides a series of "shortcuts" for some commonly-used file system paths. .. means "this directory's containing folder. These shortcuts look strange but they're designed to be short and therefore easy to type and therefore fast and, as we've hinted, the CLI is all about speed.

So in this command example we said: change directory to the parent folder. You can do this all the way back up to the / directory.

Let's use cd to get back to our home directory.

Change Directories Using cd

The bash shell provides default shortcut for your home directory: ~. Use cd .. to go "up" a few directories (why not all the way to the root?).

Then, when you run this command:

$ cd ~
$ pwd

You'll see you're back in your home directory. Use pwd to verify!

SHORTCUT By the way, if you enter cd with no argument, you'll be taken to your home directory.

Another shortcut, that might seem not too useful at first, is . meaning "the current directory I'm in.

If you try this command:

$ cd .
$ pwd

You should see you are still in the same directory where you wrote the command.

Instead of using shortcuts like .., ., or ~, you can give a path to cd and it will "take" you there.

Try cd /Applications or even cd /. You can run a pwd in these directories and see that you've "navigated into" them. Try cd alksdjfalksdjfalskdjf. What error does bash give you? Is that what you expected? We'll discuss more about the types of paths you can give bash in the next section.

Paths in Shell

The path supplied to the cd command can be either absolute or relative paths. An absolute path is a path that always gets you to the same folder. You can recognize them because they start with /. For example /Users/kellyegreene, is an absolute path.

A relative path is a path relative to the working directory you're "in" at the time you write the command. They start with the name of a directory or a file. For example kellyegreene/Documents, is a relative path.

If I were in my home directory /Users/kellyegreene and said cd mixtapes/the-masked-rapper-vol-1, it would work! If I were in /Users/annoyingbrother and said cd mixtapes/the-masked-rapper-vol-1, bash would return an error because that sub-directory doesn't exist there (because I, Kellye Greene, am the Masked Rapper, while my brother can't rhyme).

Absolute paths and relative paths might sound confusing in CLI universe, but we intuitively understand this difference in our day-to-day lives. We usually call this difference context.

If someone asks if you want to go get a slice of pizza for lunch they're probably thinking of somewhere close to where you're currently located – somewhere close relatively speaking. They're expecting you to say something like:

"Oh yeah, let's go West on 18th street, cross 6th Avenue and go to the place on the corner."

What would surprise the heck out of them would be if you gave them absolute coordinates; and it would be even more surprising if that location were far away. How might they react if you said:

"Oh yeah, let's go to 41.890221 and -87.633904!"

Latitude and longitude give absolute directions based on the Equator and Prime Meridian. They're not commonly used by humans to make decisions on where to get lunch (even if they point, in fact, to one of our favorite pizzerias in Chicago).

So far we've been finding out where we are in the file system "tree," how about we find out what's in these directories (besides other directories)? We'll cover that in our next lesson.

Time-Saving Tip: Tab Completion

As you type in commands in the shell, you can use "tab completion." Tab completion allows the shell to be smart and to try and guess what command you want to run when you hit the tab. If there's only one logical way to complete your command, bash will fill in the rest for you, or will show you the possibilities and you can add more letters until you can tab-complete your command.

For example let's say we have the following directory structure with two sub- directories:

flatiron_school/
flatiron_building/

If I type $ cd f and then hit tab, it will fill in everything up until the conflict so I'll see $ cd flatiron_. If I then add the s and hit tab it will fill in $ cd flatiron_school and I can hit enter.

Conclusion

As we keep exploring and working with the command line, we will start to unlock and understand its full potential! Adopting the terminal can allow us to become more productive users.

Resources

cli-essentials-bash-navigation-sfo-web-021720's People

Contributors

drakeltheryuujin avatar maxwellbenton avatar jenmyers avatar sgharms avatar

Watchers

James Cloos avatar Kaitlin Vignali avatar  avatar Victoria Thevenot avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar Ben Oren avatar Matt avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León avatar  avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.