Linux Command Basics and Common Tools Quick Reference¶
1. Command Basic Format and General Tips¶
In Linux, almost all operations are performed via commands. The basic command format is: command [options] [arguments].
- Options: Start with -, e.g., -l in ls -l means “long format display”; multiple options can be combined (e.g., ls -la).
- Arguments: Usually target files or directories, e.g., ls document.txt.
Essential Tips for Beginners:
- Get Help: Stuck? Use command --help (brief help) or man command (detailed manual, press q to exit).
- Quick Completion: Type the first few letters of a command/filename, then press Tab to auto-complete (press Tab repeatedly to cycle through candidates).
- Interrupt Command: If a command freezes, press Ctrl+C to terminate it.
- Clear Screen: Press Ctrl+L to clear the terminal (similar to Windows “cls”).
- History Navigation: Use ↑/↓ to scroll through previous commands; Ctrl+R to search history.
2. File & Directory Operations (Core Fundamentals)¶
-
List Directory Contents:
ls
- Basic:ls(shows files/folders in the current directory).
- Common Options:-l: Detailed list (shows permissions, size, modification time, etc.).-a: Show hidden files (starting with.).-h: Display sizes in human-readable units (KB/MB/GB).- Example:
ls -lh(shows file sizes with hidden files).
-
Change Directory:
cd
- Basic:cd target_directory.
- Common Paths:.: Current directory (e.g.,cd .does nothing)...: Parent directory (e.g.,cd ..goes up one level).~: Home directory (e.g.,cd ~returns to user’s home).- Absolute Path: Starts with
/(e.g.,cd /home/user/documents). - Relative Path: Doesn’t start with
/(e.g.,cd ../images).
-
Create Files/Directories:
-touch filename: Create an empty file (e.g.,touch test.txt).
-mkdir directoryname: Create a new directory (e.g.,mkdir myfolder). -
Copy/Move/Delete:
-cp source_file target_location: Copy files (e.g.,cp test.txt ./backup/).
-mv source_file target_location: Move or rename (e.g.,mv old.txt new.txt).
- Dangerous Warning:rm filename: Delete files (use with caution!). Add-ifor confirmation (e.g.,rm -i test.txt), or-rto delete directories (e.g.,rm -r myfolder).
3. System Information & Management (Server Daily Use)¶
-
System Status:
-uname -a: Show kernel version, hostname, etc. (e.g.,Linux server 5.4.0-xxx-generic #1 SMP ...).
-uptime: Show system uptime and load average (e.g.,12:34:56 up 10 days, 2 users, load average: 0.12, 0.15, 0.10). -
Disk & Memory:
-df -h: Check disk space usage (h= human-readable units, e.g.,/dev/sda1 20G 15G 5G 75%).
-free -h: Check memory usage (e.g.,Mem: 15G 10G 5G= total 15G, used 10G, free 5G). -
Process Management:
-ps aux: View all processes (a= all users,u= detailed info,x= background processes).
-top: Dynamically show process resource usage (pressqto exit;M= sort by memory,P= sort by CPU).
4. Text Processing Tools (Efficient Filtering & Viewing)¶
-
View File Content:
-cat filename: Display entire file (good for small files).
-head -n 5 filename: Show first 5 lines (n= number of lines, e.g.,head -n 10 log.txt).
-tail -n 5 filename: Show last 5 lines (common for logs, e.g.,tail -f log.txtto tail log in real-time). -
Search Text:
-grep "keyword" filename: Search for “keyword” in a file (e.g.,grep "error" app.log).
-grep -r "keyword" /path: Recursively search all files in a directory (e.g.,grep -r "config" /etc/).
5. Package Management (Install/Update Software)¶
Package commands vary by Linux distribution; examples for common systems:
- Ubuntu/Debian (Deb Packages):
- sudo apt update: Update package source list.
- sudo apt install package-name: Install software (e.g., sudo apt install vim).
- sudo apt remove package-name: Uninstall software.
- CentOS/RHEL (RPM Packages):
sudo yum update: Update system (yum is legacy; usednffor newer versions).sudo yum install package-name: Install software (e.g.,yum install wget).
6. Beginner Pitfalls to Avoid¶
-
Permission Issues:
- Regular users needsudofor root-level commands (e.g.,sudo apt update).
- Directory/file permissions: Check withls -l; modify withchmod(e.g.,chmod 755 script.shto make it executable for all). -
Dangerous Commands:
- Never runrm -rf *in a non-empty directory (deletes all files).
- Verify target paths before moving/deleting (e.g.,mv file.txt ../instead ofmv file.txt /). -
Quick Recovery:
- Accidentally deleted a file? If not overwritten, useextundelete(requires prior installation) to restore.
7. Summary¶
Linux commands may seem complex, but they become intuitive with practice. Start with basics like ls, cd, mkdir, then practice cp, mv, rm for file operations. Use grep/cat for text processing. When stuck, use --help or man first, then practice (e.g., create test directories, simulate file operations).
Pro Tip: Keep a cheat sheet of common commands, and practice 3-5 new commands daily. Within a week, you’ll master 80% of basic scenarios!