Top 15 Linux Commands Every User Must Know: A Complete Hands-On Guide
Whether you're a complete beginner stepping into the world of Linux or a seasoned developer looking to refresh your command-line skills, mastering essential Linux commands is crucial for efficient system administration and daily operations. This comprehensive guide will walk you through the 15 most important Linux commands that every user should know, complete with practical examples and real-world applications.
Why Linux Commands Matter
Linux commands form the backbone of system administration, development workflows, and server management. Unlike graphical interfaces, command-line operations offer precision, automation capabilities, and universal compatibility across different Linux distributions. Understanding these commands will dramatically improve your productivity and give you deeper control over your system.
1. ls - List Directory Contents
The ls command is arguably the most frequently used Linux command, allowing you to view files and directories in your current location.
Basic Usage
`bash
ls
`
This displays files and directories in the current directory in a simple list format.
Common Options
List with detailed information:
`bash
ls -l
`
Output example:
`
drwxr-xr-x 2 user user 4096 Nov 15 10:30 Documents
-rw-r--r-- 1 user user 1024 Nov 15 09:45 file.txt
`
Show hidden files:
`bash
ls -a
`
Combine options for detailed view with hidden files:
`bash
ls -la
`
Human-readable file sizes:
`bash
ls -lh
`
Sort by modification time:
`bash
ls -lt
`
Reverse sort order:
`bash
ls -lr
`
Practical Examples
List all .txt files:
`bash
ls *.txt
`
List files in a specific directory:
`bash
ls /home/user/Documents
`
List files recursively:
`bash
ls -R
`
2. cd - Change Directory
The cd command navigates between directories, making it essential for file system navigation.
Basic Usage
`bash
cd /path/to/directory
`
Navigation Shortcuts
Go to home directory:
`bash
cd ~
or simply
cd`Go to previous directory:
`bash
cd -
`
Go up one directory level:
`bash
cd ..
`
Go up two directory levels:
`bash
cd ../..
`
Practical Examples
Navigate to Documents folder:
`bash
cd ~/Documents
`
Navigate using absolute path:
`bash
cd /var/log
`
Navigate using relative path:
`bash
cd ./subfolder/another_folder
`
3. pwd - Print Working Directory
The pwd command displays your current directory location.
`bash
pwd
`
Output example:
`
/home/username/Documents/projects
`
This command is particularly useful in scripts or when you need to confirm your current location in the file system.
4. mkdir - Make Directory
Create new directories using the mkdir command.
Basic Usage
`bash
mkdir new_directory
`
Advanced Options
Create multiple directories:
`bash
mkdir dir1 dir2 dir3
`
Create nested directories:
`bash
mkdir -p parent/child/grandchild
`
Set permissions while creating:
`bash
mkdir -m 755 secure_directory
`
Practical Examples
Create a project structure:
`bash
mkdir -p project/{src,docs,tests,config}
`
This creates:
`
project/
├── src/
├── docs/
├── tests/
└── config/
`
5. rmdir and rm - Remove Directories and Files
rmdir - Remove Empty Directories
`bash
rmdir empty_directory
`
rm - Remove Files and Directories
Remove a file:
`bash
rm filename.txt
`
Remove multiple files:
`bash
rm file1.txt file2.txt file3.txt
`
Remove directory and contents recursively:
`bash
rm -r directory_name
`
Force removal without prompts:
`bash
rm -f filename.txt
`
Combine recursive and force:
`bash
rm -rf directory_name
`
Interactive removal (prompts before each deletion):
`bash
rm -i filename.txt
`
Safety Tips
Always double-check paths before using rm -rf. Consider using:
`bash
ls -la directory_name
`
before:
`bash
rm -rf directory_name
`
6. cp - Copy Files and Directories
The cp command copies files and directories from one location to another.
Basic Usage
Copy a file:
`bash
cp source.txt destination.txt
`
Copy to a directory:
`bash
cp file.txt /path/to/directory/
`
Advanced Options
Copy directories recursively:
`bash
cp -r source_directory destination_directory
`
Preserve file attributes:
`bash
cp -p file.txt backup_file.txt
`
Interactive mode (prompt before overwriting):
`bash
cp -i source.txt destination.txt
`
Verbose mode (show what's being copied):
`bash
cp -v file.txt backup/
`
Practical Examples
Backup configuration files:
`bash
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
`
Copy multiple files:
`bash
cp *.txt backup_folder/
`
7. mv - Move/Rename Files and Directories
The mv command both moves files to different locations and renames them.
Basic Usage
Rename a file:
`bash
mv old_name.txt new_name.txt
`
Move a file:
`bash
mv file.txt /path/to/destination/
`
Move and rename simultaneously:
`bash
mv old_file.txt /new/location/new_name.txt
`
Advanced Options
Interactive mode:
`bash
mv -i source.txt destination.txt
`
Verbose mode:
`bash
mv -v file.txt new_location/
`
Practical Examples
Organize files by type:
`bash
mv *.jpg images/
mv *.pdf documents/
`
8. grep - Search Text Patterns
The grep command searches for specific patterns within files or output streams.
Basic Usage
Search for a pattern in a file:
`bash
grep "pattern" filename.txt
`
Search in multiple files:
`bash
grep "pattern" file1.txt file2.txt
`
Advanced Options
Case-insensitive search:
`bash
grep -i "pattern" filename.txt
`
Show line numbers:
`bash
grep -n "pattern" filename.txt
`
Recursive search in directories:
`bash
grep -r "pattern" /path/to/directory/
`
Invert match (show lines that don't match):
`bash
grep -v "pattern" filename.txt
`
Count matching lines:
`bash
grep -c "pattern" filename.txt
`
Show context (lines before and after match):
`bash
grep -C 3 "pattern" filename.txt
`
Regular Expressions
Search for lines starting with pattern:
`bash
grep "^pattern" filename.txt
`
Search for lines ending with pattern:
`bash
grep "pattern$" filename.txt
`
Search for any single character:
`bash
grep "p.ttern" filename.txt
`
Practical Examples
Find all error messages in log files:
`bash
grep -i "error" /var/log/syslog
`
Search for IP addresses:
`bash
grep -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" access.log
`
Find all Python files containing a specific function:
`bash
grep -r "def function_name" --include="*.py" .
`
9. find - Locate Files and Directories
The find command searches for files and directories based on various criteria.
Basic Usage
Find files by name:
`bash
find /path/to/search -name "filename.txt"
`
Find files with wildcards:
`bash
find . -name "*.py"
`
Advanced Search Criteria
Find by file type:
`bash
find . -type f # files only
find . -type d # directories only
`
Find by size:
`bash
find . -size +100M # larger than 100MB
find . -size -1k # smaller than 1KB
`
Find by modification time:
`bash
find . -mtime -7 # modified in last 7 days
find . -mtime +30 # modified more than 30 days ago
`
Find by permissions:
`bash
find . -perm 755
`
Execute Commands on Found Files
Delete found files:
`bash
find . -name "*.tmp" -delete
`
Execute command on each found file:
`bash
find . -name "*.py" -exec chmod +x {} \;
`
Practical Examples
Find large files consuming disk space:
`bash
find /home -size +500M -type f
`
Find and remove old log files:
`bash
find /var/log -name "*.log" -mtime +30 -delete
`
10. chmod - Change File Permissions
The chmod command modifies file and directory permissions in Linux.
Understanding Permissions
Linux permissions consist of three groups: - Owner (u): The file owner - Group (g): Users in the file's group - Others (o): All other users
Each group has three permission types: - Read (r): Permission to read the file (4) - Write (w): Permission to modify the file (2) - Execute (x): Permission to execute the file (1)
Numeric Method
Common permission combinations:
`bash
chmod 755 file.txt # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 file.txt # rw-------
chmod 777 file.txt # rwxrwxrwx (not recommended)
`
Symbolic Method
Add permissions:
`bash
chmod +x script.sh # Add execute permission for all
chmod u+w file.txt # Add write permission for owner
chmod g+r file.txt # Add read permission for group
`
Remove permissions:
`bash
chmod -x script.sh # Remove execute permission for all
chmod u-w file.txt # Remove write permission for owner
`
Set specific permissions:
`bash
chmod u=rwx,g=rx,o=r file.txt
`
Recursive Changes
`bash
chmod -R 755 directory/
`
Practical Examples
Make a script executable:
`bash
chmod +x deploy.sh
`
Secure sensitive files:
`bash
chmod 600 ~/.ssh/id_rsa
`
Set web directory permissions:
`bash
chmod -R 644 /var/www/html/*.html
chmod -R 755 /var/www/html/
`
11. chown - Change File Ownership
The chown command changes file and directory ownership.
Basic Usage
Change owner:
`bash
chown newowner file.txt
`
Change owner and group:
`bash
chown newowner:newgroup file.txt
`
Change group only:
`bash
chown :newgroup file.txt
`
Recursive Changes
`bash
chown -R user:group directory/
`
Practical Examples
Change web files ownership:
`bash
chown -R www-data:www-data /var/www/html/
`
Fix home directory ownership:
`bash
chown -R username:username /home/username/
`
12. ps - Display Running Processes
The ps command shows information about running processes.
Basic Usage
Show processes for current user:
`bash
ps
`
Show all processes:
`bash
ps aux
`
Show processes in tree format:
`bash
ps auxf
`
Understanding ps Output
`
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 19356 1544 ? Ss 10:00 0:01 /sbin/init
`
- USER: Process owner - PID: Process ID - %CPU: CPU usage percentage - %MEM: Memory usage percentage - COMMAND: Command that started the process
Filtering Processes
Find specific processes:
`bash
ps aux | grep nginx
`
Show processes by user:
`bash
ps -u username
`
Practical Examples
Find memory-intensive processes:
`bash
ps aux --sort=-%mem | head
`
Find CPU-intensive processes:
`bash
ps aux --sort=-%cpu | head
`
13. kill - Terminate Processes
The kill command terminates processes using their Process ID (PID).
Basic Usage
Terminate a process:
`bash
kill PID
`
Force terminate a process:
`bash
kill -9 PID
`
Kill Signals
- SIGTERM (15): Graceful termination (default) - SIGKILL (9): Force termination - SIGHUP (1): Restart process
`bash
kill -15 PID # Graceful termination
kill -9 PID # Force kill
kill -1 PID # Restart
`
killall - Kill by Process Name
`bash
killall firefox
killall -9 unresponsive_app
`
Practical Examples
Kill all Python processes:
`bash
killall python3
`
Find and kill a specific service:
`bash
ps aux | grep nginx
kill $(pgrep nginx)
`
14. systemctl - Control System Services
The systemctl command manages systemd services on modern Linux distributions.
Basic Service Operations
Start a service:
`bash
systemctl start service_name
`
Stop a service:
`bash
systemctl stop service_name
`
Restart a service:
`bash
systemctl restart service_name
`
Reload service configuration:
`bash
systemctl reload service_name
`
Check service status:
`bash
systemctl status service_name
`
Enable/Disable Services
Enable service to start at boot:
`bash
systemctl enable service_name
`
Disable service from starting at boot:
`bash
systemctl disable service_name
`
Enable and start simultaneously:
`bash
systemctl enable --now service_name
`
System Information
List all services:
`bash
systemctl list-units --type=service
`
List failed services:
`bash
systemctl --failed
`
Show service dependencies:
`bash
systemctl list-dependencies service_name
`
Practical Examples
Manage web server:
`bash
systemctl status nginx
systemctl restart nginx
systemctl enable nginx
`
Check system boot time:
`bash
systemd-analyze
systemd-analyze blame
`
15. Additional Essential Commands
cat - Display File Contents
`bash
cat filename.txt
cat file1.txt file2.txt # Concatenate multiple files
`
head and tail - View File Beginnings and Endings
`bash
head -n 10 filename.txt # First 10 lines
tail -n 10 filename.txt # Last 10 lines
tail -f /var/log/syslog # Follow file changes in real-time
`
less and more - Page Through Files
`bash
less filename.txt # Navigate with arrow keys, q to quit
more filename.txt # Space to continue, q to quit
`
Command Combinations and Pipes
Linux commands become powerful when combined using pipes (|):
Practical Pipe Examples
Find largest files:
`bash
ls -la | sort -k5 -n | tail
`
Count unique IP addresses in log:
`bash
grep -o '\b[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\b' access.log | sort | uniq -c | sort -nr
`
Monitor system processes:
`bash
ps aux | grep -v grep | sort -k3 -nr | head -10
`
Find files and search within them:
`bash
find . -name "*.py" | xargs grep "function_name"
`
Best Practices and Tips
1. Use Tab Completion
Press Tab to auto-complete file names, commands, and paths.2. Command History
- Use↑ and ↓ arrows to navigate command history
- Use history command to view recent commands
- Use !number to re-execute a specific command from history3. Command Aliases
Create shortcuts for frequently used commands:`bash
alias ll='ls -la'
alias grep='grep --color=auto'
`4. Safety First
- Always usels before rm
- Use -i flag for interactive mode when deleting files
- Make backups before making system changes5. Man Pages
Useman command_name to read detailed documentation:
`bash
man ls
man grep
man chmod
`Conclusion
Mastering these 15 essential Linux commands will significantly improve your efficiency and confidence when working with Linux systems. From basic file operations with ls, cd, and cp to advanced process management with ps, kill, and systemctl, these commands form the foundation of Linux system administration.
Remember that learning Linux commands is a gradual process. Start with the basics like navigation and file operations, then progressively incorporate more advanced commands like grep, find, and systemctl into your workflow. Practice regularly, experiment safely, and don't hesitate to consult man pages for detailed information about command options and usage.
The command line might seem intimidating at first, but with consistent practice and application of these commands, you'll soon discover the power, flexibility, and efficiency that makes Linux the preferred choice for developers, system administrators, and power users worldwide.
Whether you're managing servers, developing applications, or simply exploring Linux as a desktop operating system, these commands will serve as your reliable toolkit for navigating and controlling your Linux environment effectively.