Skip to content

Command 行基础

学习 terminal: 导航 directories, 运行 commands, piping 输出, and 为什么 developers live in shell.

你将学到

  • 什么 command 行 (terminal) 是 and 为什么 developers use 它
  • 必要 commands: ls, CD, pwd, mkdir, rm, cat
  • 如何 导航 filesystem 从 terminal
  • Piping and redirecting 输出之间 commands
  • 如何 command-行 skills accelerate development workflow

概念讲解

Command Line Basics

The command line (also called the terminal, shell, or console) is a text-based interface for interacting with your computer. Instead of clicking icons and menus, you type commands and press Enter. For developers, the command line is the primary workspace — it is faster, more powerful, and more automatable than a graphical interface.

Why Developers Use the Terminal

The terminal gives you direct access to every tool in your development stack: running programs, managing files, using Git, starting servers, installing packages, and running tests. GUI tools show you a curated subset of options; the terminal gives you everything. Once you learn the commands, you can work faster than with a mouse — and you can script sequences of commands to automate repetitive tasks.

Essential Commands

Here are the commands you will use every day:

| Command | What it does | |---------|-------------| | pwd | Print working directory — where am I? | | ls | List files in the current directory | | CD folder | Change directory — move into folder | | CD .. | Move up one directory | | mkdir name | Create a new directory | | touch file.js | Create an empty file | | cat file | Print a file's contents to the screen | | rm file | Remove (delete) a file | | cp src dest | Copy a file | | mv src dest | Move or rename a file |

Navigating the Filesystem

The terminal is always "in" a specific directory — your working directory. pwd tells you where you are. ls shows what is here. CD moves you somewhere else. You can use relative paths (CD src/组件 — relative to where you are) or absolute paths (CD /home/用户/项目 — starting from the root).

Piping and Redirection

Two powerful features let you combine commands:

  • Redirection (>) sends output to a file instead of the screen: ls > files.txt saves the listing to a file.
  • Pipe (|) sends the output of one command as input to another: cat 数据.JSON | head -5 shows the first 5 lines of a file.

Pipes are the Unix philosophy in action: small, focused commands that do one thing well, combined to solve complex tasks.

The Development Workflow

A typical developer session in the terminal looks like this: CD my-项目 to enter the project, git 拉取 to get the latest code, npm 安装 to install dependencies, npm 测试 to run tests, npm 运行 dev to start the dev server. Every one of these is a command-line operation. Learning the terminal is not optional for a developer — it is the environment where your code lives.

示例代码

              # Navigate to a project and explore its structure
pwd                          # /home/user
cd projects/my-app           # move into the project
ls                           # see what's here
# package.json  src/  tests/  README.md

# Look at a file's contents
cat package.json             # print the whole file
head -5 package.json         # first 5 lines only

# Create a new directory and file for a feature
mkdir src/features/auth
touch src/features/auth/login.js

# Search for a function across all JavaScript files
grep -r "function login" src/
# src/features/auth/login.js:function login(user) {

# Combine commands with a pipe: count JavaScript files
ls src/**/*.js | wc -l       # e.g., 42

# Run the project's test suite
npm test
            

这个会话 demonstrates 导航 (CD, ls, pwd), file inspection (cat, head), file creation (mkdir, touch), 搜索 (grep), piping (|), and 运行项目 command (npm 测试). 这些是 daily 操作的 developer.

动手试一试

  • Crontab Generator

    Cron schedules 是配置 in terminal. Generate and 理解 cron 表达式 used in command-行 scheduling.

  • HTTP 请求 Tester

    测试 APIs 从 command 行 (使用 curl) 是核心 skill. 这个 tool gives 你 GUI equivalent 的 curl commands.

常见错误

错误写法

Using rm on files 不使用是 确定的 path.

正确做法

terminal 有否 撤销. 总是检查使用 ls 首先, and use rm -i (interactive 模式) 哪个 asks for confirmation. 从不运行 rm -rf / or 相似 destructive commands 不使用理解它们 fully.

错误写法

获取 lost in filesystem 使用 CD.

正确做法

Use pwd 到检查哪里你 是. Use CD 使用否 实参到 返回 home, or CD ~. Tab-completion (按下 Tab 虽然输入 path) prevents typos and 速度上 导航.

错误写法

Memorizing commands 不使用理解什么它们做.

正确做法

Use man command (manual) or command --help 到读取什么 command 做 and 它的 options. 理解概念后面 commands 是更多 durable than rote memorization.

相关速查表

  • Bash Cheatsheet

    完整引用 for shell commands, piping, redirection, and scripting.

  • Git Cheatsheet

    Git 是 command-行 tool — 学习 version control commands 你将 use daily.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误