Working with pwd, ls, and cd
The commands pwd, ls, and cd form the core of moving around a file system from the terminal. pwd tells you where you are, ls shows what is there, and cd takes you somewhere else. This lesson explains how directories are organized, how each command works, and how to combine them to navigate confidently.
Understanding the Concept
Files on Linux and macOS are arranged in a tree of directories, which many graphical tools call folders. The top of the tree is the root directory, written as /. Every other directory sits somewhere below it.
/
├── home
│ └── student
│ ├── projects
│ │ ├── notes.txt
│ │ └── site
│ │ ├── index.html
│ │ ├── css
│ │ └── js
│ └── my docs
└── usr
Bash always has a current working directory, which is the place where commands that use relative file names look first. Your shell begins in your home directory, such as /home/student on Linux or /Users/student on macOS. Every command that takes a path is interpreted relative to the current directory unless the path starts with /.
The examples in this lesson use the tree above, with the home directory shown as ~.
Printing the Working Directory with pwd
pwd stands for "print working directory". It prints the full path of the directory the shell is in:
pwd
Output:
/home/student
The result is an absolute path, which means it starts from / and spells out every directory on the way down. Use pwd whenever you are unsure where you are, especially before running a command that changes or deletes files.
Logical and Physical Paths
A directory can be reached through a symbolic link, which is a shortcut that points to another directory. The pwd built-in has two options for this:
pwd -Lprints the logical path, including the names of links you followed. This is the default.pwd -Pprints the physical path, with every link resolved to the real directory.
Suppose ~/projects/link is a link to ~/projects/site:
cd ~/projects/link
pwd
pwd -P
pwd -L
Output (with the home directory shown as ~ for readability):
~/projects/link
~/projects/site
~/projects/link
The first and third commands report the path you typed. The second reports where the files really are.
Listing Directory Contents with ls
ls lists the names of the files and directories in a directory. Without arguments it uses the current directory:
cd ~/projects
ls
Output:
notes.txt site
You can also pass a path to list a different directory, without moving there:
ls site
Output:
css index.html js
Hidden Files
Names that begin with a dot are hidden from the normal listing. They are mostly configuration files. Use -a to include them:
cd ~
ls
ls -a
Output:
my docs projects
. .. .hidden my docs projects
The special names . and .. appear in every directory. A single dot means "this directory", and two dots mean "the parent directory". The option -A lists hidden files but leaves out . and ..:
ls -A
Output:
.hidden my docs projects
The Long Format
The -l option prints one line for each item, with details:
cd ~/projects
ls -l
Output:
total 4
-rw-r--r-- 1 student student 0 Nov 20 10:52 notes.txt
drwxr-xr-x 4 student student 4096 Nov 20 10:52 site
Reading a line from left to right:
| Field | Example | Meaning |
|---|---|---|
| Type and permissions | drwxr-xr-x | The first character is the type (- for a file, d for a directory), followed by nine permission characters |
| Link count | 4 | Number of hard links to the item |
| Owner | student | The user who owns it |
| Group | student | The group that owns it |
| Size | 4096 | Size in bytes |
| Date | Nov 20 10:52 | When it was last modified |
| Name | site | The file or directory name |
Permissions have their own lesson later. For now, notice that the leading d identifies site as a directory.
Add -h to print sizes in a human-readable form such as 4.0K or 1.2M:
ls -lh site
Output:
total 8.0K
drwxr-xr-x 2 student student 4.0K Nov 20 10:52 css
-rw-r--r-- 1 student student 0 Nov 20 10:52 index.html
drwxr-xr-x 2 student student 4.0K Nov 20 10:52 js
Other Useful Options
| Command | What it does |
|---|---|
ls -1 | One name per line |
ls -F | Adds a marker after each name: / for directories, * for executables |
ls -d */ | Lists only the directories in the current directory |
ls -ld site | Shows details of the directory itself instead of its contents |
ls -t | Sorts by modification time, newest first |
ls -R | Lists directories recursively, including everything below them |
Here are two of them in action:
ls -F
ls -R site
Output:
notes.txt site/
site:
css index.html js
site/css:
style.css
site/js:
app.js
In the first output, the trailing slash marks site as a directory. In the recursive listing, each directory is shown as a heading followed by its contents.
Changing Directories with cd
cd means "change directory". It moves the shell to another directory, and the prompt and pwd update to match. It is a built-in, as explained in the previous lesson, because it must change the directory of the shell itself.
Absolute and Relative Paths
An absolute path starts with / and works from anywhere:
cd /usr
pwd
Output:
/usr
A relative path does not start with / and is resolved from the current directory:
cd ~/projects
cd site/css
pwd
Output:
~/projects/site/css
Starting from ~/projects, the path site/css leads down through site into css. Use .. to move up to the parent:
cd ..
pwd
Output:
~/projects/site
You can chain parent references. Each .. rises one level from where you stand now, so from ~/projects/site the path ../.. leads up two levels, to the home directory:
cd ../..
pwd
Output:
~
Shortcuts
Several shortcuts save typing:
| Command | Destination |
|---|---|
cd | Your home directory |
cd ~ | Your home directory |
cd - | The previous directory |
cd .. | The parent directory |
cd / | The root directory |
The shortcut cd - returns to wherever you were before the last cd, and it prints the directory name as it goes. Bash tracks the previous directory in the variable OLDPWD:
cd ~/projects/site
cd ~
cd -
Output of the last command (the directory it returns to, printed in full):
/home/student/projects/site
Plain cd with no argument always takes you home, no matter how deep you are:
cd ~/projects/site/css
cd
pwd
Output:
/home/student
Directories with Spaces
If a directory name has a space, quote it or escape the space, so that Bash sees one argument:
cd "my docs"
pwd
Output:
/home/student/my docs
The form cd my\ docs works the same way. Tab completion, covered in the lesson on using the terminal, inserts the backslashes for you.
When cd Fails
If the directory does not exist, cd prints an error and returns a failing exit status, and the shell stays where it was:
cd nowhere
echo "status=$?"
pwd
Output in an interactive shell:
bash: cd: nowhere: No such file or directory
status=1
/home/student
Because cd returns a non-zero status when it fails, scripts often write cd somewhere || exit 1, so that they stop instead of running the rest of their commands in the wrong place.
Combining the Three Commands
A typical navigation routine uses all three:
pwd
ls
cd projects
pwd
ls -l
cd site
ls
cd ..
cd
pwd
Sample output, starting in the home directory:
/home/student
my docs projects
/home/student/projects
total 4
-rw-r--r-- 1 student student 0 Nov 20 10:52 notes.txt
drwxr-xr-x 4 student student 4096 Nov 20 10:52 site
css index.html js
/home/student
Walking through the commands:
pwdshows the starting point, the home directory.lsreveals two entries,my docsandprojects.cd projectsmoves down one level, andpwdconfirms the new location.ls -lshows details, including thatsiteis a directory.cd siteandlsmove into it and show its three entries.cd ..goes back up toprojects, andcdwith no arguments returns home.- The final
pwdconfirms that the shell is back in/home/student.
Working this way, you can orient yourself at any moment with pwd, discover what is nearby with ls, and move with cd.