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, because the same command does different things in different places. ls lists somewhere, and the delete command coming in unit 3 deletes somewhere. Knowing exactly where you stand is the difference between cleaning out a scratch directory and destroying real work, which is why locating yourself is the first reflex to build.

Two commands cover it:

CommandStands forWhat it tells you
pwdprint working directoryThe full address of the directory you are standing in
lslistThe names of the things inside that directory

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

/ the root home/ tmp/ sam/ you are here notes.txt projects/ pwd prints /home/sam ls prints what is inside sam/ only: notes.txt projects
A filesystem tree rooted at slash, containing home and tmp. Inside home sits the sam directory, highlighted as the current working directory, holding notes.txt and a projects directory. Alongside it, pwd reports the full address /home/sam while ls reports only the two names inside that directory.

Moving the shell and confirming where it landed

cd (change directory, covered fully in unit 2) moves the shell, and pwd reports where it ended up. The example stands first at the root /, then at /tmp, a scratch directory every Linux system provides.

cd /
pwd
cd /tmp
pwd

Output

/
/tmp

Each pwd prints the location as of that moment, which is why the two lines differ. pwd takes no arguments at all: it has nothing to configure, it simply reports the current location.

Listing what is here

ls with no arguments lists the working directory's contents, sorted alphabetically. To have something worth listing, the example below first builds a small directory containing three files. mkdir makes a directory and touch makes empty files, and both get a full lesson of their own in unit 2.

Building a directory, then listing it

The first three commands create a directory, step into it, and put three empty files inside. Only the final ls prints anything, and it reports those three names in alphabetical order.

mkdir demo
cd demo
touch apple.txt banana.txt cherry.txt
ls

Output

apple.txt
banana.txt
cherry.txt

mkdir, cd, and touch all succeed silently, which is normal on the command line: a command that has nothing to report says nothing. Silence means it worked.

When you have lost track of your position in the filesystem, pwd is the command that shows the full address of the directory you are standing in. The name is a mnemonic for print working directory, and it prints an absolute address such as /home/sam/projects.

ls answers a different question. It shows what is inside the current directory but never says where that directory sits in the tree, so a bare ls listing looks identical whether you are in a scratch folder or in something important.

Here is the same build-then-list pattern with a second directory. mkdir fruits creates it, cd fruits steps inside so the new files land in the right place, touch creates both files in a single command because touch accepts as many names as you give it, and ls confirms the result.

mkdir fruits
cd fruits
touch kiwi.txt mango.txt
ls

Output

kiwi.txt
mango.txt

The order of those four commands is what makes it work. cd fruits has to come before the touch, because touch creates files in the working directory, so running it too early would scatter kiwi.txt and mango.txt into the parent directory instead.