Command Line Basics: How to find your way around your computer
Tags: bash, beginners
Published: 2020-08-01 19:54:19

*This post was originally published [on my blog](https://blog.annamcdougall.com/command-line-basics-how-to-find-your-way-around-your-computer-ckd972hb700az38s11lg0fztx). You can also [follow me on Twitter @AnnaJMcDougall](http://www.twitter.com/AnnaJMcDougall).*

There is no debate: using a command-line interface (CLI) is more efficient than the graphic user interface (GUI) with which we’re all brought up. The problem is, it’s not as intuitive and it’s not what we all grew up with. Here I’ll cover the absolute basics for finding your way around your computer using the command line using Bash, which is the standard shell used by Linux. If you don’t understand what all these terms mean, don’t sweat it. The important thing is that you’ve booted up Linux, opened the Terminal (shortcut Ctrl-Alt-T) and are ready to find and make files.

However, before we begin it is crucial that you understand that you see a `$` in the code examples below, that means that you enter it into the command line.

By the end of this post, you’ll not only be able to use the following commands, but you’ll know what they mean (*gasp!*) and the variations you can use to help you save some time:

```
$pwd
$cd
$ls
```

## Where am I? Locating your current folder using the command line using `$pwd`

When you first open the Terminal, by default you will see some sort of identifying information, followed by a tilde (~) and then a dollar sign ($).

The dollar sign is an indicator that you are in the command line, and is commonly used in code snippets (like on this post) to show you that the code is to be entered into the Terminal.

**The tilde is also important, as it indicates you are in your Home Directory**. A quick editor’s note here: I’m purposefully going to use capital letters when referring to the Home Directory because of a fun quirk: the Home Directory is not the same as the folder/directory named ‘home’. You will see why this becomes important later, so for now please always make this association:

### Home Directory = `~$`
### `~$` = Home Directory

So where is your home directory? Try typing this:

```
$pwd
```

Remember, you don’t need to type the dollar sign ($) because it is only ever used as an indicator that you are in the Bash terminal. So just type ‘pwd’ and enter.

…Congratulations! You just executed your first Terminal command, and you can now see exactly where in your computer you are working from. Most likely, you see something like one of these:

- `/Users/yourUserName`
- `/home/yourUserName` 
- `/user/yourUserName`

The second example in the results above shows you why I’ll always use capitals to describe the Home Directory: because ‘home’ is not always Home: in this case, your Home Directory is actually a folder within the ‘home’ directory! Got a headache yet? Great! You’re on your way to becoming a developer!

The command `$pwd` stands for ‘present working directory’, and it tells you where you are within your folders at any given time. If you’re used to using the Windows GUI, you’re actually already used to seeing something like this in the Explorer bar:


![A screenshot of a Windows folder navigation bar](https://cdn.hashnode.com/res/hashnode/image/upload/v1596136431470/e5TqW8Jjt.png)

## Root Directory vs Home Directory

You may have noticed that when you typed `pwd` above, the location of your Home Directory doesn’t begin with a word but with a slash. That slash represents your Root Directory: if you picture your entire computer and all its files as one big tree, the root is the singular location from which it all stems. There is only one root location, and it is always denoted with a `/`.

### Just as `~$` always indicates your Home Directory, `/$` always indicates your Root Directory.

Now it’s time to learn another command, `$cd`, which allows us to move from one folder/directory into another. Try this…

```
$ cd /
```

Don’t forget the space, and remember not to type the $. Now, try going back to our trusty ol’ pal, `$pwd`, so we can find out where we are!

Hang on, it’s only showing a `/`? How can that be? Well, as I clarified above, the `/` indicates that we are in our root directory, and our root directory is as far back/down the tree as we can go. When we typed `$cd /` we were combining two ideas: `$cd` is the command, essentially saying ‘Go to…’ and then the `/` is our Root Directory. So what we have told the computer is “Go to the Root Directory, please” (side note: as far as I’m aware, there is no analogous term to ‘please’ in the Bash Terminal, but I’m doing a polite translation).

So now we’re in our Root Directory, and there’s nowhere deeper for us to explore in our computer. That’s good to know, but really we want to be doing the majority of our work in our Home Directory. So let’s get back there! Here you have two options:

```
$cd ~
```

This is probably what you’d expect, right? The `~` represents your Home Directory, so if we use it with `cd` then we can navigate home. AND IT WORKS! Hoorah!

Now, not to burst your bubble, but since the Home Directory is kinda a big deal, the developers of Bash have made it even easier to get back there. All you need to type is:

```
$cd
```

Yep, that’s it. Just the command, running without an argument. `$cd` will always take you home, no matter where you are or how far you’ve roamed. If you need a way to remember it, just remember that if you’re at a party and someone tells you to “Leave!”, you will probably just go home. If someone says “Leave with Andrew and his friends so you can make it to the next party!” then you’ll go there instead. So the `$cd` command with arguments takes you somewhere in your files and folders, and the `$cd` command without arguments just takes you to your Home Directory.

## Does that mean `$cd` can take me to any of my folders, anywhere on my computer?

Why yes, dear reader, yes it does! There are two ways of navigating to another folder: an ‘absolute path’ or a ‘relative path’. Let’s look at both using the following folder structure as an example:

```
Root Directory
|__user
    |__Anna (~)
       |__Music
       |__Downloads
       |__Documents
          |__CameraUploads
          |__ImportantForWork
          |__InternetFiles
```

As you can see, my Root Directory is the beginning of all my folders, and from there branches off the user directory, then the folder with my name (which is my Home Directory), then all the files I use day-to-day. Now let’s say I really want to find an HTML file which I know is important for work. As you may have guessed, it’s in my ‘ImportantForWork’ folder.

## Absolute Paths: navigating to a folder based on its relationship to the Root Directory
An ‘Absolute Path’ describes where a folder or file is, relative to the Root directory.

We want to get to ‘ImportantForWork’ which is in Documents, which is in Anna (my Home Directory), which is in user, which is in my Root Directory. In Bash, this looks like the following:

```
/user/Anna/Documents/ImportantForWork
```

Please note that these commands are all case-sensitive.

## Relative Paths: navigating to a folder based on where you are now

The fun thing about developers is that they’ve learned by now that laziness can often mean efficiency: similarly, although the above will always work (assuming the folder exists and you have the location correct), it is a bit of a bother to type that every time you want to move from one place to another, especially when the folders are quite close by. For example, if you’ve just opened the Terminal, you’re already half way to the ‘ImportantForWork’ folder, because you’re in Anna folder (which is our Home Directory or `~`). So why would you bother instructing the computer to go back to where you already are? Ain’t nobody got time for that!

Instead, we can tell the computer to take us to a location nestled within our current one.

```
~$cd Documents
```

As you can see, the tilde tells us we’re in our Home Directory (Anna), and from there we can simply jump into the Documents folder. Now we just need to go one step further, into our final destination folder, and then we’ll use `$pwd` to confirm that we made it.

```
$ cd ImportantForWork
$ pwd
```

The Terminal should now show us `/user/Anna/Documents/ImportantForWork`. Huzzah! We made it!

“Hold on one second, Anna,” I hear you say indignantly. “That wasn’t particularly efficient!”

Why shucks, dear reader, no it wasn’t. Let’s make it better. First we’ll use `$cd` alone, to bring us back to our Home Directory (Anna).

```
$ cd Documents/ImportantForWork
```

BOOM! Easy. Now we’re where we needed to be. 

## Want to make it even easier? Use your ‘tab’ button to act as an auto-complete tool.

As soon as you have enough characters typed to indicate a unique directory name, hit `<tab>` and you’ll see it pop up. In this case, something like 
```
$cd Doc <tab> / Im <tab>
```

Navigating this way, we’ve managed to get to our folder using just 11 keystrokes rather than the 40(!) we used when navigating using the absolute path.

Simply put: if you know where you’re going relative to your current location, you’re better off using the relative path in combination with the tab key.

## Show me more ways to get around!

OK since you insist. Let’s have a look at our file structure again:

```
Root Directory
|__user
    |__Anna (~)
       |__Music
       |__Downloads
       |__Documents
          |__CameraUploads
          |__ImportantForWork
          |__InternetFiles
```

As you may have noticed, you can use `$cd` with a folder name to access what we call a ‘child directory’, that is, a directory which stems directly off the one you’re in now. In the above example, CameraUploads, ImportantForWork, and InternetFiles are all child directories of Documents. Logically it therefore follows that we can refer to Documents as their ‘parent directory’.

## Moving to a Parent Directory

Any time you’re in a folder, you can move to the parent directory using:

```
$cd ..
```

The .. tells the computer to move up a step. These can also be stacked to access a grandparent directory:

```
$cd ../..
```

## Moving to a directory within the parent directory

Let’s say we are already in the ImportantForWork folder, and we now want to check out a photo of our nephew in the CameraUploads folder. How do we get there? If you logic it out, it’s quite simple: first we move to the parent directory, then we `$cd` into the CameraUploads folder.

```
$cd ..
$cd CameraUploads
```

This will work, but as usual we can save time by stacking the two commands:

```
$cd ../CameraUploads
```

Tada! You’ve navigated up a level and down a level in one command.

## Last but not least… Seeing what’s inside using `$ls`!

The last command is pretty straightforward, but next to `$cd` it is one of the commands you will type the most. Navigate to your Home Directory using` $cd` or `$cd ~` and then type the following:

```
$ ls
```

The command stands for ‘list’ and when used without an argument (as above) it shows you what files and folder are within the present working directory (i.e. where you are).

If your system is relatively new, you might not have any hidden files, but later on – especially if you plan on using Git – you will definitely have some! `$ls  won’t show those to you automatically, so to see them, type:

```
$ls -a
```

This `-a` is called a ‘flag’ and it tells the computer to something special with the command it’s given. In this case, it stands for ‘all’, so we’re telling the computer “List the files… yes, all of them please, even the hidden ones.”

As you may have guessed from my wording above, you can also use `$ls` *with an argument*, meaning you tell it which folder to list without actually having to go into it. 

For example, if we want to see the files in the Documents folder, we can either do this:

```
~$cd Documents
$ls
```

or we can simply type:

```
~$ls Documents
```

This will show us everything in the Documents folder, without actually taking us outside of our Home Directory.

## That’s it! You’re now a genius!

OK maybe not, but at least you can now move between folders using `$cd`, find out where you are using `$pwd`, and see what’s inside a folder using `$ls`, plus you know how to cut some corners by using relative paths, parent directories, and the `<tab>` button, saving you some time in an industry where every keystroke can be valuable.