Intro to Programming: How to Use the Command Line (Part 2)

Let's continue learning about the command line with some basic commands.
By Ciprian Stratulat • Updated on May 2, 2023
blog image

Welcome to another article in our Intro to Programming series. I will pick up where I left off in part one of my article on how to use the command line, and cover some basic commands in part two.

 

 

How to Start With Basic Command Prompts

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 you saw in part one, the command line prompt can be customized and, in my case, I set it to show me the current directory, which is the tilda (~) sign - I'll come back to it in a moment.

The command line prompt 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.

Image Source: Edlitera

 

Why it's Important to Know Which Directory You're In

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 a directory (also called a folder). 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 is 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!

 

How to Determine the Current Path of a Directory Tree

As mentioned above, 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:

Image Source: Edlitera

The diagram above 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 is called 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 you create new sub-directories, just like on a living tree new branches can grow on existing branches.

In this directory tree, I care about the gray boxes in the diagram above. The gray boxes 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.

From the diagram above, you can see that to get to my home folder I start 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

 

Article continues below

How to Find Your Home Directory (~)

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. A home directory is where all the stuff that belongs to that particular user gets stored. Home directories allow computers to avoid mixing up different users' files. By default, the command line will simply drop you in your own home directory.

 

Where is Your Home Directory?

Ok, so now looking at my command line prompt, you might be wondering where can you see 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 ~.

Image Source: Edlitera

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, I came up with this ~ which is an alias, or a shortcut that refers to the home directory. This is a bit of a simplification, but it's a good mental model to use.

 

What's the Current Directory?

Now that you 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:

Image Source: Edlitera

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.

 

How to List All the Files in the Current 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).

Image Source: Edlitera

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, you 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.

Image Source: Edlitera

This may look overwhelming at first, but don't worry, the following diagram will help you keep track of what's shown here:

Image Source: Edlitera

From the diagram above, you can see that running ls -l provides a lot more information about the files in the current directory, such as 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:

Image Source: Edlitera

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.

 

How to Change the Current Directory

Next, you'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 commandcd,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.

Image Source: Edlitera

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 may 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.

Image Source: Edlitera

 

What is the Difference Between Absolute Vs. Relative Paths of a Directory Tree?

Consider the following diagram, representing an expanded directory tree:

Image Source: Edlitera

What's new in this image above is that now I have a directory called images inside the i2p directory and inside the Applications directory, it now shows the Spotify.app directory, which itself has a subdirectory called Contents.

Now let's assume you start again in our home directory, which is /Users/ciprian for me. You'll ask two questions.

 

1) How Do You Navigate to the Images Directory From the Home Directory?

To navigate to the images directory starting from the home directory, you have two options.

The first one is to simply move to the i2p directory first. Second, you then move to the images directory by running the cd i2p command, followed by cd images command:

Image Source: Edlitera

As you can see, this works.

After you execute the two commands, our command prompt updates to reflect the fact that you are now in the directory with the path /Users/ciprian/i2p/images directory. You 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.

Image Source: Edlitera

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, you can type just the portion of the path relative to the directory you're starting in.

Image Source: Edlitera

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.

 

2) How Do You Navigate to the Contents Directory From the Home Directory?

One quick look at our directory tree shows you that the Contents directory is not on the home directory branch. So how can you change to that directory?

Image Source: Edlitera

Using just what you know so far, the answer is to use the absolute path of the Contents directory. In other words, if you're in the home directory and we want to move to the Contents directory shown in the diagram above, you can accomplish this by running the cd /Users/Applications/Spotify.app/Contents command:

Image Source: Edlitera

In this section you learned that using relative paths is faster when possible (less typing) and when you can't use relative paths, you 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!

Image Source: Edlitera

 

How Do You Move a Directory Tree? 

Before I 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:

Image Source: Edlitera

Let's assume you're in the /Users/ciprian/i2p/images and you want to move "up" to the /Users/ciprian/i2p directory. You know you can already do this by typing cd /Users/ciprian/i2p:

Image Source: Edlitera

That's fine, but if you'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 ..

Image Source: Edlitera

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 is called the parent directory).

In fact, you can actually see the .. listed when you run ls -al to show all hidden directories and files:

Image Source: Edlitera

As you can see in the screenshot above, all directories have the .. shortcut. You see it inside /Users/ciprian/i2p/images and you also see it in /Users/ciprian/i2p. For this reason, you can use .. to move up any number of levels.

For example, to move up two levels, you can run cd ../..

Image Source: Edlitera

In the screenshot above, you can see you 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, using the command line is 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.

 

Ciprian Stratulat

CTO | Software Engineer

Ciprian Stratulat

Ciprian is a software engineer and the CTO of Edlitera. As an instructor, Ciprian is a big believer in first building an intuition about a new topic, and then mastering it through guided deliberate practice.

Before Edlitera, Ciprian worked as a Software Engineer in finance, biotech, genomics and e-book publishing. Ciprian holds a degree in Computer Science from Harvard University.