In the daily operations of Linux servers, the command line is the most direct and efficient tool. For beginners, mastering a few core basic commands allows you to quickly get started with file management, directory navigation, and server status checking. The following 5 commands are simple and practical; follow the steps to take your first step in server management!
1. ls: Quickly view files/folders in the directory¶
Purpose: Lists all files and folders in the current directory, similar to clicking on a folder in a file manager to view its contents.
Basic Usage:
- Simple usage: Just type ls, which will display files and folders in the current directory (hidden files are not shown).
- ls -l: Shows detailed information, including file permissions, size, modification time, etc. (similar to Windows’ “Details” view).
- ls -a: Shows all files, including hidden files (files/folders starting with ., e.g., the configuration file .bashrc).
- ls target_path: View contents of a specified path (e.g., ls /home to view the home folder under the root directory).
Example:
Typing ls -l in the terminal might show output like:
total 24
drwxr-xr-x 2 root root 4096 10-01 14:30 Documents # Directory (starts with 'd'), permissions, owner, modification time, name
-rw-r--r-- 1 root root 123 10-02 09:15 test.txt # File (starts with '-')
2. cd: Switch to the target directory (“Navigation” command)¶
Purpose: Like clicking on a folder in a file manager, it switches to the directory you want to work in.
Basic Usage:
- cd directory_name: Enter a specified subdirectory (e.g., cd Documents to enter the Documents folder).
- cd ..: Return to the parent directory (from a subdirectory to the parent directory).
- cd ~: Enter the current user’s “home directory” (each user’s personal directory, e.g., /home/your_username).
- cd /: Enter the root directory (the topmost directory of the Linux system, the “home” of all files).
Example:
If you are currently in the root directory and want to enter the user folder under home, the command is:
cd home/user
3. pwd: Show current directory (“Location” command)¶
Purpose: Always let you know which directory you are in to avoid operation errors (e.g., accidentally deleting files in the root directory).
Basic Usage:
Simply type pwd, and the system will display the full path of the current directory.
Example:
Typing pwd in the terminal might output:
/home/user/documents
This indicates you are currently in the /home/user/documents directory.
4. mkdir: Create a new directory (“Create Folder” command)¶
Purpose: Create a new folder in the current directory to organize files effectively.
Basic Usage:
- mkdir directory_name: Create a single directory (e.g., mkdir backup to create a backup folder).
- mkdir -p parent_dir/sub_dir: Create multi-level directories (automatically creates parent directories if they don’t exist).
Example:
- To create a folder named logs in the current directory:
mkdir logs
- To create the
2023/10folder under thedatadirectory (wheredatamay not exist):
mkdir -p data/2023/10
5. df -h: Check server disk space¶
Purpose: Check how much free space the server’s hard drive has left to avoid program failures due to full disks.
Basic Usage:
Type df -h directly; the -h parameter converts disk sizes to human-readable units (e.g., 20G, 500M).
Example:
After typing df -h, the output might look like:
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 15G 4.5G 78% / # Root partition, 78% used
tmpfs 2.0G 0 2.0G 0% /dev/shm # Temporary memory partition, available
/dev/vdb1 50G 10G 35G 20% /data # Data partition, 20% used
- The
Availcolumn shows available space, andUse%shows usage percentage. If a partition’s usage exceeds 80%, consider cleaning up or expanding it.
Quick Summary¶
These 5 commands are the “foundation” of Linux server management: ls helps you view files, cd helps you navigate, pwd helps you locate your position, mkdir helps you create folders, and df -h helps you check disk space. Practice with different parameters (e.g., ls -a to see hidden files, mkdir -p to create multi-level directories) and familiarize yourself with them before learning more complex commands. Remember: the core of Linux commands is “parameters + target”; master basic combinations first, then explore more possibilities!