Skip to main content

Command Palette

Search for a command to run...

Working with Path and cd Command

Updated
โ€ข5 min readโ€ขView as Markdown
R
Safety professional by day, Linux explorer by night.

The very first basic Linux command to understand is the cd command. We use it to move to a directory. Think of a directory like a folder in Windows. However, before we go further into the cd command, we need to understand paths in Linux.

Path

When we open the terminal, we'll have a default directory called the Home directory. It will look like this:

/home/myusername/

๐Ÿ“ Info:

  • home is the home folder in Linux. Think of it like the C: drive in Windows.

  • myusername is the username. Think of it like an Admin or Guest account in Windows.

If your username is haha, then the home path would be:

/home/haha/

Linux has given users an easier way to use a tilde (~) as a replacement for the home path. So, instead of typing:

/home/myusername/

you can just type:

~/

which means the same thing. We will cover this later in the cd command section.

Path types

In Linux, there are two ways to define a path.

1. Absolute path

This type of path starts with a slash (/). You write the full path, including the home path. Imagine your home is your house where your family lives. You want to go to the city library. You will pass a playground on the way there (the library directory is inside the playground directory).

If you want to go to the playground:

/home/myusername/playground/

or

~/playground/

If you want to go to the library and you are currently still at the playground:

/home/myusername/playground/library/

or

~/playground/library/ 

2. Relative path

This type of path will be explained in the cd command section below. One thing to remember here is that a relative path does not begin with slash.

cd command

cd stands for "change directory". If you want to move from your home directory to the playground directory, you need to write cd at the very beginning of the command.

Basic syntax:

cd [directory]

If you want to go to the library and you are currently still at the playground using an absolute path:

cd /home/myusername/playground/library/

or

cd ~/playground/library/

If you want to go to the library and you are currently still at the playground using a relative path:

cd library

๐Ÿ“ Info: Keep in mind that the relative path does not use slash in the beginning of the path.

Go up one level

You can go up one level (the parent directory). The parent of the library directory is the playground directory. If you want to go to the parent directory, use two dots after cd like this:

cd ..

Go up two levels

You can double going up by doubling the double-dot command.

cd ../../

Move to sibling directory

Now, imagine there are two directories (library and school) inside the playground directory.

โ”œโ”€โ”€ home
โ”‚    โ””โ”€โ”€ myusername
โ”‚          โ””โ”€โ”€ playground
โ”‚                โ”œโ”€โ”€ library
โ”‚                โ””โ”€โ”€ school

If you are now in the library directory and want to go to the school directory, you can write a single command like this:

cd ../school

๐Ÿ“ Info:

  • The combination of cd .. and a relative path shortens your commands.

  • Without using cd .., you would need to write these commands:

cd /home/myusername/playground/
cd /home/myusername/playground/school 

Repeat the last directory

To go to the last directory you used, use this command:

cd -

For example, if you are now at:

cd /home/myusername/playground/library

then you need move to:

cd /home/myusername/playground/school

and you need to go back to the library directory, you can just use the cd - command.

Move quickly to the home directory

If you are now in a very deep directory path like this:

/home/myusername/playground/school/elementary school/grade4/task/taskweek1/maths/addition/fraction/submitted/final/veryfinal/nomistake/iswear/thishasnomistake/youcantrustme/seriousmode/nojoking/keepmyword/

and you want to jump to your home directory, just use this command:

cd

or

cd ~/

More than one-word directory names

You can't use space(s) directly in a directory name in commands because the terminal treats spaces as separators between arguments. To solve this, use quotes (either double or single quotes) when mentioning that directory name.

Imagine there is another directory named online game.


/
โ”œโ”€โ”€ home
โ”‚    โ””โ”€โ”€ myusername
โ”‚          โ””โ”€โ”€ playground
โ”‚                โ”œโ”€โ”€ library
โ”‚                โ”œโ”€โ”€ online game
โ”‚                โ””โ”€โ”€ school

To go to that directory from home, you can write commands like these:

cd /home/myusername/playground/"online game"

or

cd "/home/myusername/playground/online game"

Another way to write a space is to use a backslash before the space like this:

cd /home/myusername/playground/online\ game 

โš ๏ธ WARNING: Writing even one incorrect character in a terminal command can result in either:

  • the command not working, or

  • the command producing an unintended result.

The second possibility might become a nightmare for you! The good news is that most mistakes only cause errors without any serious impact.

Check the current directory

Sometimes, when you go too deep into directories, you get lost and don't remember where you are. To check your current position, use this command:

pwd

pwd stands for "print working directory", which shows where you are.