Register to get access to free programming courses with interactive exercises

Navigation CLI fundamentals

After we start an operating system with a graphical interface, we reach a special workspace, often called the "desktop". This is the central entry point, from here you can open menu items and start different tasks. For example, to navigate the file system, you will need the Explorer.

The command line is different: after booting the system (and logging in, but we will discuss this point later), we'll see that the terminal is waiting for us to enter a command. This mode is tied to the file system. One can say that we're always inside a directory called the working directory. The current directory is checked using the pwd command:

pwd

/Users/guest

By the way, the name pwd is an abbreviation that stands for "print working directory". This is how the names of many commands are organized, making them easier and faster to remember.

By default, a new shell session opens inside the user's home directory, which is different for each user, so in your case the path will be different. This example has two surprises for those who are used to using Windows:

  1. At the beginning there is no "local drive", but there is a single root directory /. This is the top of the file system, all other files and directories lie inside this root.
  2. Instead of backslashes \, we use straight /.

We'll learn more about the differences and the file structure in a future lesson, for now, let's focus on navigation.

ls (list) is a program that outputs a list of files and directories in the working (current) directory.

ls

Desktop  Documents Downloads Library  Movies  Music  Pictures Public

Another useful command is cd (change directory), which is used to move around the file structure. To do this, you need to pass it an argument - the directory to which you want to move:

# Entering the directory
cd Music
# Looking at its contents
ls

iTunes
# Viewing the current working directory
pwd

/Users/guest/Music
# If the directory name contains a space, it must be escaped with `\`
cd Best\ music

Let's look at this point in more detail. You probably know that the path to the file can be either absolute or relative. The absolute path is the full path starting from the root, in our case, /, and the relative path is the path starting from our current working directory. Earlier we gave a relative path. It is very easy to distinguish them from each other: if the first character in the path is /, then it's an absolute path, otherwise it's a relative path. When a relative path is used, the cd command internally tries to calculate an absolute path. It takes the current working directory /Users/guest/ and attaches Music to it. The result is /Users/guest/Music. The cd command understands both absolute and relative paths, so you can pass anything to it:

# It doesn't matter where
cd /Users/guest/Music # Absolute path

An absolute path unambiguously determines the address of a file or directory in the file system, while a relative path does not. A relative path only makes sense if it is considered alongside the current working directory. When passing the absolute path, the working directory you're in now doesn't matter. If the path exists, you'll move to that directory. Now consider another problem. Suppose we are in the /Users/guest/Music directory. How do I exit it and get back to /Users/guest? At least we already know one way: specify an absolute path using cd:

cd /Users/guest

But there is a simpler way, just enter a special value, .., and it will go one directory higher.

# In the /Users/guest/Music directory
cd ..
pwd

/Users/guest

Moreover, you can use this placeholder to exit to any number of levels by specifying .. using a backslash:

# In the /Users/guest/Music directory
# go two levels up
cd ../..
pwd

/Users

In addition to the two dots, a single dot is sometimes used to signify the current directory. For example, instead of cd Music, you could write cd ./Music. Both of these expressions are equivalent. We'll look at why we need a dot later.

There is also a third option to return to /Users/guest from /Users/guest/Music:

# From anywhere
cd
pwd

/Users/guest

The point is that the cd command, executed without arguments, sends you to the current user's home directory.

And finally, the fourth option. The user's home directory has a special designation — ~ (tilde), which is replaced by an absolute path at the moment the command is executed. Therefore, you can jump directly to any subdirectory of the home directory from any location.

# From anywhere
cd ~/Music
pwd

/Users/guest/Music

Suppose you're in the Home directory and decide to look at files in the Music subdirectory. One way you already know is to go to the Music directory and run ls. As usual, there is another way. ls also takes an argument, which is the directory you want to analyze.

ls Music

iTunes

As with cd, the concepts of absolute and relative paths apply to the ls argument. However, this rule applies in general when we are using paths unless otherwise specified (sometimes it is necessary to pass only the absolute path).

The commands cd, ls, and pwd together form the basis of navigating the file structure. If you know them, you'll never get lost or confused.

As time goes on, it you might get lazier and lazier and not want to enter the paths. Shell makes life easier and offers name autocomplete. Autocomplete is performed by double-clicking the Tab key.


Do it yourself

Examine the contents of your file system directories. When moving between partitions with the cd command, use the Tab key to autocomplete.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY