Navigating the file system is one of the most fundamental skills in Linux shell scripting. Whether you’re managing files, writing scripts, or troubleshooting, knowing how to move around efficiently is essential. In this chapter, we’ll cover the most common commands for directory navigation and listing contents.
1. pwd
– Print Working Directory
When you’re deep in the file system, it’s easy to lose track of where you are. The pwd
(Print Working Directory) command tells you your current location.
Example:
pwd
Output:
/home/username/Documents
This shows you’re in the Documents
directory under your user’s home folder.
2. ls
– List Directory Contents
The ls
command lists files and directories in your current location. It has several useful flags to customize the output.
Basic Usage:
ls
Lists files and directories in a simple format.
Common Flags:
Flag | Description | Example |
---|---|---|
-l | Long format (shows permissions, owner, size, etc.) | ls -l |
-a | Show hidden files (those starting with . ) | ls -a |
-h | Human-readable file sizes (with -l ) | ls -lh |
-R | Recursively list subdirectories | ls -R |
Example:
ls -lah
Output:
drwxr-xr-x 5 user user 4.0K Jan 10 10:00 .
drwxr-xr-x 3 user user 4.0K Jan 9 09:00 ..
-rw-r--r-- 1 user user 123 Jan 10 09:30 file.txt
drwxr-xr-x 2 user user 4.0K Jan 8 08:00 scripts
This shows all files (including hidden ones) in a detailed, human-readable format.
3. cd
– Change Directory
The cd
(Change Directory) command lets you move between folders.
Basic Navigation:
Command | Action |
---|---|
cd folder | Move into folder |
cd .. | Move up one directory |
cd ~ or just cd | Go to home directory |
cd / | Go to root directory |
cd - | Switch back to the previous directory |
Examples:
cd Documents/Projects # Move into Projects folder
cd .. # Go back to Documents
cd ~ # Return to home directory
cd /etc # Jump to the system's etc folder
4. Autocomplete with Tab
Bash has a handy autocomplete feature to save typing and avoid errors.
- Start typing a directory or filename.
- Press
Tab
to autocomplete. - If multiple matches exist, press
Tab
twice to see options.
Example:
cd Docu[Tab] # Auto-completes to "Documents/"
cd /e[Tab][Tab] # Shows possible matches like /etc /env /export
Summary Cheat Sheet
Command | Description |
---|---|
pwd | Show current directory |
ls | List files and directories |
ls -lah | Detailed list with hidden files |
cd [dir] | Change directory |
cd .. | Move up one level |
Tab | Autocomplete paths |
Remembering these commands will make your Linux shell experience much smoother. In the next chapter, we’ll dive into file operations—creating, copying, moving, and deleting files.
Leave a Reply