Filesystem Navigation

Now that you understand the filesystem structure, let's learn how to move around efficiently. Mastering navigation is essential for becoming comfortable with Linux.

Webmonkey Style: Think of the filesystem as a city. You need to know how to walk around (cd), read street signs (ls), and find addresses (find) to get anywhere efficiently.

Understanding Your Location

Current Working Directory

The pwd command (print working directory) shows where you currently are in the filesystem.

user@ubuntu:~$ pwd
/home/user
Home Directory Shortcut: The ~ symbol always represents your home directory. That's why your prompt often shows user@ubuntu:~$.

Listing Directory Contents

The ls command shows what's in your current directory.

user@ubuntu:~$ ls
Documents  Downloads  Music  Pictures  Public  Templates  Videos

user@ubuntu:~$ ls -l
total 24
drwxr-xr-x 2 user user 4096 Dec 10 09:00 Documents
drwxr-xr-x 2 user user 4096 Dec 10 09:00 Downloads
drwxr-xr-x 2 user user 4096 Dec 10 09:00 Music
drwxr-xr-x 2 user user 4096 Dec 10 09:00 Pictures

Changing Directories

Basic Navigation with cd

The cd command (change directory) moves you around the filesystem.

# Go to Documents directory
user@ubuntu:~$ cd Documents
user@ubuntu:~/Documents$ pwd
/home/user/Documents

# Go back to home directory
user@ubuntu:~/Documents$ cd ~
user@ubuntu:~$ pwd
/home/user

Path Types

Linux supports two types of paths: absolute and relative.

Absolute Paths

Start from root (/) and specify the complete path.

user@ubuntu:~$ cd /var/log
user@ubuntu:/var/log$ pwd
/var/log

Relative Paths

Start from your current directory.

user@ubuntu:~$ cd Documents
user@ubuntu:~/Documents$ cd ..
user@ubuntu:~$ pwd
/home/user

Navigation Shortcuts

Special Directory References

# Current directory (.)
user@ubuntu:~$ ls .
Documents  Downloads  Music  Pictures  Public  Templates  Videos

# Parent directory (..)
user@ubuntu:~/Documents$ cd ..
user@ubuntu:~$ pwd
/home/user

# Previous directory (-)
user@ubuntu:~$ cd /var/log
user@ubuntu:/var/log$ cd -
/home/user

Tab Completion

Press Tab to autocomplete file and directory names. This is your best friend!

# Type 'cd D' and press Tab
user@ubuntu:~$ cd D[Tab]
cd Documents/  Downloads/

# Type 'cd Do' and press Tab
user@ubuntu:~$ cd Do[Tab]
user@ubuntu:~$ cd Documents/
Pro Tip: If Tab completion doesn't work, press Tab twice to see all possible matches.

Advanced Listing Options

Useful ls Flags

# Show hidden files (starting with .)
user@ubuntu:~$ ls -a
.  ..  .bashrc  .profile  Documents  Downloads  Music  Pictures

# Human-readable sizes
user@ubuntu:~$ ls -lh
total 24K
drwxr-xr-x 2 user user 4.0K Dec 10 09:00 Documents
drwxr-xr-x 2 user user 4.0K Dec 10 09:00 Downloads

# Show file types with colors
user@ubuntu:~$ ls --color=auto

# Long format with human sizes and all files
user@ubuntu:~$ ls -lah
total 32K
drwxr-xr-x 12 user user 4.0K Dec 10 10:00 .
drwxr-xr-x  3 root root 4.0K Dec 10 08:00 ..
-rw-------  1 user user 1.2K Dec 10 09:00 .bashrc
drwxr-xr-x  2 user user 4.0K Dec 10 09:00 Documents

Creating Aliases

Create shortcuts for frequently used commands.

# Add to ~/.bashrc
user@ubuntu:~$ echo 'alias ll="ls -lah"' >> ~/.bashrc
user@ubuntu:~$ source ~/.bashrc
user@ubuntu:~$ ll
total 32K
drwxr-xr-x 12 user user 4.0K Dec 10 10:00 .
drwxr-xr-x  3 root root 4.0K Dec 10 08:00 ..
-rw-------  1 user user 1.2K Dec 10 09:00 .bashrc

Finding Files and Directories

find Command

Powerful search tool for finding files and directories.

# Find by name
user@ubuntu:~$ find . -name "*.html"
./projects/website/index.html
./projects/website/about.html

# Find by type (f=file, d=directory)
user@ubuntu:~$ find . -type d
.
./projects
./projects/website
./Documents

# Find by size
user@ubuntu:~$ find . -size +1M
./downloads/bigfile.zip
./videos/movie.mp4

locate Command

Faster search using a database (needs to be updated).

# Update database first
user@ubuntu:~$ sudo updatedb

# Search for nginx config
user@ubuntu:~$ locate nginx.conf
/etc/nginx/nginx.conf
/usr/share/doc/nginx/examples/nginx.conf

which Command

Finds where a command is located.

user@ubuntu:~$ which python3
/usr/bin/python3

user@ubuntu:~$ which nginx
/usr/sbin/nginx

Web Development Navigation Examples

Navigating Web Projects

# Go to web project
user@ubuntu:~$ cd projects/mywebsite
user@ubuntu:~/projects/mywebsite$ ls -la
total 16
drwxr-xr-x 3 user user 4096 Dec 10 10:00 .
drwxr-xr-x 5 user user 4096 Dec 10 09:00 ..
-rw-r--r-- 1 user user 10918 Dec 10 10:00 index.html
-rw-r--r-- 1 user user  1024 Dec 10 09:30 style.css
drwxr-xr-x 2 user user 4096 Dec 10 08:00 images

Finding Configuration Files

# Find all nginx config files
user@ubuntu:~$ find /etc -name "*.conf" | grep nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d/default.conf

# Find web server logs
user@ubuntu:~$ find /var/log -name "*nginx*"
/var/log/nginx/access.log
/var/log/nginx/error.log

Working with Symbolic Links

# Create symlink to current project
user@ubuntu:~$ ln -s $HOME/projects/mywebsite $HOME/current_project
user@ubuntu:~$ cd current_project
user@ubuntu:~/current_project$ pwd
/home/user/projects/mywebsite

Directory Tree Visualization

tree Command

Shows directory structure in a tree format (may need to install).

# Install tree if needed
user@ubuntu:~$ sudo apt install tree

# Show project structure
user@ubuntu:~$ tree projects/mywebsite
projects/mywebsite/
├── index.html
├── style.css
└── images/
    ├── logo.png
    └── background.jpg

2 directories, 4 files

Navigation Tips and Tricks

Quick Navigation

# Go to root
user@ubuntu:~/projects/mywebsite$ cd /

# Go to parent's parent
user@ubuntu:~/projects/mywebsite$ cd ../..

# Go to specific user's home
user@ubuntu:~$ cd ~otheruser

# Go back multiple levels
user@ubuntu:~/deep/nested/path$ cd ../../..

Pushd and popd

Stack-based navigation for jumping between locations.

# Push current directory to stack and go to new location
user@ubuntu:~$ pushd /var/log
~/ /var/log

# Pop back to previous location
user@ubuntu:/var/log$ popd
/var/log ~

Key Takeaways

Practice Exercises

  1. Navigate to /var/log and list the contents
  2. Find your way back to your home directory using different methods
  3. Create a project directory structure and navigate through it
  4. Use find to locate all configuration files in /etc
  5. Practice tab completion with long directory names
Congratulations! You've completed Module 2! You now understand the Linux filesystem structure and can navigate confidently. In the next module, we'll explore file permissions.
Next Module: File Permissions →