Skip to content

Command 行基础

学习 terminal, 必要 commands, and 如何 导航 and manipulate file 系统.

你将学到

  • 什么 terminal (shell) 是 and 如何 打开它
  • 必要 commands: ls, CD, pwd, mkdir, rm, cp, mv
  • 如何 读取 and 搜索 file 内容使用 cat, grep, 更少
  • Pipes and redirection 到组合 commands

概念讲解

终端

The command line, also called the terminal or shell, is a text-based interface for interacting with your computer's operating system. Instead of clicking buttons and menus, you type commands and press Enter. Every developer and DevOps engineer needs command-line fluency because servers, build tools, and automation scripts all run in the shell.

On macOS the default shell is zsh; on most Linux distributions it is bash; on Windows you can use PowerShell or install Git Bash and the Windows Subsystem for Linux (WSL). The concepts are the same across all of them, though some commands differ slightly.

Essential Navigation Commands

The most fundamental commands let you move around the file system and see what is there. pwd prints your current directory. ls lists files in the current directory. CD changes the directory. These three commands are the starting point for everything else you will do on the command line.

File Operations

You can create, copy, move, and delete files and directories entirely from the terminal. mkdir creates directories. touch creates an empty file. cp copies files, mv moves or renames them, and rm removes them. The -r flag makes rm and cp work recursively on directories. Always be careful with rm -rf — it deletes without asking for confirmation.

Pipes and Redirection

One of the most powerful features of the shell is the ability to chain commands together. The pipe operator | sends the output of one command as input to another. Redirection operators > and >> send output to a file. For example, ls | grep ".txt" lists only text files, and echo "hello" > file.txt writes text to a file. These building blocks let you compose complex operations from simple commands.

Learning the Shell

The best way to learn the command line is to use it daily. Start by replacing file explorer clicks with terminal commands. Learn one new command each day. Use man command or command --help to read built-in documentation. Bookmark a cheat sheet. Over time, you will build muscle memory and the terminal will become faster than any GUI. Every DevOps tool — Docker, Kubernetes, Terraform — is a command-line tool, so this foundation is essential for everything that follows.

示例代码

              # Navigate the file system
pwd                          # print current directory
ls -la                       # list all files with details
cd ~/projects                # change to projects directory

# Create and manipulate files
mkdir -p myapp/src           # create nested directories
touch myapp/src/index.js     # create an empty file
cp index.js index.bak.js     # copy a file
mv index.bak.js backup/      # move a file

# Search and filter
grep -r "TODO" .             # search for TODO in all files
cat package.json | head -20  # show first 20 lines
            

这些 commands cover 必要: 导航, file manipulation, and 搜索. 练习它们直到它们是 muscle memory — 你将 use 它们每个 day as DevOps engineer.

动手试一试

常见错误

错误写法

Using rm -rf 不使用检查 path

正确做法

总是验证你的 current directory 使用 pwd and use ls 到确认什么你 是关于到 删除. typo 如 rm -rf / 能 destroy 你的整个系统.

错误写法

Forgetting 那个 Linux paths use 向前 slashes

正确做法

On Linux and macOS, paths use 向前 slashes (/). On Windows PowerShell, 两者都工作, 但 in Git Bash 总是 use 向前 slashes.

练习 Command 行基础

6 个练习

练习 Command 行基础

查阅不认识的术语

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

查看术语表

动手做项目

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

查看项目

解读错误信息

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

查看错误