Topic Overview
Linux Basic Commands
Learn essential Linux commands: file operations, process management, networking, and system info.
Linux Basic Commands
Why This Matters
Linux commands are the interface to the operating system. They let you interact with processes, files, networks, and system resources. Understanding Linux commands helps you understand how the OS works—when you run ps, you're seeing process management. When you run df, you're seeing file system management. Commands are practical tools that demonstrate OS concepts.
This matters because Linux commands are essential for system administration, debugging, and understanding system behavior. When a service is slow, you use top to see CPU usage. When disk is full, you use df to check space. When processes hang, you use ps and kill to manage them. Understanding commands helps you understand the OS.
In interviews, when someone asks "How would you debug a slow system?", they're testing whether you know Linux commands. Do you know how to check CPU usage? Do you know how to find processes? Most engineers don't. They just use GUIs and wonder why things are slow.
What Engineers Usually Get Wrong
Most engineers think "Linux commands are just tools." But commands are interfaces to OS concepts. When you run ps, you're querying the process management system. When you run netstat, you're querying the networking stack. Understanding this helps you understand how the OS works.
Engineers also don't understand that commands use system calls. Every command (like ls, cat, grep) makes system calls to the OS. Understanding this helps you understand why some commands are slow (many system calls) and how to optimize them.
How This Breaks Systems in the Real World
A service was slow. The team didn't know how to diagnose it. They restarted the service, but it was still slow. The fix? Use top to check CPU usage, iostat to check I/O, netstat to check network connections. Found that the service was I/O-bound (high disk I/O). Understanding Linux commands helps you diagnose system issues.
Another story: A service was creating many files but not deleting them. Disk space was exhausted. The team didn't know how to find large files. The fix? Use du -sh * to find large directories, find to locate old files, rm to delete them. Understanding Linux commands helps you manage system resources.
Essential Commands by Category
File Operations
ls: List directory contents
ls -l # Long format (permissions, size, date)
ls -a # Show hidden files
ls -lh # Human-readable sizes
cd: Change directory
cd /path # Absolute path
cd .. # Parent directory
cd ~ # Home directory
cp/mv/rm: Copy, move, remove
cp file1 file2
mv file1 dir/
rm file # Remove file
rm -r dir # Remove directory recursively
find: Search for files
find /path -name "*.log"
find /path -size +100M # Files larger than 100MB
find /path -mtime -7 # Modified in last 7 days
Process Management
ps: List processes
ps aux # All processes, detailed
ps -ef # All processes, full format
ps aux | grep nginx
top/htop: Monitor processes
top # Real-time process monitor
htop # Interactive process monitor (better)
kill: Terminate processes
kill <pid> # Send SIGTERM (graceful)
kill -9 <pid> # Send SIGKILL (force)
killall nginx # Kill all nginx processes
System Information
df: Disk space usage
df -h # Human-readable, all file systems
df -i # Inode usage
du: Directory space usage
du -sh * # Summary of each directory
du -h --max-depth=1
free: Memory usage
free -h # Human-readable memory info
uname: System information
uname -a # All system information
Networking
netstat/ss: Network connections
netstat -tulpn # TCP/UDP listening ports
ss -tulpn # Modern replacement for netstat
ifconfig/ip: Network interface configuration
ifconfig # Network interfaces
ip addr # Modern replacement
ping: Test connectivity
ping google.com
Examples
Example 1: Finding Large Files
# Find files larger than 100MB
find /var/log -size +100M
# Find largest directories
du -h --max-depth=1 / | sort -h
# Find and delete old log files
find /var/log -name "*.log" -mtime +30 -delete
Example 2: Monitoring System Resources
# Check disk space
df -h
# Check memory usage
free -h
# Check CPU usage
top
# Or: htop (better interface)
# Check I/O
iostat -x 1
Example 3: Process Management
# Find process by name
ps aux | grep nginx
# Kill process gracefully
kill <pid>
# Force kill if needed
kill -9 <pid>
# Monitor process
top -p <pid>
Common Pitfalls
Pitfall 1: Using rm -rf without caution
- Problem:
rm -rfdeletes recursively, no undo - Solution: Double-check paths, use
rm -rifor interactive deletion - Example:
rm -rf /deletes entire system (never do this!)
Pitfall 2: Not understanding file permissions
- Problem: Can't access files due to permission errors
- Solution: Understand permissions (chmod, chown), check with
ls -l - Example: Script can't execute because missing execute permission
Pitfall 3: Not monitoring system resources
- Problem: System runs out of resources without warning
- Solution: Regularly check disk space (df), memory (free), processes (top)
- Example: Disk fills up, service fails, no warning
Pitfall 4: Using outdated commands
- Problem: Some commands deprecated (netstat, ifconfig)
- Solution: Use modern replacements (ss, ip)
- Example: netstat may not be available on newer systems
Pitfall 5: Not understanding command options
- Problem: Commands behave unexpectedly due to wrong options
- Solution: Read man pages (
man command), understand common options - Example:
rm -fforces deletion without confirmation (dangerous)
Interview Questions
Beginner
Q: How would you find which process is using the most CPU?
A: Use top or htop to see real-time CPU usage. The processes are sorted by CPU usage by default. You can also use ps aux --sort=-%cpu | head -10 to see the top 10 CPU-consuming processes. htop provides a better interactive interface where you can sort by different columns and see more details.
Intermediate
Q: How would you diagnose why a Linux system is running slowly?
A: I would check system resources systematically:
- CPU usage:
toporhtopto see CPU-bound processes - Memory usage:
free -hto check if system is swapping - Disk I/O:
iostat -x 1to check disk utilization - Disk space:
df -hto check if disk is full - Network:
netstat -iorss -ito check network interface errors - Process count:
ps aux | wc -lto check if too many processes - Load average:
uptimeto see system load
Based on findings:
- High CPU: Identify CPU-bound processes, consider optimization
- High memory/swapping: Check for memory leaks, add RAM
- High disk I/O: Check for I/O-bound processes, optimize I/O patterns
- Full disk: Find and clean up large files (
du,find)
Senior
Q: How would you design a monitoring and alerting system for a Linux server using command-line tools?
A: I would create a comprehensive monitoring system:
-
Resource monitoring scripts:
- CPU:
top -bn1 | grep "Cpu(s)" - Memory:
free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}' - Disk:
df -h | awk '$5 > 80 {print}' - Load:
uptime | awk -F'load average:' '{print $2}'
- CPU:
-
Process monitoring:
- Track specific processes:
ps aux | grep <process> - Monitor process count:
ps aux | wc -l - Check for zombie processes:
ps aux | grep defunct
- Track specific processes:
-
Network monitoring:
- Connection count:
ss -s - Bandwidth:
iftopornethogs - Port monitoring:
ss -tulpn | grep <port>
- Connection count:
-
Log monitoring:
- Check system logs:
journalctlor/var/log/ - Monitor application logs:
tail -f /var/log/app.log - Error detection:
grep -i error /var/log/*.log
- Check system logs:
-
Automation:
- Use cron for periodic checks
- Script alerts when thresholds exceeded
- Send notifications (email, Slack, PagerDuty)
-
Historical tracking:
- Log metrics over time
- Use tools like
sarfor historical data - Track trends and patterns
-
Alerting strategy:
- Set appropriate thresholds (CPU > 80%, disk > 90%)
- Avoid alert fatigue (rate limiting, grouping)
- Escalate critical alerts
This system provides comprehensive monitoring using standard Linux commands.
-
File operations: ls, cd, cp, mv, rm, mkdir, rmdir for managing files and directories
-
Process management: ps, kill, top, htop for viewing and controlling processes
-
Networking: netstat, ss, ifconfig, ping for network monitoring and configuration
-
System info: df, du, free, uname for checking system resources and information
-
Permissions: chmod, chown for managing file permissions and ownership
-
Best practices: Use appropriate commands for tasks, understand permissions, monitor system resources
-
Process vs Thread - Understanding processes that are managed using Linux commands (ps, kill, top)
-
System Calls - How Linux commands use system calls to interact with the OS
-
File Systems (EXT4, NTFS, FAT32) - How file operations commands interact with file systems
-
Shell vs Kernel - Understanding the shell interface where Linux commands are executed
-
I/O Management - How I/O monitoring commands (iotop) show I/O management
Key Takeaways
File operations: ls, cd, cp, mv, rm, mkdir, rmdir for managing files and directories
Process management: ps, kill, top, htop for viewing and controlling processes
Networking: netstat, ss, ifconfig, ping for network monitoring and configuration
System info: df, du, free, uname for checking system resources and information
Permissions: chmod, chown for managing file permissions and ownership
Best practices: Use appropriate commands for tasks, understand permissions, monitor system resources
Related Topics
Process vs Thread
Understanding processes that are managed using Linux commands (ps, kill, top)
System Calls
How Linux commands use system calls to interact with the OS
File Systems (EXT4, NTFS, FAT32)
How file operations commands interact with file systems
Shell vs Kernel
Understanding the shell interface where Linux commands are executed
I/O Management
How I/O monitoring commands (iotop) show I/O management
What's next?