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 asll)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 directorycd -: Return to the last directory you enteredcd /: 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/yournameafter execution
4. mkdir - Create New Directory¶
- Purpose: Create a new folder
- Syntax:
mkdir directory_name - Example:
mkdir test(create a folder namedtestin 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 filenote.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: Copyfile.txtand rename it tonew_file.txtcp -r folder/ new_folder/: Recursively copy directories (-rmeans 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/tmpdirectorymv 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 (-rmeans 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 ofnote.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 oflog.txttail -5 log.txt: Display the last 5 lines oflog.txttail -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 (
moreonly scrolls down,lesssupports up and down) - Syntax:
more file_nameorless file_name - Operations: Press
Spaceto turn pages,qto exit,/keywordto search (lesssupports 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/homedirectory 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 thetestdirectory (-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” (pressCtrl+Cto 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
1after 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)
IV. Network-Related Commands¶
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) orip addr(newer version, recommended) - Example:
ip addr(display IP information for all network cards, e.g.,eth0orens33)
3. curl/wget - Download Files¶
- Purpose: Download files via URL (suitable for command-line use)
- Syntax:
curl -O URL(-Opreserves the filename) orwget URL - Examples:
curl -O https://example.com/file.zip: Download a file and preserve its original namewget 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, usesudo)sudo apt install software_name: Install software (e.g.,sudo apt install vim)sudo apt remove software_name: Uninstall softwaresudo 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 systemsudo 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 passwordsudo userdel -r username: Delete a user and remove their home directory
VII. Learning Suggestions¶
- Practice More: The fastest way to remember commands is through actual operation, such as creating directories, copying files, and deleting test folders.
- Use Help Effectively:
- Add--helpto any command:ls --help(view option descriptions)
- Complete manual pages:man command(e.g.,man ls, pressqto exit) - Memorize with Scenarios: For example, “Delete files with
rm” or “Recursively copy directories withcp -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!