15 Essential Linux Commands for Programmers: A Guide to Efficiency Tools

From “Command Novice” to “Terminal Expert”: Unlocking the Efficiency Secrets of Linux in Just 3 Minutes a Day

In the server room at 2 AM, operations engineer Xiao Lin stares at the scrolling logs on the screen, his fingers flying over the keyboard. While his colleagues struggle to find files in the graphical interface, he has already pinpointed the issue with a single line: grep "ERROR" /var/log/nginx/access.log | wc -l — this is the power of the Linux command line. Whether for development debugging, system management, or automation scripts, the command line is a programmer’s “Swiss Army knife.” Today, we have selected 15 of the most practical Linux commands, organized into three main modules: daily operations, system management, and file handling, guiding you from “copy-paste commands” to truly understanding the underlying logic.

📂 Daily Operations: 5 Commands to Build a Basic Operation System

📋 ls: The “Magnifying Glass” for Directory Contents

Command Format: ls [options] [directory]

Core Parameters:

  • -l: Display detailed file information in long format (permissions, owner, size, modification time)

  • -a: Show all files, including hidden files that start with a dot

  • -h: Human-readable file sizes (automatically converts to KB/MB/GB)

Practical Scenarios:

  • View detailed information of all files (including hidden files) in the current directory

    ls -la

  • Display file sizes in a readable format in the /usr/local directory, sorted by modification time

    ls -lht /usr/local

✅ Example Output:

total 24K drwxr-xr-x 2 root root 4.0K Oct 15 09:30 bin drwxr-xr-x 3 root root 4.0K Sep 28 14:15 etc drwxr-xr-x 2 root root 4.0K Sep 28 14:15 games

💡 Tip: The first column of the output from ls -l shows the permission string (e.g., drwxr-xr-x), where the first character represents the file type (d=directory, -=regular file, l=link), and the next nine characters are grouped into three sets representing the read, write, and execute permissions for the owner, group, and others.

🧭 cd and pwd: The “Compass” for Directory Navigation

cd Command Format: cd [directory path]pwd Command Format: pwd

Core Usage of cd:

  • cd ..: Go back to the parent directory

  • cd ~ or cd: Switch to the current user’s home directory

  • cd -: Switch to the last working directory

Practical Scenarios:

  • Switch from the current directory to the /tmp directory

    cd /tmp

  • Quickly return to the home directory

    cd ~

  • Display the absolute path of the current directory

    pwd

✅ Example Output:

/home/developer

⚠️ Warning: When using the cd command, if the path contains spaces, it must be enclosed in quotes, such as cd "/home/user/my documents", or use a backslash to escape, like cd /home/user/my\ documents.

📁 mkdir and rm: The “Create and Delete” of Directory Management

mkdir Command Format: mkdir [options] directory_namerm Command Format: rm [options] file or directory

Core Parameters of mkdir:

  • -p: Recursively create multi-level directories

Core Parameters of rm:

  • -r: Recursively delete directories and their contents

  • -f: Force delete without prompting for confirmation

  • -i: Prompt for confirmation before deletion (enabled by default, can prevent accidental operations)

Practical Scenarios:

  • Recursively create a project directory structure

    mkdir -p /project/{src,docs,tests}

  • Safely delete the tmp folder in the current directory (will confirm one by one)

    rm -ri tmp/

  • Force delete expired logs in the system temporary directory (use with caution)

    rm -rf /var/log/old_logs/

⚠️ Danger Warning: rm -rf / is one of the most dangerous commands in Linux, as it will recursively delete all files under the root directory and cannot be recovered. In production environments, it is recommended to use rm -i or configure an alias alias rm='rm -i' for added protection.

📊 System Management: 5 Commands to Master System Operation Status

🔄 top and ps: The “Dashboard” for Process Monitoring

top Command Format: top [options]ps Command Format: ps [options]

Core Parameters of top:

  • -d: Specify the refresh interval in seconds (e.g., top -d 2 refreshes every 2 seconds)

  • -p: Monitor the process with the specified PID (e.g., top -p 1234)

  • -u: Only display processes of the specified user (e.g., top -u root)

Core Parameters of ps:

  • a: Display processes of all users

  • u: Display process information centered around the user

  • x: Display processes without a controlling terminal

Practical Scenarios:

  • Real-time monitor system processes, sorted by memory usage (in the top interface, press M key)

    top

  • View detailed information of all nginx processes in the system

    ps aux | grep nginx

  • Find the top 5 processes consuming the most CPU

    ps aux --sort=-%cpu | head -n 6

✅ Example Output (top command header information):

top – 14:30:15 up 2 days, 4:18, 3 users, load average: 0.85, 0.92, 0.78 Tasks: 235 total, 1 running, 233 sleeping, 0 stopped, 1 zombie %Cpu(s): 12.3 us, 3.7 sy, 0.0 ni, 83.5 id, 0.2 wa, 0.0 hi, 0.3 si, 0.0 st MiB Mem : 15987.3 total, 1234.5 free, 8765.2 used, 5987.6 buff/cache

