Let's go ahead and open the command line. I'm on a Mac, so I'll be using the Terminal app, but if you're on Windows, you should launch the Command Prompt app. Or, even better, install the Anaconda Distribution and use the Anaconda Prompt, which is very similar to a Linux or Mac terminal.
Start by hitting enter a few times. You will see that a new line is printed on the screen and it always starts with the same words (these words are the command line prompt). As we saw in the previous article, this particular part can be customized and, in my case, I set it to show me the current directory, which is the tilda (~) sign - we'll come back to it in a moment. It also shows the current logged in user, which is ciprian. The dollar sign at the end simply marks the part where the command prompt ends and where I can begin to type my commands.
In a command line environment, you execute commands by writing them and then hitting Enter. The commands are always run in the context of the current directory, unless otherwise specified. This means that at any given time when you're using the command line to run a command, you are in some directory (or folder, if you prefer that name) on your computer. You can navigate to a different directory, if you so choose, but you're always in some directory. This is important to remember because the commands you run may have some effect on the directory or the files inside it.
Think of it this way: imagine you're in a house that has no exit doors (creepy, I know). At any given time, you're in some room in the house. If you perform some action, you will interact with the current environment around you. You could perform the same action, but in different rooms, and the result would be different. If say, you're in the kitchen and you decide to paint the current room purple, the end result will be that the kitchen will be purple. If you then go to the bedroom and decide to perform the same action again (paint the current room purple), the result will be that the bedroom will now also be purple.
Directories (or folders) are similar to rooms in our house example above. They actually represent specific locations on your computer: My Documents, My Pictures, my home folder, etc. The reason it is important to remember this is because some commands that you will run will make changes to the current directory or to the files in the current directory. If you are in the wrong directory and, for example, you run a command that deletes all the files in the current directory, you will end up deleting the wrong files!
So it's very important to be aware which directory you are in at any given time. This is why a very important piece of information in the command line prompt is the path of the current directory.
If you've never had to think about directories, consider the following diagram:
This represents a simplified version of the directory structure on my laptop. As you can see, directories on my laptop (and yours too) are organized in what we call a tree structure. It's actually an upside down tree, where the root sits at the top and branches grow below it. This tree can keep going as we create new subdirectories, just like on a living tree new branches can grow on existing branches.
In this directory tree, we care about the gray boxes: these represent the directories on our laptops. I only included a few here, for exemplifications, but you and I both have tons more directories than are shown here. To determine the path of a given directory, you start at the root / and you add the directories you encounter on your path to your desired directory. You separate the names of the directories you encounter with the forward slash character on a Mac or Linux and with the back slash on a Windows (in Windows, the root is a named drive, such as C, or D, but essentially it's the same concept).
So, it's pretty easy to see from the diagram above that to get to my home folder, starting at the root, I first need to visit the Users directory and then the ciprian directory. So the path of my home directory is /Users/ciprian. Hopefully this all makes sense.
By default, when you open the command line, it will set the current directory to be the home directory of the currently logged in user (you). Every user on a computer has a home directory - this is where all the stuff that belongs to that particular user gets stored. This allows computers to avoid mixing up different users' files. By default, the command line will simply drop you in your own home directory.
Ok, so now looking at my command line prompt, you might be wondering where can you see the my home directory? After all, there's no /Users/ciprian to be seen anywhere, yet I told you that by default the command line takes me to my home directory.
The answer is, it's that weird looking ~.
We all have a home directory somewhere, yet we don't share the same name for that home directory. Mine is /Users/ciprian on my laptop, as I mentioned. Yours very likely is very different. But the concept of a home directory is general, so to simplify things a bit, we came up with this ~ which is an alias, a shortcut that refers to the home directory. This is a bit of a simplification, but it's a good mental model to use.
Now that we have a good mental model for directories, let's begin running some commands. One of the simplest task you can do on the command line is see what is the current directory. I can do this using the pwd (print working directory) command:
So you can see above that my current directory is ~ (my home directory) and the computer prints /Users/ciprian, which is indeed my home directory.
Knowing which directory you're in is useful, but knowing what files exist in the current directory is probably even more useful. You can get that information by running the ls command (on Mac and Linux systems) or the dir command (on Windows only).
So you can see that I have quite a few files and folders. If you type ls (or dir) in your current directory, you will see different files and folders - go ahead and try it. The ls command is also useful for finding information about the files and folders listed. To do so, we can run ls -l (on Mac and Linux). Unfortunately, dir does not have an equivalent, so this section is only relevant for Mac and Linux users.
This may look overwhelming at first, but don't worry, the following diagram will help you keep track of what's shown here:
From the diagram above, you can see that running ls -l provides a lot more information about the files in the current directory: file permissions (who can do what to the file), file owner, creation date, file size and file name.
The -l is an example of how you specify an option to a command. These options influence how commands are run and what gets printed as the result. Most commands have a number of options that you can specify, and generally these options are specified using single letters. For example, the ls command also has an a option which instructs it to print all files, including hidden files. Hidden files on Mac and Linux computers have a dot in the beginning of their names and are not generally visible when browsing your file system using a user interface (such as Finder on Mac).
Below is a sample of what I see when I run ls -al on my computer. As you can see, when you specify single letter options to a command, you don't have to use spaces between the options, you can just write the options one after the other and add a dash in front. You could, of course, also use the dash in front of each option (ls -a -l), but you'd have to type more.
Next, we'll learn how to move between directories. This is important because, as I said, the command line will generally take you to your home directory when it opens, but not all your files will be in the home directory. By the way, the reason I say it will generally take you to your home directory and not always is that this default directory is actually configurable. This configuration, however, is beyond the scope of this brief introduction, but know that it exists and you can make your command line open inside any directory you want.
You can change the current directory by running the command cd, which stands for change directory, followed by the directory you want to change to.
Let's go over an example. It so happens that in my home directory I have a directory called i2p. It's a place where I keep some materials for this introduction to python article series. To move inside that folder, I have to change to that directory by typing cd i2p
You can see that after the command finished executing, the command prompt updated to reflect that now I'm in the ~/i2p directory. Remember, you can think of ~ as a shortcut for your home directory, which in my case is /Users/ciprian, so ~/i2p is essentially same thing as /Users/ciprian/i2p. If that's the case, perhaps you wonder, can you also type cd /User/ciprian/i2p and achieve the same result? Yes, of course. In fact, in certain cases, as you will soon see, you actually have to specify the whole path.
Consider the following diagram, representing an expanded directory tree:
What's new in this image is that now we have a directory called images inside the i2p directory and inside the Applications directory, we now show the Spotify.app directory, which itself has a subdirectory called Contents.
Now let's assume we start again in our home directory, which is /Users/ciprian for me. We'll ask two questions.
To navigate to the images directory starting from the home directory, we have two options. The first one is to simply move to the i2p directory first, and then move to the images directory by running the cd i2p command, followed by cd images command:
As you can see, this works. After we execute the two commands, our command prompt updates to reflect the fact that we are now in the directory with the path /Users/ciprian/i2p/images directory. We can verify this by running the pwd command, which indeed prints /Users/ciprian/i2p/images.
This approach can be a bit tedious though. A shorter way to get the same result is to use the whole path, that is, to run the command cd /Users/ciprian/i2p/images.
As you can see, this has the exact same result. But there's an even shorter way to achieve this. Instead of typing the whole path of the images directory, we can type just the portion of the path relative to the directory we're starting in.
i2p/images is the path of the images directory relative to /Users/ciprian, which is my home directory. The relative path of a directory is the path of that directory, relative to some other directory. Here's another example, the path of the images directory relative to the /Users directory is ciprian/i2p/images.
The absolute path of the images directory is /Users/ciprian/i2p/images. The absolute path refers to the path of a directory, starting from the root.
One quick look at our directory tree shows you that the Contents directory is not on the home directory branch. So how can we change to that directory?
Using just what we know so far, the answer is to use the absolute path of the Contents directory. In other words, if we're in the home directory and we want to move to the Contents directory shown in the diagram above, we can accomplish this by running the cd /Users/Applications/Spotify.app/Contents command:
In this section we learned that using relative paths is faster when possible (less typing) and when we can't use relative paths, we can use absolute paths for navigating between directories.
And here's one more trick: you can always use cd ~ to go to your home directory, regardless of where you are!
Before we end this post, I want to show you one more useful trick: how to move up the directory tree. This is something that you'll have to do quite often, so it's good to know a shortcut for this. Let's again take a look at our directory tree structure:
Let's assume we're in the /Users/ciprian/i2p/images and we want to move "up" to the /Users/ciprian/i2p directory. We know we can already do this by typing cd /Users/ciprian/i2p:
That's fine, but if we're deep inside some directory with a very long path and you just want to move up one or two levels? It seems annoying to have to type the absolute path. Fortunately, there's a faster way. You can just run the command cd ..
Why does that work? Well, it's because .. like ~ is a sort of shortcut. .. refers to the parent directory of the current directory. So, wherever you are in the directory tree, .. will refer to the directory right "above" (what we call the parent directory). In fact, you can actually see the .. listed when you run ls -al to show all hidden directories and files:
As you can see in the screenshot above, all directories have the .. shortcut. We see it inside /Users/ciprian/i2p/images and we also see it in /Users/ciprian/i2p. For this reason, we can use .. to move up any number of levels. For example, to move up two levels, we can run cd ../..
As you can see in the screenshot above, we start in /Users/ciprian/i2p/images and use cd ../.. to move two levels above, to /Users/ciprian.
So that's it for a crash course on how to use the Terminal on Mac and Linux. While the command line can look intimidating at first, it is actually fairly simple to use once you get used to it - to some extent simpler than some of the user interfaces you are already used to. So with a bit of practice, it can become second nature and you might even come to prefer using it over certain user interfaces.
In this article, we begin to cover the basics of how to use the command line. While it can look scary at first (no colors, no buttons, and why does it look like it's from the 1980s?), the command line is a very important tool to be familiar with.
Building a compelling personal brand and story is a great asset for landing a job as a Data Scientist and for developing a strong professional network for your career. Here are six steps that you can take right now towards building your career in Data Science.
Learning to code? You're in good company! Here is why some Hollywood celebrities have been learning it too.