2.3 Using Directories and Listing Files
Key Takeaways
- Linux organizes data as a single directory tree; files hold content and directories hold names that point to other files or directories
- Names beginning with a dot are hidden; ls without -a omits them, while ls -a (or ls -A) reveals them
- Absolute paths start at /; relative paths start from the current working directory; ~ expands to your home directory
- cd changes directory; . means the current directory and .. means the parent; cd or cd ~ returns to home
- ls lists directory entries; common flags include -l (long), -a/-A (hidden), -h (human sizes), -R (recursive), and -d (list the directory itself)
2.3 Using Directories and Listing Files
Quick Answer: Everything on a Linux system lives in one directory tree rooted at
/. You move withcd, see names withls, and address locations with absolute paths (from/), relative paths (from where you are), or~for your home. Dotfiles stay hidden until you asklsto show them with-aor-A.
Objective 2.3 is about finding your way: files vs directories, paths, home, hidden names, cd, and ls.
Files Versus Directories
A file stores content (text, a program, an image). A directory stores a list of names that refer to other files or directories. Directories form a hierarchy: each directory except / has a parent, and most have children.
On the command line you work with pathnames. Two styles matter on every location question:
| Path type | Starts with | Resolved from | Example |
|---|---|---|---|
| Absolute | / | Filesystem root | /etc/hosts, /home/alex/notes.txt |
| Relative | anything else | Current working directory | notes.txt, docs/readme, ../backup |
Absolute paths always mean the same place. Relative paths change when you cd. Confirm location with pwd if unsure.
Home Directories and ~
Each normal user has a home directory—a personal starting place for documents and configuration. On most systems that path looks like /home/username. The root account’s home is usually /root.
The shell expands ~ to the current user’s home directory:
cd ~
cd ~/Documents
ls ~/.
Bare cd with no arguments also returns you to your home directory—the same destination as cd ~. Many Essentials tasks happen under ~, while system files live under absolute paths such as /etc.
Hidden Files and Directories
Any name that begins with a dot (.) is a hidden file or directory: .bashrc, .ssh, .config. Hidden does not mean encrypted—it only means ordinary listings skip those names.
ls # omits names that start with .
ls -a # show all entries, including . and .. and hidden names
ls -A # almost all: hidden names, but usually not . and ..
Exam trap: If a question asks how to see .bashrc in a listing, the answer involves -a or -A (or an explicit path to that name), not a second ls with no options.
Absolute Paths, Relative Paths, ., and ..
Two special directory names appear in every listing that includes them:
| Name | Meaning |
|---|---|
. | The current directory |
.. | The parent directory (one level up) |
Use them constantly in relative paths:
cd ..
cd ../..
ls ./scripts
cp ../shared/config.txt .
From /home/alex, relative projects/app matches absolute /home/alex/projects/app. From that app directory, .. is projects and ../.. is home.
Worked path scenario
Suppose pwd prints /home/sam/lab:
/home/sam/
lab/
notes.txt
data/
sample.csv
.bashrc
| You type | Effect |
|---|---|
ls | Shows notes.txt and data (not .bashrc) |
ls -a ~ | Lists home including .bashrc |
cd data | Working directory becomes /home/sam/lab/data |
cd .. | Back to /home/sam/lab |
ls /home/sam/lab/notes.txt | Absolute path to the file from anywhere |
ls ../.bashrc | Relative path from lab up to home’s hidden file |
Same destination, three path styles—practice reading all three on sight:
ls /home/sam/lab/data/sample.csv # absolute from /
ls data/sample.csv # relative from /home/sam/lab
ls ~/lab/data/sample.csv # via home shortcut
ls ./data/../notes.txt # . and .. cancel; resolves to notes.txt here
ls ../../etc/hosts # climb toward root (from deep under /home)
If you cd /tmp first, ls data/sample.csv no longer finds Sam’s file, but ls /home/sam/lab/data/sample.csv and ls ~/lab/data/sample.csv (for user sam) still do. That contrast is the exam point of absolute versus relative paths.
Which path is absolute on a Linux system?
Changing Directories With cd
cd (change directory) updates your current working directory:
cd /etc
cd /home/alex/projects
cd ..
cd ~
cd
Prefer absolute paths for distant jumps (cd /var/log), relative paths for nearby moves (cd docs, cd ..), and cd / cd ~ to reset home. If cd fails with “No such file or directory,” the path is missing or mistyped—and remember Linux names are case-sensitive.
Listing With ls and Common Options
ls lists directory contents. Alone, it lists the current directory in a short view.
ls
ls /etc
ls ~/lab
Options you must recognize
| Option | Purpose |
|---|---|
-l | Long listing: type/permissions, links, owner, group, size, timestamp, name |
-a | Show all entries, including hidden names and . / .. |
-A | Show hidden names but typically omit . and .. |
-h | With -l, print sizes in human-readable units (K, M, G) |
-R | Recursive: list this directory and every subdirectory beneath it |
-d | List the directory entry itself instead of its contents |
Flags combine. Everyday combos:
ls -la # long + all (including hidden)
ls -lah ~/Downloads # long, all, human-readable sizes
ls -l /etc/hosts # long details for one file
ls -ld /etc # long details for the /etc directory itself
ls -R projects # recursive tree listing under projects
ls -lR ~/lab # long + recursive under home lab
ls -la /home/sam/.ssh # absolute path into a hidden directory
ls -ld ~/lab data # metadata for each named directory, not their children
Without -R, ls projects shows only direct children. With ls -R projects, nested folders and their contents appear too—-R means descend into subdirectories. Compare ls ~/lab (one level) with ls -R ~/lab (every nested name under that tree).
ls -l /tmp shows what is inside /tmp. ls -ld /tmp shows metadata for the directory itself. Questions about “the directory, not its contents” point at -d. You can also pass several targets: ls -l /etc/hosts ~/lab/notes.txt lists those two files without changing directory.
Putting Navigation and Listing Together
pwd # where am I?
ls -la # what is here, including hidden?
cd ~/lab
ls -lah
cd ..
ls -R data # peek the subtree before entering it
| Task | Command |
|---|---|
| Show current directory | pwd |
| Go home | cd or cd ~ |
| Go up one level | cd .. |
| Long + hidden | ls -la |
| Recursive listing | ls -R |
| Directory itself, not contents | ls -d name |
Master these moves before creating or deleting anything—wrong directory plus a powerful rm is how beginners erase the wrong tree.
You are in /home/sam/lab and want to return to /home/sam without typing the full absolute path. Which command does that?
Which ls invocation shows hidden names such as .bashrc in the current directory?
What does the ~ character represent in a path like ~/Documents?