Introduction

Although Linux is powerful, it’s common for beginners to forget so many commands. This cheat sheet compiles the most core and commonly used commands in daily Linux usage, categorized by function, making it easy for you to quickly look up and learn. It is recommended to save this cheat sheet and practice several times to form muscle memory!

I. File and Directory Operations (The Most Basic!)

Files and directories are the core of the Linux system. Mastering these commands will allow you to “navigate” and “manage” files in the system.

1. ls - List Directory Contents

  • Purpose: Display files and subdirectories in the current directory
  • Syntax: ls [options] [directory/file]
  • Common Examples:
  • ls: Show files/folders in the current directory (excludes hidden files)
  • ls -l: Detailed information (permissions, size, modification time, etc., often abbreviated as ll)
  • ls -a: Show all files (including hidden files starting with ., e.g., .bashrc)
  • ls -h: Display file sizes in “human-readable” format (e.g., 1.2K, 300M)
  • ls -lha: Comprehensive display (detailed + hidden + size)

2. cd - Change Directory

  • Purpose: Enter a specified directory
  • Syntax: cd target_directory_path
  • Common Examples:
  • cd ~: Return to the “home directory” (your personal folder, usually at /home/username)
  • cd ..: Go up one level to the parent directory
  • cd -: Return to the last directory you entered
  • cd /: Enter the root directory (the topmost level of the system, use with caution!)

3. pwd - Show Current Directory

  • Purpose: Print the full path of the “current working directory”
  • Syntax: pwd (no parameters)
  • Example: Outputs a path like /home/yourname after execution

4. mkdir - Create New Directory

  • Purpose: Create a new folder
  • Syntax: mkdir directory_name
  • Example: mkdir test (create a folder named test in the current directory)
  • ⚠️ Note: Cannot create an existing directory. To create nested directories, use mkdir -p test/subdir

5. touch - Create Empty File

  • Purpose: Create an empty file (if the file exists, it updates the modification time)
  • Syntax: touch file_name
  • Example: touch note.txt (create an empty text file note.txt)

6. cp - Copy Files/Directories

  • Purpose: Copy files or directories to a target location
  • Syntax: cp [options] source_file/directory target_location
  • Common Examples:
  • cp file.txt new_file.txt: Copy file.txt and rename it to new_file.txt
  • cp -r folder/ new_folder/: Recursively copy directories (-r means recursive, copies subdirectories and files)

7. mv - Move/Rename Files

  • Purpose: Move files/directories or directly rename them
  • Syntax: mv source_file/directory target_location/new_file_name
  • Common Examples:
  • mv old.txt new.txt: Rename a file (within the current directory)
  • mv file.txt /tmp/: Move a file to the /tmp directory
  • mv folder/ /home/back/: Move a directory to a specified location

8. rm - Delete Files/Directories (Dangerous!)

  • Purpose: Delete files or directories
  • Syntax: rm [options] file/directory
  • ⚠️ Warning: Deleted files cannot be recovered! Beginners must confirm the path first!
  • Common Examples:
  • rm file.txt: Delete a single file (requires confirmation)
  • rm -f file.txt: Force delete (no confirmation prompt)
  • rm -r folder/: Recursively delete directories (-r means delete subdirectories and files)
  • rm -rf *: ⚠️ Delete all files in the current directory (extremely dangerous! Only for testing!)

II. System Information and File Viewing

These commands are very useful when you need to understand system status or view file content!

1. cat - View File Content

  • Purpose: Quickly view the content of text files (suitable for small files)
  • Syntax: cat file_name
  • Example: cat note.txt (display the entire content of note.txt)

2. head/tail - View File Head/Tail

  • Purpose: Quickly view the first few lines/last few lines of a file (suitable for large files to avoid lag)
  • Syntax: head -n number_of_lines file_name, tail -n number_of_lines file_name
  • Examples:
  • head -3 log.txt: Display the first 3 lines of log.txt
  • tail -5 log.txt: Display the last 5 lines of log.txt
  • tail -f log.txt: Real-time tracking of file updates (often used for log monitoring)

3. more/less - Page-by-Page File Viewing

  • Purpose: Display file content page by page, supporting page turning (more only scrolls down, less supports up and down)
  • Syntax: more file_name or less file_name
  • Operations: Press Space to turn pages, q to exit, /keyword to search (less supports this)

4. df - Check Disk Space

  • Purpose: Show total capacity, used space, and free space of disk partitions
  • Syntax: df [options]
  • Common Examples:
  • df -h: Display in “human-readable” format (e.g., G, M)
  • df -h /home: Only check disk space for the partition where the /home directory resides

