Shell 命令速查手册:从入门到实战,覆盖 Linux/Mac/Windows
记录常用命令行工具及其用法,涵盖 Linux、Mac 和 Windows 系统,助你快速查阅与上手。
在日常开发中,命令行是我们绕不开的工具。无论是文件操作、目录管理,还是远程连接、权限设置,掌握常用的 Shell 命令都能极大提升效率。
本手册整理了 文件操作、目录操作、查找统计、权限管理、网络相关、进程管理 等常用命令,每个命令都包含概述、参数说明、常用示例以及 Windows 对应命令,方便你在不同操作系统间无缝切换。
无论你是刚接触命令行的新手,还是需要快速查阅的老手,这份手册都能成为你手边的参考利器。
目录
-
文件操作
- cat
- touch
- cp
- mv
- rm
-
目录操作
- ls
- cd
- pwd
- mkdir
- tree
- pushd / popd
-
查找与统计
- find
- wc
-
权限管理
- chmod
-
网络相关
- ssh
- scp
- ifconfig
-
其他工具
- open
- lsof
- kill
-
快速参考表
文件操作
cat
概述:cat(concatenate)用于连接文件并打印到标准输出。常用于查看文件内容、合并文件、创建文件等。
释义
| 命令 | 英文 | 参数 |
|---|---|---|
| cat | concatenate | -n -b -s -A -E -T |
# 查看文件内容
cat a.txt
cat a.txt b.txt # 同时显示多个文件内容
# 显示行号
cat -n a.txt # -n 对所有行编号
cat -b a.txt # -b 只对非空行编号
cat -s a.txt # -s 压缩连续空行为一行
# 显示特殊字符
cat -A a.txt # 显示所有特殊字符
cat -E a.txt # -E 在每行末尾显示 $
cat -T a.txt # -T 将Tab显示为 ^I
# 合并文件
cat a.txt b.txt > c.txt # 合并a.txt和b.txt到c.txt
cat *.txt > all.txt # 合并所有txt文件
# 追加内容
cat a.txt >> b.txt # 将a.txt内容追加到b.txt末尾
# 创建文件
cat > newfile.txt # 从标准输入创建文件,Ctrl+D结束
cat > newfile.txt << EOF
第一行内容
第二行内容
EOF
# 清空文件
cat /dev/null > a.txt
# 配合管道使用
cat a.txt | grep "error" # 查找包含error的行
cat a.txt | head -10 # 显示前10行
cat a.txt | tail -20 # 显示后20行
Windows 对应命令
type a.txt # 显示文件内容
type a.txt > b.txt # 复制文件
Get-Content a.txt # PowerShell
touch
概述:touch命令有两个功能:一是创建空文件,二是更新文件的时间戳(访问时间、修改时间)。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| touch | touch | -a -c -d -m -r -t |
touch a.txt # 创建空文件或更新时间戳
touch file1.txt file2.txt # 同时创建多个文件
touch {a,b,c}.txt # 创建a.txt、b.txt、c.txt
touch test{1..5}.txt # 创建test1.txt到test5.txt
touch -a a.txt # 只更新访问时间
touch -m a.txt # 只更新修改时间
touch -c a.txt # 如果文件不存在则不创建
touch -d "2024-01-01 12:00:00" a.txt # 使用指定日期时间
touch -t 202401011200 a.txt # 使用指定时间格式
touch -r ref.txt a.txt # 使用ref.txt的时间戳更新a.txt
stat a.txt # 查看文件时间戳
Windows 对应命令
type nul > a.txt # CMD创建空文件
New-Item a.txt -ItemType File # PowerShell
cp
概述:cp(copy)用于复制文件或目录,支持递归复制、保留属性等功能。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| cp | copy | -r -f -i -u -p -v -l |
# 复制文件
cp a.txt b.txt # 复制a.txt为b.txt
cp a.txt ./backup/ # 复制a.txt到backup目录
cp a.txt b.txt ./backup/ # 复制多个文件到目录
# 复制目录
cp -r source/ destination/ # -r 递归复制目录及其内容
# 覆盖确认
cp -i a.txt b.txt # -i 覆盖前确认
cp -f a.txt b.txt # -f 强制覆盖
cp -n a.txt b.txt # -n 不覆盖已存在的文件
# 保留文件属性
cp -p a.txt b.txt # -p 保留权限、所有者、时间戳
cp -a source/ destination/ # -a 归档模式(保留所有属性)
# 其他选项
cp -v a.txt b.txt # -v 显示复制过程
cp -l a.txt b.txt # -l 创建硬链接
cp -s a.txt b.txt # -s 创建符号链接
cp -b a.txt b.txt # -b 覆盖前创建备份
Windows 对应命令
copy a.txt b.txt # 复制文件
xcopy source\ dest\ /E /I # 复制目录
robocopy source\ dest\ /E # 高级复制(推荐)
mv
概述:mv(move)功能有两个:1. 重命名文件/目录 2. 将源文件移动到目标目录
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| mv | move | -b -f -i -n -u -v |
# 重命名文件
mv a.txt old_a.txt # 重命名文件
# 移动文件
mv a.txt ./backup/ # 移动文件到目录
mv a.txt b.txt c.txt ./backup/ # 移动多个文件
# 移动目录
mv ./old_dir ./new_dir # 重命名目录
mv ./dir1 ./dir2/ # 把dir1移动到dir2目录下
# 覆盖选项
mv -b a.txt b.txt # 覆盖前创建备份
mv -f a.txt b.txt # 强制覆盖
mv -i a.txt b.txt # 交互模式,覆盖前确认
mv -n a.txt b.txt # 不覆盖已存在的文件
mv -u a.txt ./backup/ # 只移动更新的文件
mv -v a.txt ./backup/ # 显示详细信息
# 使用通配符
mv *.txt ./backup/ # 移动所有txt文件
Windows 对应命令
move a.txt old_a.txt # 重命名
move a.txt .\backup\ # 移动文件
rm
概述:rm(remove)用于删除文件或目录。删除后无法恢复,使用时需谨慎。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| rm | remove | -r -f -i -v -d -I |
# 删除文件
rm a.txt # 删除文件
rm file1.txt file2.txt # 删除多个文件
rm *.txt # 删除所有txt文件
rm -i a.txt # -i 交互模式,删除前确认
rm -f a.txt # -f 强制删除,不提示确认
# 删除目录
rm -r directory/ # -r 递归删除目录及其内容
rm -rf directory/ # -rf 强制递归删除(需谨慎)
rm -d empty_dir/ # -d 删除空目录
# 其他选项
rm -v a.txt # -v 显示删除过程
rm -I directory/ # -I 删除超过3个文件时提示一次
# 安全提示
# 1. 使用 -i 参数进行交互确认
# 2. 删除前先用 ls 确认要删除的内容
# 3. 重要文件做好备份
# 4. 永远不要执行 rm -rf /
alias rm='rm -i' # 创建安全别名
Windows 对应命令
del a.txt # 删除文件
rmdir directory # 删除空目录
rmdir /s directory # 删除目录及其内容
Remove-Item a.txt # PowerShell
目录操作
ls
概述:ls(list)是最常用的命令之一,用于列出目录内容和文件信息。
释义
| 命令 | 英文 | 参数 |
|---|---|---|
| ls | list | -a -l -h -R -t -S -r |
# 基本用法
ls # 列出当前目录内容
ls /home/user # 列出指定目录内容
# 显示详细信息
ls -l # 长格式显示
ls -lh # -h 人类可读格式显示大小
ls -la # -a 显示所有文件(包括隐藏)
ls -lA # -A 显示除 . 和 .. 外的所有文件
# 排序方式
ls -lt # -t 按修改时间排序(最新在前)
ls -lS # -S 按文件大小排序(最大在前)
ls -lX # -X 按扩展名排序
ls -lr # -r 反向排序
ls -ltr # 按修改时间排序(最旧在前)
# 递归显示
ls -R # 递归显示子目录
# 显示文件类型
ls -F # 添加类型标识符:/ 目录、* 可执行、@ 符号链接
ls -p # 只在目录后添加 /
# 其他选项
ls -i # 显示inode号
ls -d */ # 只显示目录
ls -1 # 每行显示一个文件
# 组合使用
ls -lah # 显示所有文件,长格式,人类可读大小
ls -lhS # 按大小排序,人类可读
ls -lha --color=auto # 彩色显示
Windows 对应命令
dir # 列出目录内容
dir /a # 显示所有文件
dir /s # 递归显示
dir /o:n # 按名称排序
Get-ChildItem # PowerShell
cd
概述:cd(change directory)是最常用的命令之一,用于切换当前工作目录。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| cd | change directory | . .. ~ - |
# 基本用法
cd /home/user # 切换到绝对路径目录
cd ./project # 切换到相对路径目录
cd .. # 切换到上一级目录
cd ../.. # 切换到上两级目录
# 特殊目录
cd ~ # 切换到家目录
cd # 不带参数,切换到家目录
cd ~username # 切换到指定用户的家目录
cd - # 切换到上一个工作目录
# 使用环境变量
cd $HOME # 切换到home目录
cd $OLDPWD # 切换到上一个工作目录
# 实用技巧
pushd /path/to/dir # 保存当前目录并切换
popd # 返回之前的目录
相关环境变量
echo $HOME # 家目录路径
echo $PWD # 当前工作目录
echo $OLDPWD # 上一个工作目录
Windows 对应命令
cd \Users\work # 切换目录
cd .. # 上一级目录
cd %USERPROFILE% # 切换到家目录
cd /d D:\project # /d 参数可跨盘符切换
pwd
概述:pwd(print working directory)用于显示当前工作目录的绝对路径。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| pwd | print working directory | -L -P |
pwd # 显示当前工作目录的绝对路径
# 处理符号链接
pwd -L # 显示逻辑路径(包含符号链接)
pwd -P # 显示物理路径(解析符号链接)
# 在脚本中使用
current_dir=$(pwd)
echo "当前目录: $current_dir"
相关环境变量
echo $PWD # 当前工作目录
echo $OLDPWD # 上一个工作目录
cd - # 切换到上一个工作目录
Windows 对应命令
cd # 显示当前目录
echo %cd% # 显示当前目录
Get-Location # PowerShell
mkdir
概述:mkdir(make directory)用于创建新目录。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| mkdir | make directory | -p -m -v |
# 基本用法
mkdir mydir # 创建目录
mkdir dir1 dir2 dir3 # 同时创建多个目录
# 创建嵌套目录
mkdir -p parent/child/grandchild # -p 递归创建
mkdir -p project/{src,lib,tests} # 创建多个子目录
mkdir -p project/{src/{js,css},lib,tests} # 创建嵌套结构
# 设置权限
mkdir -m 755 mydir # 创建时指定权限
mkdir -m 777 public # 创建完全开放权限的目录
# 显示详细信息
mkdir -v mydir # 显示创建过程
mkdir -pv parent/child # 递归创建并显示过程
# 实用技巧
mkdir -p backup/$(date +%Y%m%d) # 创建以日期命名的备份目录
mkdir -p logs/{access,error,debug} # 创建多个日志目录
Windows 对应命令
mkdir mydir # 创建目录
md mydir # 简写
mkdir a\b\c # 递归创建(自动支持)
New-Item -ItemType Directory -Path mydir # PowerShell
tree
概述:tree命令用于以树状结构递归列出目录下的所有内容,直观展示目录层级关系。
释义
| 命令 | 英文 | 参数 |
|---|---|---|
| tree | tree | -L -I -a -d -f -C |
tree # 列出当前目录的树状结构
tree /home/user/project # 列出指定目录
tree -L 2 # 只显示2层深度
tree -I "node_modules" # 排除node_modules目录
tree -L 3 -I "node_modules|dist|build" # 排除多个目录
tree -a # 显示所有文件(包括隐藏)
tree -d # 只显示目录
tree -f # 显示完整路径
tree -C # 彩色输出
tree -h # 以人类可读格式显示文件大小
tree -p # 显示每个文件的权限
tree -u # 显示文件所有者
tree -P "*.js" # 只显示匹配模式的文件
# 组合使用
tree -L 2 -I "node_modules" -C -h
# 输出到文件
tree > tree.txt
tree -H ./ > index.html # 生成HTML格式
不同系统对应命令
| 系统 | 命令 | 说明 |
|---|---|---|
| Mac/Linux | tree | 需安装 |
| Windows | tree /F | 系统自带 |
# Windows 命令
tree # 只显示目录结构
tree /F # 显示所有文件
# Mac 安装
brew install tree
# Linux 安装
apt-get install tree # Debian/Ubuntu
yum install tree # CentOS/RHEL
pushd / popd
概述:pushd和popd是一对配合使用的命令,用于管理目录栈。常用于在多个目录间快速切换。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| pushd | push directory | +n -n |
| popd | pop directory | +n -n |
| dirs | display directories | -c -l -p -v |
# pushd 基本用法
pushd /home/user/work # 将当前目录压入栈,并切换目录
pushd # 在当前目录和栈顶目录之间切换
# popd 基本用法
popd # 弹出栈顶目录,并切换回去
# dirs 查看目录栈
dirs # 显示目录栈内容
dirs -v # 详细格式显示(带编号)
dirs -c # 清空目录栈
# 实际使用场景
cd /home/user/project
pushd /var/log
# ... 做一些操作 ...
popd # 回到/home/user/project
Windows 对应命令
pushd C:\Users\work # 压入并切换目录
popd # 弹出并返回
查找与统计
find
概述:find 用于在目录树中搜索文件和目录,支持按名称、类型、大小、时间等多种条件查找。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| find | find | -name -type -size -time -exec |
# 按名称查找
find . -name "a.txt" # 查找名为a.txt的文件
find . -name "*.txt" # 查找所有txt文件
find . -iname "README*" # -iname 忽略大小写
# 按类型查找
find . -type f # 普通文件
find . -type d # 目录
find . -type l # 符号链接
# 按大小查找
find . -size +100M # 大于100M的文件
find . -size -1k # 小于1k的文件
find . -empty # 空文件和空目录
# 按时间查找
find . -mtime -7 # 7天内修改过的文件
find . -mtime +30 # 30天前修改过的文件
find . -mmin -60 # 60分钟内修改过的文件
find . -newer a.txt # 比a.txt更新的文件
# 按权限查找
find . -perm 755 # 权限为755的文件
find . -perm -u+x # 用户有执行权限的文件
# 按用户/组查找
find . -user root # 属于root用户的文件
find . -group admin # 属于admin组的文件
# 组合条件
find . -name "*.txt" -type f
find . ( -name "*.txt" -o -name "*.md" ) # -o 或
find . -not -name "*.txt" # -not 非
# 执行操作
find . -name "*.log" -delete # 删除找到的文件
find . -name "*.txt" -exec ls -l {} ; # 执行命令
find . -name "*.txt" -exec rm {} ; # 删除文件
find . -name "*.txt" -exec mv {} ./backup/ ; # 移动文件
# 限制搜索深度
find . -maxdepth 2 -name "*.txt" # 最多搜索2层
# 排除目录
find . -path "./node_modules" -prune -o -name "*.js" -print
# 实用示例
find /var/log -name "*.log" -mtime +7 -delete # 删除7天前的日志
find . -type f -size 0 -delete # 删除所有空文件
find . -type d -empty # 查找所有空目录
Windows 对应命令
dir /s /b *.txt # 递归查找文件
where /r . *.txt # 查找文件
Get-ChildItem -Recurse -Filter "*.txt" # PowerShell
wc
概述:wc(word count)用于统计文件的行数、单词数、字节数等信息。
释义
| 命令 | 英文 | 参数 |
|---|---|---|
| wc | word count | -l -w -c -m -L |
wc ./release.sh # 输出:行数、单词数、字节数、文件名
wc -l file.txt # 只统计行数
wc -w file.txt # 只统计单词数
wc -c file.txt # 只统计字节数
wc -m file.txt # 统计字符数
wc -L file.txt # 输出最长行的长度
# 统计多个文件
wc file1.txt file2.txt file3.txt
# 配合管道使用
cat file.txt | wc -l # 统计文件行数
ls -l | wc -l # 统计当前目录文件数量
grep "error" log.txt | wc -l # 统计包含error的行数
# 统计代码行数
find . -name "*.js" | xargs wc -l
find . -name "*.js" -exec wc -l {} + | tail -1 # 只显示总行数
Windows 对应命令
(Get-Content file.txt).Count # 行数
(Get-Content file.txt | Measure-Object -Word).Words # 单词数
权限管理
chmod
概述:chmod(change mode)用于修改文件或目录的权限。Linux/Unix系统中每个文件都有读(r)、写(w)、执行(x)三种权限。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| chmod | change mode | -R -v -c -f |
# 数字权限
chmod 755 ./script.sh # 所有者可读写执行,其他用户可读执行
chmod 644 ./config.txt # 所有者可读写,其他用户只读
# 符号权限
chmod u+x ./script.sh # 给所有者添加执行权限 (u=user)
chmod g-w ./config.txt # 移除用户组的写权限 (g=group)
chmod o+r ./file.txt # 给其他用户添加读权限 (o=other)
chmod a+x ./script.sh # 给所有用户添加执行权限 (a=all)
# 递归修改
chmod -R 755 ./build/ # 递归修改目录及其下所有文件
# 显示详细信息
chmod -v 755 ./script.sh # 显示权限修改的详细信息
# 参考文件
chmod --reference=./a.txt ./b.txt # 将b.txt的权限设置为与a.txt相同
网络相关
ssh
概述:ssh(Secure Shell)是一种加密的网络传输协议,用于安全地远程登录和执行命令。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| ssh | Secure Shell | -p -i -L -R -X -x -N |
# 基本连接
ssh user@192.168.1.100 # 连接到远程服务器
ssh -p 2222 user@192.168.1.100 # 指定端口
# 使用密钥连接
ssh -i ~/.ssh/id_rsa user@192.168.1.100
# 执行远程命令
ssh user@192.168.1.100 "ls -la"
ssh user@192.168.1.100 "cat /etc/os-release"
# X11转发
ssh -X user@192.168.1.100 # 开启X11转发
ssh -x user@192.168.1.100 # 禁用X11转发
# 端口转发(本地)
ssh -L 8080:localhost:80 user@192.168.1.100 # 本地8080转发到远程80
ssh -L 3306:db.server:3306 user@jump.server # 通过跳板机访问数据库
# 端口转发(远程)
ssh -R 8080:localhost:80 user@192.168.1.100 # 远程8080转发到本地80
# SOCKS代理
ssh -D 1080 user@192.168.1.100 # 创建SOCKS5代理
# 后台运行
ssh -N -f -L 8080:localhost:80 user@192.168.1.100
# 保持连接
ssh -o ServerAliveInterval=60 user@192.168.1.100
# SSH配置文件 (~/.ssh/config)
Host myserver
HostName 192.168.1.100
User user
Port 22
IdentityFile ~/.ssh/id_rsa
# 生成密钥对
ssh-keygen -t rsa -b 4096 # 生成RSA密钥对
ssh-keygen -t ed25519 # 生成ED25519密钥对(推荐)
# 复制公钥到远程
ssh-copy-id user@192.168.1.100
Windows 对应命令
ssh user@192.168.1.100 # Windows 10/11 自带OpenSSH
scp
概述:scp(Secure Copy)基于SSH协议,用于在本地和远程主机之间安全地复制文件和目录。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| scp | secure copy | -r -P -p -i -C -v |
# 本地复制到远程
scp ./a.txt user@192.168.1.100:/home/user/
scp ./a.txt user@192.168.1.100:~/
scp ./a.txt user@192.168.1.100:./work/
# 远程复制到本地
scp user@192.168.1.100:/home/user/a.txt ./
scp user@192.168.1.100:/home/user/a.txt ./local.txt
# 远程到远程
scp user1@server1:/file.txt user2@server2:/path/
# 复制目录
scp -r ./build user@192.168.1.100:/home/user/
scp -r user@192.168.1.100:/home/user/build ./
# 指定端口
scp -P 2222 ./a.txt user@192.168.1.100:/home/user/
# 保持文件属性
scp -p ./a.txt user@192.168.1.100:/home/user/
# 使用密钥
scp -i ~/.ssh/id_rsa ./a.txt user@192.168.1.100:/home/user/
# 压缩传输
scp -C ./bigfile user@192.168.1.100:/home/user/
# 限制带宽
scp -l 800 ./a.txt user@192.168.1.100:/home/user/
# 复制多个文件
scp a.txt b.txt c.txt user@192.168.1.100:/home/user/
scp user@192.168.1.100:/home/user/{a.txt,b.txt} ./
Windows 对应命令
pscp a.txt user@192.168.1.100:/home/user/ # PuTTY工具
scp a.txt user@192.168.1.100:/home/user/ # Windows 10/11 自带
ifconfig
概述:ifconfig(interface configuration)用于配置和显示网络接口信息。在新版Linux中已被ip命令替代。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| ifconfig | interface configuration | -a up down |
ifconfig # 显示当前激活的网络接口信息
ifconfig -a # 显示所有网络接口信息
ifconfig eth0 # 显示指定网络接口信息
ifconfig eth0 192.168.1.100 # 设置IP地址
ifconfig eth0 up # 启用网络接口
ifconfig eth0 down # 禁用网络接口
ifconfig eth0 netmask 255.255.255.0 # 设置子网掩码
Linux、Mac与Windows的区别
| 项目 | Linux | Mac | Windows |
|---|---|---|---|
| 默认命令 | ifconfig(已过时) | ifconfig(仍常用) | ipconfig(常用) |
| 推荐替代 | ip addr | ifconfig 或 ip | ipconfig / Get-NetAdapter |
# Linux 新版推荐使用 ip 命令
ip addr show # 显示网络接口信息
ip link set eth0 up # 启用接口
ip link set eth0 down # 禁用接口
# Mac 上 ifconfig 仍然可用
ifconfig en0 # 查看无线网卡信息
ifconfig lo0 # 查看本地回环接口
# Windows 使用 ipconfig 命令
ipconfig # 显示简要网络配置信息
ipconfig /all # 显示详细网络配置信息
ipconfig /release # 释放DHCP分配的IP地址
ipconfig /renew # 重新获取DHCP分配的IP地址
ipconfig /flushdns # 清除DNS缓存
# Windows PowerShell 新版命令
Get-NetAdapter # 获取网络适配器列表
Get-NetIPAddress # 获取IP地址信息
其他工具
open
概述:open命令用于打开文件、目录或应用程序。Mac使用open,Linux使用xdg-open,Windows使用start。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| open | open | -a -e -t -n -R |
# Mac open 命令
open ./a.txt # 用默认编辑器打开文件
open ./build # 打开Finder并定位到目录
open . # 打开当前目录的Finder窗口
open -a "Visual Studio Code" ./project # 用指定应用打开
open -a "Google Chrome" https://www.google.com # 用指定浏览器打开URL
open -e ./a.txt # 使用TextEdit打开
open -n ./app.app # 打开应用程序的新实例
open -R ./a.txt # 在Finder中显示文件
# 打开多个文件
open a.txt b.txt c.txt
open *.txt # 打开所有txt文件
不同系统的对应命令
| 系统 | 命令 | 说明 |
|---|---|---|
| Mac | open | 原生支持 |
| Linux | xdg-open | 通用打开命令 |
| Linux | nautilus | GNOME文件管理器 |
| Windows | start | 原生支持 |
# Linux 命令
xdg-open ./a.txt # 用默认程序打开文件
xdg-open https://www.google.com # 用默认浏览器打开URL
nautilus ./build # 用GNOME文件管理器打开目录
# Windows 命令
start a.txt # 用默认程序打开文件
start . # 打开当前目录的资源管理器
start https://www.google.com # 用默认浏览器打开URL
start notepad a.txt # 用记事本打开文件
lsof
概述:lsof(list open files)用于列出当前系统打开的文件。在Linux/Unix中,一切皆文件,因此lsof可以查看端口、进程、网络连接等信息。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| lsof | list open files | -i -p -u -c -P |
# 基本用法
lsof # 列出所有打开的文件
lsof a.txt # 查看指定文件被哪个进程占用
# 查看端口占用
lsof -i :8080 # 查看8080端口占用情况
lsof -i :22 # 查看22端口占用情况
lsof -i # 列出所有网络连接
lsof -i TCP # 只列出TCP连接
lsof -i UDP # 只列出UDP连接
lsof -i :80 -P # -P 显示端口号而非服务名
lsof -i :80 -n # -n 显示IP而非主机名
# 查看进程打开的文件
lsof -p 1234 # 查看PID为1234的进程打开的文件
lsof -p 1234,5678 # 查看多个进程
# 查看用户打开的文件
lsof -u root # 查看root用户打开的文件
lsof -u username # 查看指定用户打开的文件
lsof -u ^root # 查看非root用户打开的文件
# 按进程名查找
lsof -c nginx # 查看nginx进程打开的文件
lsof -c ssh # 查看ssh相关进程
lsof -c /nginx/ # 使用正则表达式匹配
# 查看目录被占用
lsof +D /path/to/dir # 查看目录下被打开的文件
lsof +d /path/to/dir # 只查看目录本身
# 组合使用
lsof -i :8080 -P -n # 查看端口占用,显示数字
lsof -u root -i :22 # 查看root用户对22端口的使用
# 常用场景
lsof -i :8080 | grep LISTEN # 查看8080端口监听状态
kill -9 $(lsof -t -i :8080) # 杀掉占用8080端口的进程
# 查看删除但仍占用空间的文件
lsof | grep deleted # 查找已删除但进程仍在使用的文件
Windows 对应命令
# Windows 使用 netstat 查看端口
netstat -ano | findstr :8080 # 查看8080端口占用
netstat -ano | findstr LISTENING # 查看所有监听端口
# 使用 tasklist 查看进程
tasklist | findstr nginx # 查看nginx进程
# PowerShell
Get-NetTCPConnection -LocalPort 8080 # 查看端口占用
Get-Process -Id 1234 # 查看进程信息
kill
概述:kill 用于向进程发送信号,常用于终止进程。除了终止进程,还可以发送其他信号如暂停、继续等。
释义
| 命令 | 英文释义 | 参数 |
|---|---|---|
| kill | kill | -l -s -9 -15 -2 -1 |
# 基本用法
kill 1234 # 发送SIGTERM(15)信号,正常终止进程
kill -15 1234 # 同上,明确指定信号15
# 强制终止
kill -9 1234 # 发送SIGKILL(9)信号,强制终止进程
kill -KILL 1234 # 同上,使用信号名
# 发送其他信号
kill -1 1234 # SIGHUP,重新读取配置文件
kill -2 1234 # SIGINT,中断进程(同Ctrl+C)
kill -19 1234 # SIGSTOP,暂停进程
kill -18 1234 # SIGCONT,继续暂停的进程
# 列出所有信号
kill -l # 列出所有可用信号
kill -l 9 # 查看信号9的名称
# 按进程名终止
killall nginx # 终止所有nginx进程
killall -9 nginx # 强制终止所有nginx进程
killall -u user nginx # 终止指定用户的nginx进程
# 使用pkill(按模式匹配)
pkill nginx # 终止所有nginx进程
pkill -9 nginx # 强制终止
pkill -f "python script.py" # 按完整命令行匹配
pkill -u root # 终止root用户的所有进程
pkill -t pts/0 # 终止指定终端的进程
# 常用组合
ps aux | grep nginx # 先查找进程
kill -9 1234 # 再终止进程
# 一步到位
kill -9 $(pgrep nginx) # 终止所有nginx进程
kill -9 $(lsof -t -i :8080) # 终止占用8080端口的进程
# 查看进程状态
ps -ef | grep nginx # 查看进程
pgrep -l nginx # 查找进程ID和名称
pidof nginx # 获取进程ID
常用信号说明
| 信号编号 | 信号名称 | 说明 |
|---|---|---|
| 1 | SIGHUP | 挂起,重新读取配置 |
| 2 | SIGINT | 中断(同Ctrl+C) |
| 9 | SIGKILL | 强制终止,不可捕获 |
| 15 | SIGTERM | 正常终止(默认) |
| 18 | SIGCONT | 继续暂停的进程 |
| 19 | SIGSTOP | 暂停进程 |
| 20 | SIGTSTP | 终端暂停(同Ctrl+Z) |
Windows 对应命令
# Windows 使用 taskkill
taskkill /PID 1234 # 终止指定PID的进程
taskkill /PID 1234 /F # 强制终止
taskkill /IM nginx.exe # 按进程名终止
taskkill /IM nginx.exe /F # 强制终止
taskkill /F /IM nginx.exe # 强制终止所有nginx进程
# PowerShell
Stop-Process -Id 1234 # 终止进程
Stop-Process -Name nginx # 按名称终止
Stop-Process -Id 1234 -Force # 强制终止
kill 1234 # PowerShell别名
快速参考表
| 命令 | 英文释义 | 功能 | Windows对应 |
|---|---|---|---|
| cat | concatenate | 查看/合并文件 | type |
| touch | touch | 创建文件/更新时间戳 | type nul > |
| cp | copy | 复制文件 | copy/xcopy |
| mv | move | 移动/重命名 | move |
| rm | remove | 删除文件 | del/rmdir |
| ls | list | 列出目录 | dir |
| cd | change directory | 切换目录 | cd |
| pwd | print working directory | 显示当前目录 | cd |
| mkdir | make directory | 创建目录 | mkdir/md |
| tree | tree | 树状显示目录 | tree |
| find | find | 查找文件 | where |
| wc | word count | 统计行/字/字节 | PowerShell |
| chmod | change mode | 修改权限 | icacls |
| ssh | Secure Shell | 远程登录 | ssh |
| scp | secure copy | 远程复制 | scp/pscp |
| ifconfig | interface configuration | 网络配置 | ipconfig |
| open | open | 打开文件 | start |
| lsof | list open files | 查看打开的文件 | netstat |
| kill | kill | 终止进程 | taskkill |
结语
命令行是程序员和运维人员的必备技能,掌握常用命令可以大幅提升工作效率。本手册涵盖了你日常开发中最可能用到的命令,每个命令都提供了丰富的示例和跨平台的对应用法。建议收藏备用,遇到记不清的命令时,随时查阅。
如果你有更多的常用命令想要补充,或者发现任何错误,欢迎在评论区留言.