The Command Line and You - Part One

I'm teaching a group of students in a Coding Bootcamp, and one of the first things we're learning about his how to get around a Command Line Interface -- in this class, we have a mix of Mac and PC users, so the former is using Terminal, while the latter is using Git Bash.

I'm "seasoned" enough to have worked on DOS computers, where the first thing you saw when you turned on the computer was the blinking cursor next to the C:\> Prompt, very much like what you see in the Windows Command Line (cmd.exe) today. From there, you issued commands to navigate your file structure and launch programs. We didn't know anything else. You had to know your way around the command line to get anything done, like copying files to a floppy disk or launching
WordPerfect.

Then along came Windows (okay, MacIntosh, then Windows) and for a while there, the Command Line was relegated to an "It's there in case you need it" role. Hidden away, as the computer operating system makers touted the wonders of the Graphical User Interface.

Now, it's back, driven by Linux and the Open Source World, and is necessary to do anything with modern web development. Node.js is the runtime of choice for the tools you'll use, and Git Version Control also lives on the Command Line. True, there are a growing number of GUI options for these, but you will spend a great deal of your life as a web developer on the Command Line, so it's a good idea to understand it.

Part One - Folders and Paths

I'm going to use Git Bash for this article, but it is virtually identical to Mac Terminal in terms of how it works, and you should be able to follow along with the concepts.

I'm also going to assume that you have Git Bash installed, the easiest way is to install Git for Windows.

If you don't have a shortcut for Git Bash on your Desktop or Task Bar, you can hit the Windows Key and type "Git Bash" and launch it from the start menu. (Mac users can similarly launch Terminal from a Desktop shortcut, the Launcher or Spotlight.)

When it opens, you'll see some text related to your user and computer name, the name of the program shell you're working in (MINGW64 refers to MINimal GNU for Windows 64bit version). Then a tilde (~) and on a new line the $ prompt. (Mac users will see something similar, but the prompt will be on the same line.)


The ~ character is a shortcut for your home directory. What is your home directory? Good question!

Home Folder: The location on your computer's hard drive that stores all files related to you, things like Documents, Desktop shortcuts, Pictures, Music, Favorites, etc.

That's a simple definition, but I hope it gets the point across. The important part to know is that for most everything you create, you'll want to do so within this Home Folder.

If you're curious as to where exactly this is, let's try out an actual Command Line Interface command.

pwd: Print Working Directory -- shows you what directory you are currently working in.



On my computer, when I type pwd followed by [Enter] it shows up as /c/Users/rhosek. Mac users will see a similar path to their home folder.

If for some reason you don't see something ending in "/Users/{username}," then you may have launched Git Bash (or Terminal) from a different starting location. No worries, let's learn another command.

cd: Change Directory -- changes to the working directory to a different location

If you type cd ~ (I'm going to assume from here on out that you will also press [Enter] after typing out the command) at your prompt, it will Change the Working Directory (cd) to your Home Folder (~) and entering pwd at this point will give you the result we're expecting.

So, let's really understand what's happening here. You typed cd, then a [space], the ~ character and then [Enter]. cd is the command. Everything after the [space] are the parameters (sometimes called options). In this case, we have one parameter, ~, which as we learned earlier is a shortcut for your Home Folder. So, the cd command executes, notes that you have specified a parameter (~) and changes the Working Directory to the value of that parameter -- your Home Folder.

You could have alternatively entered cd /c/Users/rhosek and it would have done the exact same thing since ~ = /c/Users/rhosek (on my computer, yours will be different).

Let's explore cd a little more. There's another shortcut like ~ that comes in handy, and it's .. which is a shortcut for Parent Directory. Unlike the ~, it doesn't point to a specific folder on your hard drive, it refers to the parent of your Working Directory.

If we look at the structure in our current example, we can envision that each part of the path of the Working Directory is a different level.

/                      <-- Root Directory
    c/
        Users/         <-- Parent Directory (one level up)
            rhosek/    <-- Current Working Directory

So, Users/ is the Parent Directory of rhosek/ and c/ is the parent directory of users/ and / is the parent directory of c/.

Wait, what? /?

Yep, the / by itself is the root of your Directory Tree. if you entered cd / the Root Directory would become the Working Directory. But we want to stay inside your Home Folder for now.

But back to ..  If we enter cd .. and then enter pwd we'll see that the Working Directory is now the Parent Directory of rhosek, or /c/Users. You don't have to use special characters like ~ or .. with cd, you can also specify a folder name. Entering cd rhosek will take us back to the Home Folder, since we are in its Parent Folder.


It's important to always realize where in your Directory Tree you are so that you can change directories correctly. If I was in the Root Directory and entered cd rhosek, I would get an error, since the cd command cannot find the rhosek folder in the Root Directory. Instead, I would need to enter the entire path, cd /c/Users/rhosek.



Path: The chain of directories (folders) that indicate where a file or folder lives relative to the Root Directory or another folder.

If you have sharp eyes, you noticed something different about the two cd commands in the last screenshot. The first one does not have a leading / in the directory parameter (the folder we are changing to), it's just cd rhosek. If you start the directory parameter with a straight-up folder name, it will look for that folder within the current Working Directory. In this case, since there is no rhosek folder in the Root Directory, bash told me so.

But, when I add the / to the front of the directory parameter of my cd command, I am telling it to start looking in the Root Directory (remember / = Root Directory), find the c folder, then the Users folder inside the c folder, then the rhosek folder inside the Users folder. We're following a path to our ultimate destination, rhosek (our Home Folder).

When you start a path with the / (Root Directory) you're specifying an Absolute Path. That means, the cd command will always start resolving the path at the Root Directory. Starting your path with ~/ is also a version of an Absolute Path since ~ = /c/Users/rhosek (which starts with /).

When you start the path with a folder name you are using a Relative Path, and it is assumed the Absolute path to the folder starts from your current working directory. So, if we were in /c/Users, then cd rhosek would be the same as cd /c/Users/rhosek since our working directory is /c/Users.

Remember ..? The shortcut for the Parent Directory? You can also use .. to start a relative path, which allows you to start your path at the Parent Directory. So, if I was in /c/Users/rhosek and wanted to get to /c/Users/Public, instead of typing the entire absolute path (cd /c/Users/Public), I could use the relative path from the Parent Directory by typing cd ../Pubic.


You can see how that can save you some typing.

You can even chain ../ together, and move up more than one level. cd ../../../c/Users/Public would also work in this example. The chain of ../../../ takes us up three levels back to the Root Directory, then we follow that back to the Public folder. You may not specifically need to do this, but it comes in handy when specifying Relative Paths where you don't know what the Absolute Path is.

So, that's how you move around the folders on your hard drive, but how do you know where you're going?

ls: list files -- Lists information about files (and folders) in a directory

In Part Two we'll look at how to create, delete and move files around the folder structure.


Comments

Popular posts from this blog

Curiosity Digest #11

Curiosity Digest #12