Navigating the Linux Shell
Overview
The shell is your command-line interface to Linux. Learning to navigate the filesystem efficiently is the foundation of Linux mastery. Every file and directory has a path, and understanding how to move between them is essential.
Key Commands
Print Working Directory
pwd
Displays your current location in the filesystem. This shows the absolute path of where you are.
Example:
Output: /home/username - You are in the username home directory
pwdOutput: /home/username - You are in the username home directory
List Directory Contents
ls
Lists files and directories in your current location.
Options:
-l- Long format showing permissions, owner, size, and date-a- Shows all files including hidden ones (starting with .)-h- Human-readable file sizes (KB, MB, GB)-R- Recursively list subdirectories
Example:
Lists all files in long format with human-readable sizes
ls -lahLists all files in long format with human-readable sizes
Change Directory
cd directory_name
Moves you to a different directory. This is how you navigate the filesystem.
Special Paths:
cd ~- Go to your home directorycd ..- Go up one directory levelcd -- Go to previous directorycd /- Go to root directorycd ../../- Go up two directory levels
Example:
Moves into the Documents folder from your current location
cd DocumentsMoves into the Documents folder from your current location
Create Directory
mkdir directory_name
Creates a new directory (folder) in your current location.
Options:
-p- Creates parent directories if they don't exist
Example:
Creates the entire directory structure at once
mkdir -p projects/web/cssCreates the entire directory structure at once
Remove Directory
rmdir directory_name
Removes an empty directory. Use with caution!
Alternative:
rm -r directory_name- Removes directory and all contents (dangerous!)
Path Types
Absolute Path
Starts from the root directory (/) and specifies the complete path.
Example:
Full path from root to the file
/home/username/Documents/report.txtFull path from root to the file
Relative Path
Starts from your current location, doesn't begin with /
Example:
Path relative to where you currently are
Documents/report.txtPath relative to where you currently are
Important Notes
- Case sensitive: Linux treats Documents and documents as different directories
- Tab completion: Press Tab to auto-complete directory and file names
- Spaces in names: Use quotes or backslashes: cd "My Documents" or cd My\ Documents
- Hidden files: Files starting with . are hidden, use ls -a to see them