Terminal Basics
Before diving deeper into Bash scripting, it's essential to master the basic terminal commands. These commands form the foundation of everything you'll do in Bash. You'll learn navigation commands to move around your file system, file operations to create and manipulate files, and other essential commands that you'll use constantly.
Navigation Commands
Moving around your file system is one of the most fundamental skills. These commands help you navigate directories and understand where you are.
Click Run to execute your code
| Command | Description | Example |
|---|---|---|
pwd |
Print Working Directory - shows current directory | pwd → /home/user |
cd |
Change Directory - navigate to a directory | cd /tmp or cd ~ |
cd ~ |
Go to home directory | cd ~ |
cd .. |
Go up one directory level | cd .. |
cd - |
Go to previous directory | cd - |
ls |
List files and directories | ls -la |
-
~ - Your home directory (/home/username or /Users/username)-
. - Current directory-
.. - Parent directory-
- - Previous directory (used with cd)
Listing Files (ls)
The ls command lists files and directories. It has many useful options for different views.
# Basic listing
ls
# Long format with details
ls -l
# Show hidden files (starting with .)
ls -a
# Long format + hidden files
ls -la
# Human-readable file sizes
ls -lh
# Sort by modification time (newest first)
ls -lt
# Reverse order
ls -lr
# Recursive (show subdirectories)
ls -R
-l flag shows file permissions, ownership, size, and modification date. Combined with -h (human-readable), you get sizes in KB, MB, GB instead of bytes!
File Operations
Creating, copying, moving, and deleting files are essential operations you'll perform constantly in Bash scripts.
| Command | Description | Example |
|---|---|---|
touch |
Create empty file or update timestamp | touch file.txt |
mkdir |
Create directory | mkdir mydir or mkdir -p path/to/dir |
cp |
Copy file or directory | cp source.txt dest.txt or cp -r dir1 dir2 |
mv |
Move or rename file/directory | mv old.txt new.txt or mv file.txt /tmp/ |
rm |
Remove file | rm file.txt or rm -rf dir/ |
rmdir |
Remove empty directory | rmdir emptydir |
rm -rf!-
-r means recursive (deletes directories and contents)-
-f means force (no confirmation prompts)- There's no "undo" or "trash" - deleted files are gone forever!
- Double-check paths before using
rm -rf
Common Command Options
Many commands share similar options. Understanding these patterns makes learning new commands easier.
# mkdir -p creates parent directories if needed
mkdir -p project/src/main/java
# cp -r copies directories recursively
cp -r source_dir/ dest_dir/
# rm -i prompts before deleting (safer)
rm -i important_file.txt
# mv can rename files
mv oldname.txt newname.txt
# Multiple operations
touch file1.txt file2.txt file3.txt
mkdir dir1 dir2 dir3
-
-a or --all - Show/all (including hidden)-
-r or -R - Recursive (for directories)-
-f - Force (no prompts)-
-i - Interactive (prompt before actions)-
-v - Verbose (show what's happening)-
-h - Human-readable (sizes, dates)
Other Essential Commands
These commands are frequently used in scripts and daily terminal work.
| Command | Description | Use Case |
|---|---|---|
cat |
Display file contents | cat file.txt |
less / more |
View file page by page | less longfile.txt |
head |
Show first lines of file | head -n 10 file.txt |
tail |
Show last lines of file | tail -f logfile.txt (follow mode) |
find |
Search for files | find . -name "*.txt" |
grep |
Search text in files | grep "error" logfile.txt |
which |
Show path to command | which python |
whoami |
Show current username | whoami |
date |
Show current date/time | date or date +"%Y-%m-%d" |
Common Mistakes
1. Not understanding relative vs absolute paths
# Wrong - assuming you're in a specific directory
cd documents/project # Fails if not in home directory
# Correct - use absolute path or check current location
cd ~/documents/project # Absolute path from home
# Or
pwd # Check where you are first
cd documents/project # Then use relative path
2. Using rm without -r for directories
# Wrong - rm doesn't remove directories
rm mydir # Error: is a directory
# Correct - use rmdir for empty dirs or rm -r for non-empty
rmdir mydir # Only works if directory is empty
rm -r mydir # Removes directory and contents
rm -rf mydir # Force remove (no prompts)
3. Not creating parent directories with mkdir
# Wrong - fails if parent doesn't exist
mkdir project/src/main # Error if project/src doesn't exist
# Correct - use -p to create parent directories
mkdir -p project/src/main # Creates all necessary directories
4. Confusing cp and mv
# cp - copies file (original remains)
cp file.txt backup.txt # Now you have both files
# mv - moves/renames file (original is gone)
mv file.txt renamed.txt # file.txt no longer exists, renamed.txt does
# Use cp when you want to keep original
# Use mv when you want to move or rename
Exercise: Navigate and Organize Files
Task: Practice using terminal commands to create a project structure!
Requirements:
- Create a directory called
myproject - Inside it, create subdirectories:
src,docs,scripts - Create a file called
README.txtin the main directory - Copy
README.txtto thedocsdirectory - List all files and directories in your project
- Show the full path to your project directory
Hint: Use mkdir -p to create nested directories, and remember to navigate with cd!
Show Solution
# Create project directory
mkdir -p myproject/src myproject/docs myproject/scripts
# Navigate into project
cd myproject
# Create README file
touch README.txt
# Copy to docs directory
cp README.txt docs/
# List all files
ls -la
# Show full path
pwd
# Or use find to see structure
find . -type f
Summary
- Navigation:
pwd(where am I),cd(change directory),ls(list files) - File Creation:
touch(create file),mkdir(create directory) - File Operations:
cp(copy),mv(move/rename),rm(remove) - Useful Options:
-r(recursive),-f(force),-i(interactive),-a(all),-l(long format),-h(human-readable) - Special Directories:
~(home),.(current),..(parent),-(previous) - Safety: Be careful with
rm -rf- there's no undo! Use-ifor safety. - Other Commands:
cat,head,tail,find,grep,which,whoami,date
What's Next?
Excellent! You've mastered the essential terminal commands. Now that you understand navigation and file operations, we're ready to dive into Variables & Environment. You'll learn how to store data in variables, work with environment variables, use special variables, and handle arrays - the building blocks of powerful Bash scripts!
Enjoying these tutorials?