Basic Linux Commands
Master the terminal — your most powerful tool in Linux.
The Terminal
The terminal (or shell) is where you type commands to interact with Linux. Think of it as a text-based control center that lets you navigate files, manage processes, install software, and much more. The default shell on most Linux distributions is Bash (Bourne Again SHell).
Pro tip: The terminal is case-sensitive. File.txt and file.txt are two different files!
Navigation Commands
pwd — Print Working Directory
Shows the full path of your current location in the file system.
$ pwd
ls — List Files
Displays the contents of a directory. Add flags for more detail.
$ ls
$ ls -la
$ ls -lh /var/log
cd — Change Directory
Navigates to a different directory. Use special shortcuts to move quickly.
$ cd /home/user/Documents
$ cd .. # Go up one directory
$ cd ~ # Go to home directory
$ cd - # Go to previous directory
File Operations
mkdir — Make Directory
Creates new directories. Use the -p flag to create nested directories in one go.
$ mkdir projects
$ mkdir -p projects/web/css # Create nested directories
touch — Create Files
Creates empty files or updates the timestamp of existing files.
$ touch hello.txt
$ touch file1.txt file2.txt file3.txt
cp — Copy Files
Copies files or directories. Use -r to copy directories recursively.
$ cp file.txt backup.txt # Copy file
$ cp -r projects/ backup/ # Copy directory
mv — Move / Rename Files
Moves files to a new location, or renames them if the destination is in the same directory.
$ mv old.txt new.txt # Rename
$ mv file.txt /home/user/Documents/ # Move
rm — Remove Files
Deletes files and directories. Be careful — removed files don't go to a trash can!
$ rm file.txt # Remove file
$ rm -r directory/ # Remove directory
$ rm -rf directory/ # Force remove (use with caution!)
Warning: rm -rf / will delete everything on your system. Never run this command! Always double-check your rm commands.
Viewing File Content
cat — Display File Content
Outputs the entire content of a file to the terminal.
$ cat hello.txt
head / tail — View Beginning / End of Files
Peek at the first or last lines of a file — perfect for large log files.
$ head -n 5 log.txt # First 5 lines
$ tail -n 10 log.txt # Last 10 lines
$ tail -f log.txt # Follow (live updates)
grep — Search Inside Files
Finds lines matching a pattern inside one or more files.
$ grep "error" log.txt
$ grep -i "warning" log.txt # Case insensitive
$ grep -r "TODO" ./src/ # Recursive search
System Information
Quick reference for commands that tell you about your system.
| Command | Description | Example |
|---|---|---|
whoami |
Display current username | $ whoami → user |
hostname |
Show system hostname | $ hostname → linux-pc |
uname -a |
Full system information | $ uname -a |
df -h |
Disk usage (human readable) | $ df -h |
free -m |
Memory usage in megabytes | $ free -m |
top |
Real-time process monitor | $ top |
clear |
Clear the terminal screen | $ clear |
history |
Show command history | $ history |
man |
Open the manual for a command | $ man ls |
echo |
Print text to the terminal | $ echo "Hello!" |
Open your terminal and try these commands:
- Find your current location with
pwd - List all files including hidden ones with
ls -la - Create a directory called
linux-practice - Create a file inside it called
notes.txt - Display the contents with
cat notes.txt
# Step 1: Where am I?
$ pwd
/home/user
# Step 2: What's in this directory?
$ ls -la
# Step 3: Create a practice directory
$ mkdir linux-practice
# Step 4: Go inside and create a file
$ cd linux-practice
$ echo "My first Linux note!" > notes.txt
# Step 5: Read the file
$ cat notes.txt
My first Linux note!
Command Cheat Sheet
A quick-reference table of the most common commands grouped by category.
| Category | Command | What It Does |
|---|---|---|
| Navigation | pwd |
Print current directory |
ls |
List directory contents | |
cd |
Change directory | |
| Files | touch |
Create an empty file |
cp |
Copy files or directories | |
mv |
Move or rename files | |
rm |
Remove files or directories | |
mkdir |
Create a directory | |
| Viewing | cat |
Display file content |
head |
Show first lines of a file | |
tail |
Show last lines of a file | |
less |
Scroll through file content | |
| Search | grep |
Search for patterns in files |
find |
Locate files by name or type | |
which |
Show path of a command | |
| System | whoami |
Display current user |
df |
Show disk space usage | |
free |
Show memory usage | |
top |
Real-time process viewer | |
clear |
Clear the terminal screen |
Nice work! You've learned the essential commands. Next, let's understand how the Linux file system is organized.