Command me, baby: 1. Exploring an Unfamiliar Land.
This post contains language intended for an audience over the age of 13.
Welcome back. Last week we discovered how to open your shell and find out where you are with Print Working Directory, or pwd. If you missed the first week you should read that first, here: Command me, baby: Learn to love your shell. This week we will enhance your capabilities and let you move around. First open your shell — you MUST follow along on your own or you’re wasting your time by reading this. Got it open? Cool.
Now, you most likely need to fundamentally re-assess your approach to this endeavor. To effectively use your shell, you must shed your consumer mentality. Whether you know it or not, you’ve been subconsciously trained to be a consumer from birth. The explosion of prevalence and targeted effectiveness in marketing and advertising over the last half century has assured you of that.
Your capacity for exploration is limited. Your curious impulses are stunted. Your decision making process, while well established and fine-tuned by the sheer number of products pushed in your face on a daily basis, is nonetheless contingent on having the options clearly and plainly presented to you.
You will have no such luxuries in the shell. Nobody is watching what you do, nobody is there to help you along or to hold you back. All the information you could ever want is available to you, but no-one will help you find it.
The first hallmark of a good programmer is comfort with the unknown. You must build up enough faith in your problem-solving ability that you would not blink at any unfamiliar problem, no matter how esoteric or alien. When I’m done with you your first instinct when you see an apparently unattainable goal will be ‘ok, what might the first step be’, instead of your current modus operandi: ‘shit! how the hell would I get all the way up there??’
When faced with any programming task in the real world for the first year or two, 90% of the time you’ll be tasked with doing something you’ve never done before, and 100% of the time there are at least some unknown factors. Routinely, you’ll be unwillingly dropped into a completely alien terrain and forced to find your way out…equipped only with your intuition, deductive reasoning, and sweet, sweet google searches.
When you opened up your shell at the start of this, you probably looked at the unblinking (aside from the cursor) prompt and were completely paralyzed. “Where are all my options?” you think. “I don’t know what I’m supposed to write in here”, you think.
It’s not that there are no options available to you — quite the opposite. There are far, far too many to list, and that’s why they’re not listed. Every executable program, every configuration file and startup script on your computer is available to you, not to mention hundreds of utilities specifically for command-line use that you’ve never even heard of but will soon find indispensable.
Think of it as an adventure…you’re going to be plumbing the unfamiliar, submerged depths of the iceberg of 1s and 0s that you take for granted every day. You’re in an unfamiliar land, on a heroes journey to comprehend this alien environment and one day master it. If you take the right approach to learning your way around the shell, you’ll be able to rapidly assimilate the process of getting a web server restarted without errors or installing memcached to reduce load on a highly trafficked website or using git to update your client’s website. It’s all a logical progression.
I’m not going to lie: when you start, it’s all going to seem much more difficult than it should be. You’re going to get frustrated. Especially if you work with a client and they have no idea of the levels of complexity associated with this subterraneous realm they don’t know or care about. The onus of caring and understanding is on you. So let’s get you started on the knowledge train.
OK, so you’ve been dropped into this alien land like Alice in Wonderland or Link from the Original Game Boy classic Legend of Zelda: Links Awakening. From last week, you know where you are (pwd). This week we’re going to learn how to pull out a map to find out where you can go and how to teleport yourself anywhere on said map.
Surely you are comfortable with navigating files and directories within your standard Graphical User Interface (GUI for short). Clicking through folders to find files should be second nature to you. The same capacity exists within the shell, there is just a different interface for it because you don’t have a mouse and your terminal can’t draw you pretty pictures.
Firstly, you need to recreate the ability to see the contents of the folder you’re currently in. In the GUI, this is easy: just look at the screen. In the shell, you need to explicitly tell your robot pal to list the files and directories for you. The command for that is ls -lh. ls works too but prints in a less friendly way with less details…you pretty much always use ls -lh and since I care only about teaching you effectiveness, not completeness, we’re just going to start by using the command I actually use, not all the theoretical variations on it.
ls -lh and pwd are like your sanity checkers…whenever you get distracted and pulled out of the digital world, upon return to the shell you will instinctively type those two commands, one after the other, just to reload your mental map. Here’s the directories available to me:

Now that you know how to consult your map, you need to know how to move. The command to change to another directory is Change Directory or, in code aka efficient-speak, cd. You can change directories by giving this command along with an absolute path or a relative path.
An absolute path starts with /, the root directory. See each and every file and directory on your computer can be expressed in an absolute path that begins with slash. For example, when I used pwd last week, my robot buddy told me I was in /Users/neilsarkar. Note the preceding slash, it’s very important, it indicates your root directory. Your entire filesystem exists as subdirectories of the root directory, /
Remember what I said about no-one helping you know what to do or holding you back? Let’s go to the root directory now

Then do your instinctive mental map load, pwd [enter] ls -lh

This is probably the most important directory listing you can see. Every single file on your computer exists as a descendant of one of the directories you see in front of you. I remember the first time I went to a root directory, I felt like Simba standing on a ledge atop a mountain, surveying my kingdom proudly. Not getting that yet? You will if you stick with it.
The root directory is all-encompassing but powerless on its own. It’s easily the most useful point of reference you have here under the ocean, it’s your anchor and you must know it and love it. You can’t find that definitive a starting point using Finder, and that’s indicative of the increased level of power available to you in the shell. In the shell, there is no man behind the curtain. And I think that rocks.
As a side note, I’m totally cool with only knowing as little as I need to know to make something work. If you’re one of those people who needs to know or have the illusion of knowing the full story, that’s cool too, I wish I was that way sometimes it would save me a lot of embarassment. If you want to know what all of these directories mean, the cryptic ones that aren’t self explanatory (sbin, usr, var, opt, etc) are all explained here: http://www.tuxfiles.org/linuxhelp/linuxdir.html
Anyway, let’s get you back to your home directory. We’ll use our new friends ls -lh and cd to get back to your home directory:

Nice. You just navigated directories using relative paths. There is one special feature of relative path navigation: you may notice that each directory contains two special elements (. and ..). These refer to the current directory and the parent directory.
You don’t use the current directory reference often, but you use the parent directory reference a *lot*.
As an example, let’s get back to the root directory using only the parent directory reference

To tie this all together, I’m going to show you how to navigate using absolute paths, and how to utilize the single most useful feature of your shell: tab completion. I use tab completion twenty to hundreds of times a day, it’s kind of my jam. OK so we are going to re-trace our steps using absolute paths this time. cd into a random directory (I’m going to choose sbin). Now type cd / without hitting enter and press tab twice.

You indicated through your frantic tabbing that you’re looking for something. Your robot pal has helpfully supplied you with a list of files and directories available from that point (the root directory) so you can refine your guess. You see the directory you want to go to (for me, it’s “Users”) so type in Users and hit tab again a couple more times, then type in the first couple letters of your username:

Hit tab again and it will autofill. Hit enter and you’re back in your home directory. Sweet, right? Can you see how uniform and extensible this method of travel is? You can use this strategy to efficiently get *anywhere* on your filesystem. You’re welcome.
Next week we’re going to start going faster and doing shit that actually changes your system. Next time you learn how to download and compile MySQL from inside the shell. That shouldn’t frighten you — the unassailable unknown is just the temporarily unconquered, remember? Thanks for paying attention. Til next week…