5. du - Check Directory Size

  • Purpose: Show the size of a specified directory or file
  • Syntax: du [options] directory/file
  • Common Examples:
  • du -sh test/: View the total size of the test directory (-s=summary, -h=human-readable)
  • du -h --max-depth=1: View the sizes of all subdirectories in the current directory (depth level 1)

III. Process Management (Managing “Background Programs” in the System)

These commands help when programs freeze or need to be closed!

1. ps - Check Process Status

  • Purpose: List process information of the current system
  • Syntax: ps [options]
  • Common Examples:
  • ps: View processes in the current terminal (simple list)
  • ps -ef: Display all processes (-e=all, -f=full format, including PID, user, command)
  • ps aux | grep "nginx": Search for processes containing “nginx” (press Ctrl+C to exit the pipe)

2. top - Dynamic Process Monitoring (Real-time Updates)

  • Purpose: Dynamically display system processes and CPU/memory usage (similar to Windows Task Manager)
  • Operations:
  • Press 1 after entering: Show CPU core usage
  • Press P: Sort by CPU usage
  • Press M: Sort by memory usage
  • Press q: Exit

3. kill - Terminate Processes

  • Purpose: Terminate a specified process (identified by PID)
  • Syntax: kill [signal] PID
  • Common Signals:
  • kill 1234: Terminate a process (Signal 9 = forceful termination, kill -9 1234)
  • kill -9 PID: Forcefully terminate (used when a process is stuck)

Essential for troubleshooting network issues and connecting to servers!

1. ping - Test Network Connectivity

  • Purpose: Send packets to a target IP/domain to check if the network is reachable
  • Syntax: ping target_IP/domain
  • Example: ping www.baidu.com (check if the network to Baidu is normal)

2. ifconfig/ip - View/Configure Network

  • Purpose: View network interface information (IP address, MAC address, etc.)
  • Syntax: ifconfig (legacy) or ip addr (newer version, recommended)
  • Example: ip addr (display IP information for all network cards, e.g., eth0 or ens33)

3. curl/wget - Download Files

  • Purpose: Download files via URL (suitable for command-line use)
  • Syntax: curl -O URL (-O preserves the filename) or wget URL
  • Examples:
  • curl -O https://example.com/file.zip: Download a file and preserve its original name
  • wget https://example.com/install.sh: Directly download a script

V. Package Management (Essential for Beginners: Installing Software)

The software installation methods vary slightly by Linux distribution. Here, we use the common apt (Ubuntu/Debian) and yum (CentOS/RHEL) as examples.

1. apt (Debian/Ubuntu)

  • Purpose: Install, update, and uninstall software packages
  • Common Commands:
  • sudo apt update: Update the software package list (requires admin privileges, use sudo)
  • sudo apt install software_name: Install software (e.g., sudo apt install vim)
  • sudo apt remove software_name: Uninstall software
  • sudo apt upgrade: Update all system software

2. yum (CentOS/RHEL)

  • Purpose: Similar to apt, but manages software via yum repositories
  • Common Commands:
  • sudo yum update: Update the system
  • sudo yum install software_name: Install software (e.g., sudo yum install wget)
  • sudo yum remove software_name: Uninstall software

VI. User and Permission Management

These commands are important when adding users or modifying permissions!

1. sudo - Temporarily Gain Admin Privileges

  • Purpose: Execute commands with administrator privileges (used when regular user permissions are insufficient)
  • Syntax: sudo command
  • Example: sudo apt install vim (installing software requires admin rights)

2. useradd/userdel - Create/Delete Users

  • Purpose: Add or remove system users
  • Syntax:
  • sudo useradd username: Create a user (password needs to be set manually)
  • sudo passwd username: Set a user password
  • sudo userdel -r username: Delete a user and remove their home directory

VII. Learning Suggestions

  1. Practice More: The fastest way to remember commands is through actual operation, such as creating directories, copying files, and deleting test folders.
  2. Use Help Effectively:
    - Add --help to any command: ls --help (view option descriptions)
    - Complete manual pages: man command (e.g., man ls, press q to exit)
  3. Memorize with Scenarios: For example, “Delete files with rm” or “Recursively copy directories with cp -r”. The more specific the scenario, the easier it is to remember.

VIII. Conclusion

This cheat sheet covers the core commands for daily Linux operations, but the Linux world is far more extensive! Experiment more in practice, and you’ll become more proficient. When facing problems, make good use of man or online searches to quickly find answers. Enjoy exploring the Linux world!

Xiaoye