Navigation Commands
Before you can do anything useful on Linux, you need to move around the filesystem. These commands are the foundation of everything else.
pwd — Print Working Directory
pwd tells you exactly where you are in the filesystem:
pwd
# /home/ubuntu
cd /var/log
pwd
# /var/log
cd ~
pwd
# /home/ubuntu
ls — List Directory Contents
ls is the most-used Linux command. Its flags dramatically change what it shows:
Basic Usage
ls
# bin Desktop Documents Downloads projects
ls /etc
# apt bash.bashrc crontab group hosts nginx passwd ...
ls /var/log
# apt auth.log dpkg.log kern.log syslog ...
Essential Flags
# -l: long format (permissions, owner, size, date)
ls -l
# total 36
# drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 1 09:00 Desktop
# drwxr-xr-x 2 ubuntu ubuntu 4096 Apr 1 09:00 Documents
# -rw-r--r-- 1 ubuntu ubuntu 220 Mar 15 08:00 .bash_logout
# -a: show all files, including hidden (dot files)
ls -a
# . .. .bashrc .profile .ssh Desktop Documents Downloads
# -la: combine long format + all files (most common combo)
ls -la
# total 60
# drwxr-x--- 7 ubuntu ubuntu 4096 Apr 1 09:00 .
# drwxr-xr-x 4 root root 4096 Mar 15 08:00 ..
# -rw------- 1 ubuntu ubuntu 123 Apr 1 09:00 .bash_history
# -rw-r--r-- 1 ubuntu ubuntu 220 Mar 15 08:00 .bash_logout
# -rw-r--r-- 1 ubuntu ubuntu 3526 Mar 15 08:00 .bashrc
# -h: human-readable sizes (KB, MB, GB instead of bytes)
ls -lh
# total 36K
# drwxr-xr-x 2 ubuntu ubuntu 4.0K Apr 1 09:00 Desktop
# -rw-r--r-- 1 ubuntu ubuntu 1.2M Apr 1 09:00 backup.tar.gz
# -R: recursive — list directory and all subdirectories
ls -R projects/
# projects/:
# api frontend scripts
# projects/api:
# Dockerfile main.go go.mod
# projects/frontend:
# index.html styles.css
# -t: sort by modification time (newest first)
ls -lt
# -rw-r--r-- 1 ubuntu ubuntu 2048 Apr 1 10:00 latest.log
# -rw-r--r-- 1 ubuntu ubuntu 1024 Apr 1 09:00 older.log
# -S: sort by file size (largest first)
ls -lhS /var/log
# -rw-r----- 1 syslog adm 45M Apr 1 10:00 syslog
# -rw-r----- 1 syslog adm 12M Apr 1 10:00 auth.log
# --color=auto: coloured output (usually on by default)
ls --color=auto
# Directories in blue, executables in green, symlinks in cyan
# Combine flags — list all files, human-readable, sorted by time
ls -laht
Reading ls -l Output
-rw-r--r-- 1 ubuntu ubuntu 3526 Mar 15 08:00 .bashrc
│ │ │ │ │ │ │
│ │ │ │ │ │ └── Filename
│ │ │ │ │ └── Last modified date
│ │ │ │ └── File size (bytes unless -h)
│ │ │ └── Group owner
│ │ └── User owner
│ └── Number of hard links
└── Permissions: [type][user][group][other]
- = regular file
d = directory
l = symbolic link
rw-r--r-- = owner can read+write, group can read, others can read
cd — Change Directory
# Go to an absolute path
cd /var/log
# Go to home directory (three equivalent ways)
cd ~
cd $HOME
cd # cd with no argument goes to home
# Go up one directory
cd ..
# Go up two directories
cd ../..
# Go to the previous directory (toggle between two dirs)
cd -
# /var/log
cd -
# /home/ubuntu
# Go into a directory relative to current location
cd projects/api
# Autocomplete with Tab key (type partial name and press Tab)
cd /var/l[Tab] → cd /var/log/
pushd and popd — Directory Stack
pushd and popd let you maintain a stack of directories and jump between them:
# Start in home
pwd
# /home/ubuntu
# Push /var/log onto the stack and go there
pushd /var/log
# /var/log ~
# (prints the stack: /var/log is current, ~ is saved)
# Push /etc onto the stack
pushd /etc
# /etc /var/log ~
# Show the directory stack
dirs
# /etc /var/log ~
# (leftmost = current, rightmost = oldest)
# Pop back to /var/log
popd
# /var/log ~
# Pop back to home
popd
# ~
pwd
# /home/ubuntu
pushd/popd are useful when navigating between multiple deeply nested directories:
# Example workflow: editing config while monitoring logs
pushd /etc/nginx # Go to nginx config
vim nginx.conf # Edit the config
pushd /var/log/nginx # Go check the logs
tail -f access.log # Monitor logs
popd # Back to /etc/nginx
tree — Visualise Directory Hierarchy
tree prints the directory structure as a visual tree — great for understanding project layouts:
# Install tree first (may not be pre-installed)
sudo apt install tree
# Show current directory structure
tree
# .
# ├── configs/
# │ ├── nginx.conf
# │ └── redis.conf
# ├── scripts/
# │ ├── backup.sh
# │ └── deploy.sh
# └── README.md
# Limit depth
tree -L 2 /etc
# /etc
# ├── apt/
# │ ├── apt.conf.d/
# │ └── sources.list
# ├── nginx/
# │ ├── conf.d/
# │ └── nginx.conf
# └── ssh/
# └── sshd_config
# Show hidden files
tree -a ~/
# Show file sizes
tree -h ~/projects/
# .
# ├── [4.0K] api/
# │ ├── [ 12K] main.go
# │ └── [1.2K] go.mod
# Show only directories
tree -d /var
# Limit output and pipe through less
tree -L 3 / | less
Navigation Shortcuts Summary
# Directory shortcuts
~ # Your home directory (/home/username)
. # Current directory
.. # Parent directory
- # Previous directory (used with cd)
/ # Root directory
# Quick navigation
cd ~ # Go home
cd - # Toggle between last two directories
cd .. # Up one level
cd ../.. # Up two levels
# Tab completion
ls /va[Tab] → ls /var/
ls /var/l[Tab] → ls /var/log/ (if unique)
cd ~/pr[Tab] → cd ~/projects/ (if unique)
# Double-Tab shows all completions when ambiguous
Key Takeaways
pwdalways tells you exactly where you are — use it when you feel lostls -lais the most information-rich list view — combine flags freelyls -lhmakes file sizes readable (KB, MB, GB)cd -is the fastest way to toggle between two directoriespushd/popdbuild a navigation stack — ideal for multi-directory workflowstree -L 2gives an instant overview of any project or config directory- Tab completion saves time — use it constantly
| ← Back to Common Commands Overview | Next → File Management Commands |