Basic Linux Commands: The Essential Guide for Beginners
Master the fundamental commands that form the foundation of all Linux system administration and DevOps work.
📅 Published: Feb 2026
⏱️ Estimated Reading Time: 18 minutes
🏷️ Tags: Linux Commands, CLI Basics, Terminal, Beginners Guide, Command Line
📖 Introduction: Why Learn Linux Commands?
Before we dive into specific commands, let's understand why learning Linux commands is so important. Imagine you're given keys to a building, but you only know how to open the front door. Linux commands are like having the master keychain - you can open every door, control every system, and fix any problem.
In the world of technology, especially in DevOps and system administration, 90% of servers run Linux. These servers don't have graphical interfaces like your Windows or Mac computer. They're managed entirely through commands typed into a terminal. Whether you're deploying a website, troubleshooting a database, or automating backups, you'll be using these basic Linux commands.
Think of it this way: If Linux were a car, commands would be the steering wheel, pedals, and gear shift. You need to learn them to drive effectively. The good news? Once you learn about 20 basic commands, you can do 80% of all common tasks!
🗺️ Navigation Commands: Finding Your Way Around
pwd: Where Am I?
The pwd command stands for Print Working Directory. It's like looking at your GPS location or checking the address of the building you're in. Whenever you feel lost in the Linux filesystem (and everyone does at first), just type pwd to see exactly where you are.
pwd # Output: /home/yourusername # This tells you're in your home directory
Real-world analogy: You're in a large shopping mall and check the "You Are Here" map. pwd gives you that same "You Are Here" information for your Linux system.
ls: What's Here?
The ls command stands for List. It shows you what files and folders are in your current location. Think of it as opening a drawer or looking around a room to see what's inside.
# Basic listing ls # Detailed listing (most useful) ls -l # Shows: permissions, owner, size, date, filename # Human readable sizes ls -lh # Shows sizes in KB, MB, GB instead of bytes # Show all files (including hidden ones starting with .) ls -la # Hidden files are configuration files, usually not shown by default # Sort by modification time (newest first) ls -lt
Why the options matter:
-lgives you the "details view" like in Windows Explorer-ashows hidden files (configuration files that start with dot)-hmakes file sizes readable (1.5M instead of 1572864 bytes)
Real-world use: When you connect to a server for the first time, ls is usually the first command you run to see what's there.
cd: Changing Locations
The cd command stands for Change Directory. It's how you move between folders, just like walking from one room to another in a building.
# Go to home directory cd ~ # The ~ symbol always means "home directory" # Go to root directory (top level) cd / # Go up one level cd .. # The .. means "parent directory" # Go to specific directory cd /var/log # This takes you directly to the log directory # Go to previous directory cd - # This takes you back to where you just were
Common mistake: Forgetting that Linux is case-sensitive. cd Documents is different from cd documents.
Real-world scenario: You're troubleshooting and need to check logs. You'd use cd /var/log to go to the log directory, then ls to see what log files are available.
📁 File and Directory Management
mkdir: Creating New Folders
The mkdir command stands for Make Directory. It creates new folders (directories) for organizing your files.
# Create a single directory mkdir projects # Create multiple directories at once mkdir docs images scripts # Create nested directory structure mkdir -p projects/{src,test,docs,backup} # This creates: projects/src, projects/test, etc. # Create directory with specific permissions mkdir -m 755 shared_folder # Sets read/write/execute for owner, read/execute for others
The -p flag is magical: It creates parent directories if they don't exist. Without -p, if you try mkdir a/b/c and directory a doesn't exist, you'll get an error.
Real-world use: Setting up a new project structure with organized folders for different types of files.
touch: Creating Empty Files
The touch command creates empty files. It's named "touch" because it can also update the timestamp of existing files (like "touching" them to update).
# Create a single file touch notes.txt # Create multiple files touch file1.txt file2.txt file3.txt # Create files with pattern touch file{1..5}.txt # Creates: file1.txt, file2.txt, file3.txt, file4.txt, file5.txt # Update timestamp of existing file touch existing_file.txt # Updates the "last modified" time to now
Why create empty files? Often you need placeholder files, configuration files that will be filled later, or you want to mark that a task was completed.
rm: Removing Files and Directories
The rm command stands for Remove. This is the Linux equivalent of deleting files, but with an important warning: Linux has no recycle bin. Deleted files are gone forever unless you have backups.
# Delete a file (carefully!) rm file.txt # Delete multiple files rm file1.txt file2.txt file3.txt # Delete directory and everything in it rm -r directory_name # -r means "recursive" - goes through all subdirectories # Force delete without confirmation rm -f file.txt # -f means "force" - suppresses warnings # Interactive delete (safer) rm -i file.txt # Asks: "remove file.txt?" before deleting
⚠️ DANGER ZONE: rm -rf / would delete EVERYTHING on your system. Never run this unless you want to destroy your Linux installation!
Safety tip: Always use ls first to confirm what you're deleting. Some people create an alias: alias rm='rm -i' to make rm always ask for confirmation.
cp: Copying Files
The cp command stands for Copy. It duplicates files or directories.
# Copy file to another location cp source.txt destination/ # Copy with new name cp oldfile.txt newfile.txt # Copy directory recursively cp -r sourcedir/ destinationdir/ # -r means copy all files and subdirectories # Preserve file attributes cp -p importantfile.txt backup/ # -p preserves timestamps and permissions # Interactive copy (ask before overwriting) cp -i source.txt destination/
Common mistake: Forgetting the trailing slash. cp -r dir1 dir2 behaves differently than cp -r dir1/ dir2/. With the slash, you copy the contents. Without, you might create dir2/dir1/ structure.
mv: Moving or Renaming
The mv command stands for Move, but it's also used for renaming files. Think of it as picking up a file and putting it somewhere else.
# Rename a file mv oldname.txt newname.txt # Move file to another directory mv file.txt /home/user/documents/ # Move and rename at same time mv file.txt /backup/file-backup.txt # Move directory mv olddir/ newlocation/ # Interactive mode (ask before overwriting) mv -i source.txt destination/
Key insight: In Linux, renaming is just moving a file to the same location with a different name. That's why mv handles both operations.
👀 Viewing and Examining Files
cat: Concatenate and Display
The cat command stands for Concatenate, but it's most commonly used to display file contents. It shows you the entire file at once.
# View a file cat filename.txt # View multiple files cat file1.txt file2.txt # Combine files (actual concatenation) cat part1.txt part2.txt > combined.txt # Add line numbers cat -n filename.txt # Show non-printing characters cat -A filename.txt # Shows tabs as ^I, line endings as $
Best for: Small files. If you try cat on a 1GB log file, you'll be watching text scroll for a long time!
Real-world use: Quick checks of configuration files or small scripts.
less: The Smart File Viewer
The less command is for viewing large files. It shows one screen at a time and lets you navigate through the file.
# View a file page by page less largefile.log # Once in less, you can: # Spacebar = Next page # b = Previous page # /pattern = Search for pattern # q = Quit # g = Go to beginning # G = Go to end
Why it's called "less": It's a pun! There was an older program called more that could only go forward. less is "more" but can go backward too (less is more).
Pro tip: less is what runs automatically when you use man commands or when Git shows you diff outputs.
head and tail: Seeing Beginnings and Ends
These commands show just the beginning or end of files, which is incredibly useful for logs.
# Show first 10 lines (default) head filename.txt # Show first 20 lines head -20 filename.txt # Show last 10 lines (default) tail filename.txt # Show last 50 lines tail -50 filename.txt # Watch a log file in real-time tail -f /var/log/syslog # -f means "follow" - shows new lines as they're added
Real-world scenario: Your application is having issues. You check the last 100 lines of the error log: tail -100 /var/log/app/error.log. If you need to watch it live as you test: tail -f /var/log/app/error.log.
🔍 Searching and Finding
find: Locating Files
The find command searches for files based on various criteria. It's one of the most powerful Linux commands.
# Find files by name (current directory and subdirectories) find . -name "*.txt" # Find files modified in last 7 days find /home -mtime -7 # Find files larger than 100MB find / -size +100M 2>/dev/null # 2>/dev/null hides "permission denied" errors # Find and execute command on results find . -name "*.tmp" -exec rm {} \; # Finds all .tmp files and deletes them # Find empty files find . -type f -empty
Real-world troubleshooting: Disk is full. You need to find large files: find / -type f -size +500M 2>/dev/null | head -20
grep: Searching Inside Files
While find searches for files, grep searches inside files for specific text patterns.
# Search for "error" in a file grep "error" logfile.txt # Case-insensitive search grep -i "error" logfile.txt # Search recursively in directory grep -r "TODO" /home/user/projects/ # Show line numbers grep -n "function" script.py # Count occurrences grep -c "warning" logfile.txt # Search for multiple patterns grep -e "error" -e "fail" logfile.txt
Name origin: grep comes from "Global Regular Expression Print" - it searches using patterns.
Real-world use: Checking if a configuration setting is enabled: grep "Listen" /etc/nginx/nginx.conf
📊 System Information Commands
df: Disk Free Space
The df command shows Disk Free space - how much space is used and available on your disks.
# Basic disk usage df # Human readable format (MB, GB) df -h # Show filesystem type df -T # Show specific filesystem df -h /home
Output explanation:
Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 35G 12G 75% /
Size = Total capacity
Used = Space used
Avail = Space available
Use% = Percentage used
Mounted on = Where it's accessible
Monitoring tip: Set up alerts when Use% reaches 80% or 90% to prevent disk-full emergencies.
du: Disk Usage by Directory
While df shows disk-level usage, du shows Disk Usage by directory - which folders are taking up space.
# Show size of current directory du -sh # -s = summary (total only) # -h = human readable # Show sizes of all subdirectories du -h --max-depth=1 # Sort by size (largest first) du -sh * | sort -rh # Find largest directories du -ah /home | sort -rh | head -20
Troubleshooting use: Server is running out of space. Run du -sh /* | sort -rh | head -10 to find which top-level directories are largest.
free: Memory Usage
The free command shows memory (RAM) usage statistics.
# Basic memory info free # Human readable free -h # Show in megabytes free -m # Continuous monitoring free -h -s 5 # Updates every 5 seconds
Understanding output:
total used free shared buff/cache available Mem: 7.7G 2.1G 1.2G 456M 4.4G 4.8G Swap: 2.0G 512M 1.5G
used = Memory used by applications
free = Completely unused memory
buff/cache = Memory used for disk caching (can be freed if needed)
available = Memory available for new applications
Key insight: Linux uses free memory for disk caching to speed things up. This is why "used" memory might seem high even when your system isn't busy.
🔧 Utility Commands
echo: Displaying Text
The echo command prints text to the screen. It's simple but used everywhere, especially in scripts.
# Print text echo "Hello World" # Print variable value echo $HOME # Create a file with content echo "server configuration" > config.txt # Append to file echo "new setting" >> config.txt # With escape characters echo -e "Line 1\nLine 2" # -e enables interpretation of \n as newline
Scripting use: echo is often used to show progress messages: echo "Starting backup process..."
which: Finding Command Locations
The which command shows you where a command is located on your system.
# Find where ls command is which ls # Output: /bin/ls # Find where python is installed which python3 # Check if a command exists which somecommand || echo "Command not found"
Why it matters: Sometimes you have multiple versions of a program installed. which tells you which one will run when you type the command.
man: Getting Help
The man command stands for Manual. It shows the official documentation for any command.
# View manual for ls man ls # Search manuals man -k "copy" # Shows all commands related to copying # Within man pages: # Spacebar = Next page # b = Previous page # /pattern = Search # q = Quit
Pro tip: If you forget man options, man man shows the manual for the manual command itself!
Alternative: Most commands also support --help flag: ls --help
🎯 Practice Exercises
Exercise 1: Create a Project Structure
Let's practice by creating a realistic project folder structure:
# 1. Go to your home directory cd ~ # 2. Create project directory mkdir my_website_project # 3. Go into it cd my_website_project # 4. Create subdirectories mkdir -p {html,css,js,images,backup} # 5. Create some files touch index.html about.html contact.html touch css/style.css js/main.js # 6. Check what you created ls -la find . -type f # 7. Create a backup cp -r css/ backup/ ls backup/
Exercise 2: Log File Analysis
Practice with a simulated log file:
# 1. Create a sample log file cat > app.log << EOF 2024-01-10 10:00:00 INFO: Application started 2024-01-10 10:05:00 WARNING: High memory usage detected 2024-01-10 10:10:00 ERROR: Database connection failed 2024-01-10 10:15:00 INFO: Retrying connection 2024-01-10 10:20:00 ERROR: Connection timeout 2024-01-10 10:25:00 INFO: Switching to backup server 2024-01-10 10:30:00 INFO: Connection established EOF # 2. View the log cat app.log # 3. Find only error lines grep "ERROR" app.log # 4. Count warning and error lines grep -c "WARNING\|ERROR" app.log # 5. Watch last 3 lines tail -3 app.log # 6. Add a new log entry echo "2024-01-10 10:35:00 INFO: Backup completed" >> app.log # 7. Check the new entry tail -2 app.log
Exercise 3: System Check Script
Create a simple system monitoring script:
# Create the script cat > system_check.sh << 'EOF' #!/bin/bash echo "=== System Health Check ===" echo "Date: $(date)" echo echo "1. Disk Usage:" df -h / echo echo "2. Memory Usage:" free -h echo echo "3. Top 5 largest directories in /home:" du -sh /home/* 2>/dev/null | sort -rh | head -5 echo echo "4. Running as user: $(whoami)" echo "5. System uptime: $(uptime -p)" EOF # Make it executable chmod +x system_check.sh # Run it ./system_check.sh
🚨 Common Mistakes and How to Avoid Them
Mistake 1: Using rm Carelessly
Wrong: rm -rf / home/user/documents (space after / deletes everything!)
Right: rm -rf /home/user/documents
Protection: Always double-check paths. Use ls first to see what you'll delete.
Mistake 2: Case Sensitivity
Wrong: cd Documents when folder is documents
Right: Match the exact case or use tab completion
Tip: Press Tab to auto-complete. It prevents typos.
Mistake 3: Forgetting -r for Directories
Wrong: cp directory newlocation (won't copy contents)
Right: cp -r directory newlocation
Remember: -r means recursive for directory operations with cp, rm, and chmod.
Mistake 4: Not Using man or --help
Wrong: Guessing command options
Right: man commandname or commandname --help
Pro tip: If you forget a command, use apropos: apropos "copy files"
📋 Daily Command Reference Cheat Sheet
| Task | Command | Example | When to Use |
|---|---|---|---|
| See location | pwd | pwd | When you're lost |
| List files | ls -la | ls -la ~ | First command in new directory |
| Change directory | cd | cd /var/log | Navigating folders |
| Create directory | mkdir -p | mkdir -p project/{src,test} | Setting up projects |
| Create file | touch | touch config.yaml | Creating empty files |
| View file | less | less largefile.log | Reading files (better than cat) |
| View start of file | head | head -20 file.txt | Checking file beginnings |
| View end of file | tail -f | tail -f app.log | Monitoring logs in real-time |
| Copy | cp -r | cp -r source/ dest/ | Copying directories |
| Move/Rename | mv | mv old.txt new.txt | Moving or renaming |
| Delete | rm -i | rm -i oldfile.txt | Safer deletion (asks first) |
| Find files | find | find . -name "*.log" | Searching for files |
| Search in files | grep | grep "error" *.log | Finding text in files |
| Disk space | df -h | df -h /home | Checking storage |
| Directory size | du -sh | du -sh * | Finding large folders |
| Memory | free -h | free -h | Checking RAM usage |
| Get help | man | man ls | Learning commands |
🚀 Next Steps in Your Command Line Journey
Week 1-2: Master the 10 essential commands (ls, cd, pwd, cat, less, cp, mv, rm, mkdir, grep)
Week 3-4: Learn file permissions (chmod, chown) and process management (ps, top)
Week 5-6: Practice with real projects - set up a web server, analyze logs
Week 7-8: Learn shell scripting basics to automate your tasks
Remember: You don't need to memorize everything. Focus on understanding what each command does. The muscle memory will come with practice.
🔗 Practice with Interactive Labs
The best way to learn Linux commands is through hands-on practice in a safe environment where you can experiment without fear of breaking anything.
👉 Master Linux commands through guided, interactive exercises at:
https://devops.trainwithsky.com/
Our platform offers:
Real terminal in your browser
Step-by-step tutorials
Practical exercises based on real-world scenarios
Instant feedback and hints
Progress tracking and achievements
Frequently Asked Questions
Q: How many commands do I really need to know?
A: About 20 commands will let you do 80% of common tasks. Start with the ones in this guide.
Q: I keep forgetting commands. Is that normal?
A: Absolutely! Even experts use man and Google. Focus on understanding concepts, not memorization.
Q: Should I learn all the command options?
A: No! Learn the common options first. You can always look up rare options when needed.
Q: How long until I'm comfortable with the command line?
A: With 30 minutes of daily practice, most people feel comfortable in 2-3 weeks.
Q: What if I make a mistake and delete something important?
A: That's why we recommend practicing in virtual machines or our safe online labs first!
Have questions about specific commands or scenarios? Ask in the comments below - we're here to help! 💬
Comments
Post a Comment