💡 Tip: In the top real-time monitoring interface, press P to sort by CPU usage, press M to sort by memory usage, press k to input PID to terminate a specified process, and press q to exit.

📊 df and free: The “Health Report” of System Resources

df Command Format: df [options]free Command Format: free [options]

Core Parameters of df:

  • -h: Display disk space in a human-readable format

  • -T: Display the file system type

Core Parameters of free:

  • -h: Display memory size in a human-readable format

  • -m: Display in MB

  • -g: Display in GB

Practical Scenarios:

  • View disk usage of all mounted points

    df -h

  • View detailed system memory usage (including buffers and caches)

    free -h

✅ Example Output (free -h):

total used free shared buff/cache available Mem: 15Gi 8.5Gi 1.2Gi 320Mi 6.0Gi 6.8Gi Swap: 10Gi 0Bi 10Gi

💡 Tip: In Linux systems, the “buff/cache” portion of memory is used by the system for performance improvement and can be reclaimed by applications when needed, so the actual available memory should focus on the “available” column rather than the “free” column.

🔧 File Handling: 5 Commands for Text Operations

🔍 cat and grep: The “View and Search” of File Contents

cat Command Format: cat [options] filenamegrep Command Format: grep [options] “search pattern” filename

Core Parameters of cat:

  • -n: Display line numbers

  • -b: Display line numbers only for non-empty lines

Core Parameters of grep:

  • -i: Ignore case

  • -n: Display line numbers of matching lines

  • -r: Recursively search all files in the directory

Practical Scenarios:

  • View configuration files and display line numbers

    cat -n /etc/nginx/nginx.conf

  • Search for lines containing “ERROR” in the log file (case insensitive)

    grep -i "error" /var/log/app.log

  • Recursively search for lines containing “import numpy” in all .py files in the current directory

    grep -rn "import numpy" *.py

✅ Example Output (grep -in “error” app.log):

15:2023-10-18 08:30:15 [ERROR] Database connection failed 42:2023-10-18 08:32:47 [error] User authentication timeout

💡 Tip: grep supports regular expression searches, for example, grep -E "ERROR|WARNING" log.txt can match both ERROR and WARNING keywords.

🔍 find, cp, and mv: The “Locate, Copy, and Move” of File Operations

find Command Format: find [path] [options] [action]cp Command Format: cp [options] source_file target_filemv Command Format: mv [options] source_file or directory target_file or directory

Core Parameters of find:

  • -name: Match by file name (supports wildcard *)

  • -type: Find by file type (f=regular file, d=directory, l=link)

  • -mtime: Find by modification time (e.g., -mtime +7 means files modified more than 7 days ago)

Core Parameters of cp:

  • -r: Recursively copy directories

  • -p: Preserve file attributes (permissions, timestamps)

  • -a: Archive copy (equivalent to -pR, commonly used for backups)

Core Usage of mv:

  • Moving files within the same directory is equivalent to renaming

  • Moving files across directories keeps the original name

Practical Scenarios:

  • Find all .py files in the current directory

    find . -name "*.py"

  • Copy the /etc directory to the backup directory while preserving permissions

    cp -a /etc /backup/etc_$(date +%Y%m%d)

  • Move log.txt from the current directory to /var/log and rename it to app-2023.log

    mv log.txt /var/log/app-2023.log

  • Batch rename all .jpg files in the current directory to a uniform format

    find . -name "*.jpg" | xargs -I {} mv {} {}.bak

⚠️ Warning: When using cp -r to copy directories, if the target directory already exists, the source directory will be copied into the target directory; if the target directory does not exist, it will create the target directory and copy the contents.

Learning Resources and Advanced Paths for Command Mastery

Recommended Learning Resources

  1. Official Documentation: man [command] (e.g., man grep to view the complete manual for grep)

  2. Online Tutorials: The “Linux Command Line Complete Guide” series tutorial from Linux China

  3. Interactive Practice: The “Linux Fundamentals” module from TryHackMe

  4. Quick Reference Tool: cheat.sh (access directly via command line with curl cheat.sh/ls)

Command Line Efficiency Improvement Tips

  • Alias Settings: Set common command aliases in .bashrc or .zshrc, such as alias ll='ls -la'

  • Command History: Use the up and down arrow keys to browse history commands, Ctrl+R to search history commands

  • Wildcard Usage: Master the use of wildcards * (any character), ? (single character), [] (character range)

  • Pipeline Combination: Learn to use | to combine multiple commands, such as ps aux | grep python | wc -l to count the number of python processes

✅ Example: Set Practical Aliases

# Add to .bashrc
alias cls=’clear’
alias ..=’cd ..’
alias …=’cd ../../’
alias grep=’grep –color=auto’ # Highlight grep results

Interactive Discussion: What is your most commonly used Linux command?

In your daily development or operations work, which Linux command has significantly improved your efficiency? Have you ever encountered “thrilling moments” due to misusing commands? Feel free to share your command line experiences and tips in the comments. If you find this article helpful, don’t forget to like and follow, so more people can master the powerful features of the Linux command line!

Leave a Comment