Course outline · 0% complete

0/29 lessons0%

Course overview →

Where am I? pwd and ls

lesson 1-2 · ~8 min · 2/29

The shell always stands somewhere

Your files live in directories (the command-line word for folders), and directories live inside other directories, forming a tree. At any moment, your shell is standing inside exactly one directory, called the working directory. Every command you run happens from there.

This matters immediately: the same command does different things in different places. ls lists somewhere, and the delete command you'll meet in unit 3 deletes somewhere. Knowing exactly where you stand is the difference between cleaning a scratch directory and destroying real work, so "where am I?" is the first reflex to build.

Two commands tell you where you are and what is around you:

  • pwd: print working directory. Shows the full address of where you are standing.
  • ls: list. Shows what is inside the current directory.

The very top of the tree is a single directory written as /, called the root. Every address starts from it, like /home/sam/notes.txt.

Code exercise · bash

Run it. `cd` (change directory, covered fully in unit 2) moves the shell, and `pwd` proves where it landed. First we stand at the root `/`, then at `/tmp`, a scratch directory every Linux system has.

Listing what's here

ls with no arguments lists the working directory's contents, sorted alphabetically. In the example below we first build a small directory with three files so there is something to list. mkdir makes a directory and touch makes empty files (both get their own lesson in unit 2).

Code exercise · bash

Run it and read the ls output at the end: three files, listed alphabetically.

Quiz

You're lost in the filesystem. Which command shows the full address of the directory you're standing in?

Code exercise · bash

Your turn. Make a directory called `fruits`, move into it, create two files `kiwi.txt` and `mango.txt`, then list them. Expected output: ``` kiwi.txt mango.txt ```