文件系统操作指南:PowerShell / Linux Bash / Git Bash
适用场景:日常文件增删改查、目录管理、搜索文件、权限控制、软链接。
适用版本:PowerShell 7+、GNU coreutils/findutils、Git Bash;平台差异在正文中标注。
最后核对:2026-07-30
30 秒快速路径
Get-ChildItem -Force
Get-ChildItem -Recurse -File -Filter "*.ts"
Resolve-Path -LiteralPath .\dist
目录
核心概念
路径表示
| 概念 | Windows (PowerShell) | Linux / macOS | Git Bash |
|---|---|---|---|
| 分隔符 | \ |
/ |
/ |
| 根目录 | C:\ |
/ |
MSYS 根目录是 /,C 盘根目录通常映射为 /c/ |
| 家目录 | $HOME 或 ~ |
$HOME 或 ~ |
~ 通常映射到 Windows 用户目录,以 echo "$HOME" 为准 |
| 当前目录 | . |
. |
. |
| 上级目录 | .. |
.. |
.. |
绝对路径 vs 相对路径
绝对路径:从根开始,唯一确定位置
Windows: C:\Users\yourname\project\src\index.ts
Linux: /home/yourname/project/src/index.ts
相对路径:相对于当前目录
./src/index.ts # 当前目录下的 src
../package.json # 上一级目录的文件
../../config # 上两级
目录导航
# ═══ PowerShell ═══
cd C:\Users\yourname\project # 进入绝对路径
cd .\src # 进入子目录
cd .. # 回上一级
cd ~ # 回家目录
cd - # 回上一次所在目录(PowerShell 7+)
pwd # 显示当前路径(别名 Get-Location)
Push-Location .\src # 进入目录并记住当前位置
Pop-Location # 回到之前记住的位置
# ═══ Linux Bash ═══
cd /home/yourname/project # 绝对路径
cd ./src # 相对路径
cd .. # 上一级
cd ~ # 家目录
cd - # 上一次目录
pwd # 当前路径
pushd ./src # 进入并压栈
popd # 弹栈回去
查看文件与目录
# ═══ PowerShell ═══
ls # 列出当前目录(别名 Get-ChildItem)
ls -Force # 包含 Hidden 属性文件和点文件
ls *.ts # 只看 .ts 文件
Get-ChildItem -Recurse -Filter "*.json" -File # 递归搜索所有 .json
Get-ChildItem -Path . -Name # 只输出文件名
cat file.txt # 查看文件内容(别名 Get-Content)
Get-Content file.txt -Head 20 # 前 20 行
Get-Content file.txt -Tail 10 # 后 10 行
(Get-Item file.txt).Length # 文件大小(字节)
# ═══ Linux Bash ═══
ls # 列出
ls -la # 详细信息 + 隐藏文件
ls -lh # 人类可读文件大小
ls *.ts # 通配符
cat file.txt # 查看内容
head -20 file.txt # 前 20 行
tail -10 file.txt # 后 10 行
tail -f server.log # 实时追踪日志
less file.txt # 分页查看(q 退出)
wc -l file.txt # 行数统计
stat file.txt # 文件详细信息
创建
# ═══ PowerShell ═══
New-Item -ItemType File -Name "index.ts" # 创建文件
New-Item -ItemType Directory -Name "components" # 创建目录
New-Item -ItemType Directory -Force -Path "src/utils" # 递归创建(-Force = 已存在不报错)
"hello" | Out-File hello.txt # 创建并写入内容
New-Item -ItemType File -Path empty.txt -Force | Out-Null
Clear-Content -LiteralPath empty.txt # 保证文件长度为 0
# ═══ Linux Bash ═══
touch index.ts # 创建空文件(或更新时间戳)
mkdir components # 创建目录
mkdir -p src/utils/hooks # 递归创建
echo "hello" > hello.txt # 创建并写入(覆盖)
echo "more" >> hello.txt # 追加内容
> empty.txt # 创建空文件
复制、移动、重命名
# ═══ PowerShell ═══
Copy-Item file.txt file-backup.txt # 复制文件
Copy-Item -Recurse ./src ./src-backup # 复制目录
Move-Item old.txt new.txt # 重命名
Move-Item file.txt ./archive/ # 移动到另一目录
# 批量重命名(例:.js → .ts)
Get-ChildItem *.js | Rename-Item -NewName { $_.Name -replace '\.js$', '.ts' }
# ═══ Linux Bash ═══
cp file.txt file-backup.txt # 复制文件
cp -r ./src ./src-backup # 复制目录(-r = recursive)
mv old.txt new.txt # 重命名
mv file.txt ./archive/ # 移动
# 批量重命名(需要 rename 工具或循环)
for f in *.js; do mv "$f" "${f%.js}.ts"; done
删除
⚠️ 命令行删除通常不经过回收站。不要把未检查的变量、通配符或计算路径直接交给递归删除命令。
# ═══ PowerShell ═══
Remove-Item -LiteralPath .\file.txt -Confirm
# 递归删除前先解析并显示确切目标;默认保留确认提示
$target = Resolve-Path -LiteralPath .\dist -ErrorAction Stop
Get-Item -LiteralPath $target
Get-ChildItem -LiteralPath $target -Force
Remove-Item -LiteralPath $target -Recurse -Confirm
# 通配符删除也先预览
Get-ChildItem -File -Filter "*.log"
Get-ChildItem -File -Filter "*.log" | Remove-Item -Confirm
# ═══ Linux Bash ═══
rm -i -- ./file.txt # -i:删除前确认
# 递归删除:使用固定路径、解析目标、预览内容,再交互确认
target="./dist"
realpath -- "$target"
find "$target" -maxdepth 2 -print
rm -rI -- "$target" # -I:递归删除前确认一次
# 通配符先预览,避免匹配范围超出预期
printf '%s\n' ./*.log
rm -i -- ./*.log
查找文件
# ═══ PowerShell ═══
# 按名字查找(递归)
Get-ChildItem -Recurse -Filter "*.config.ts"
# 按名字模糊搜索
Get-ChildItem -Recurse | Where-Object Name -like "*auth*"
# 按修改时间(最近 24 小时内修改的文件)
Get-ChildItem -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) }
# 按文件大小(大于 1MB)
Get-ChildItem -Recurse | Where-Object { $_.Length -gt 1MB }
# ═══ Linux Bash ═══
# find — 万能查找
find . -name "*.config.ts" # 按名字
find . -name "*auth*" # 模糊匹配
find . -mtime -1 # 24 小时内修改的
find . -size +1M # 大于 1MB
find . -type d -name "node_modules" # 只找目录
# fd — 更快的现代替代品(需安装)
fd "auth" # 模糊查找
fd -e ts # 按扩展名
fd -H -I '^\.env(\..*)?$' # 包含隐藏及被 ignore 的 .env 文件
软链接 / 符号链接
软链接(Symlink)= 文件/目录的"快捷方式",指向实际位置。
Git Bash 在 Windows 上创建原生符号链接受开发者模式、权限和 MSYS 配置影响;不要假设
ln -s在所有机器上都与 Linux 完全一致。创建后用Get-Item或fsutil reparsepoint query确认它确实是链接,而不是复制品。
# ═══ PowerShell(需要管理员权限或开启开发者模式)═══
# 创建文件软链接
New-Item -ItemType SymbolicLink -Path "link.txt" -Target "C:\real\file.txt"
# 创建目录软链接
New-Item -ItemType SymbolicLink -Path ".\node_modules\.cache" -Target "D:\cache"
# 查看链接指向
Get-Item "link.txt" | Select-Object Name, LinkType, Target
# ═══ Linux Bash ═══
# 创建软链接(ln -s 目标 链接名)
ln -s /real/path/file.txt ./link.txt
ln -s /real/dir ./link-dir
# 查看链接指向
ls -la link.txt
# lrwxrwxrwx 1 user user 20 Jul 25 10:00 link.txt -> /real/path/file.txt
readlink link.txt # 只输出目标路径
readlink -f link.txt # 解析到最终真实路径
常见用途:
node_modules缓存放快速磁盘,项目里放链接- monorepo 里包之间互相链接(
npm link) - 配置文件统一管理(dotfiles)
权限管理
Linux 权限模型
# 查看权限
ls -la
# -rw-r--r-- 1 user group 1024 Jul 25 file.txt
# │├──┤├──┤├──┤
# │ 主人 组 其他
# 文件类型(- = 文件, d = 目录, l = 链接)
# 修改权限
chmod 755 script.sh # rwxr-xr-x(常用于可执行脚本)
chmod 644 config.json # rw-r--r--(常用于普通文件)
chmod +x script.sh # 添加执行权限
# 修改所有者通常需要管理员权限;递归前先确认目录
sudo chown user:group file.txt
sudo chown -R user:group ./dir
常用权限数字
| 数字 | 含义 | 典型用途 |
|---|---|---|
755 |
rwxr-xr-x | 可执行文件/目录 |
644 |
rw-r--r-- | 普通文件 |
600 |
rw------- | 私密文件(SSH key) |
777 |
rwxrwxrwx | 完全开放(几乎不该用) |
PowerShell(Windows ACL)
# Windows 用 ACL 而非 Unix 权限
Get-Acl .\file.txt | Format-List
# 先查看继承和现有权限;不要给整个 Users 组 Full Control
icacls .\file.txt
icacls .\file.txt /grant:r "$($env:USERNAME):(M)" # 仅给当前用户修改权限
磁盘占用
# ═══ PowerShell ═══
# 查看当前目录大小
(Get-ChildItem -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
# 查看各子目录大小
Get-ChildItem -Directory | ForEach-Object {
[PSCustomObject]@{
Name = $_.Name
SizeMB = [math]::Round((Get-ChildItem $_ -Recurse | Measure-Object Length -Sum).Sum / 1MB, 1)
}
} | Sort-Object SizeMB -Descending | Format-Table
# ═══ Linux Bash ═══
du -sh . # 当前目录总大小
du -sh * # 各子目录/文件大小
du -sh * | sort -rh # 按大小排序
df -h # 磁盘分区使用情况
# ncdu — 交互式磁盘占用分析(需安装)
ncdu .
前端项目常见大目录排查:
du -sh node_modules .next dist .astro .cache 2>/dev/null | sort -rh
通配符与 Glob
| 模式 | 含义 | 示例 |
|---|---|---|
* |
匹配任意多个字符 | *.ts = 所有 .ts 文件 |
? |
匹配单个字符 | file?.txt = file1.txt, fileA.txt |
[abc] |
匹配方括号中任一字符 | [ABC].ts |
[0-9] |
匹配范围 | log-[0-9].txt |
** |
Bash 开启 globstar 后递归匹配;不是 PowerShell 的通用递归语法 |
src/**/*.ts |
{a,b} |
展开多选(bash brace expansion) | *.{ts,tsx} |
# Bash 启用递归 glob
shopt -s globstar
ls src/**/*.ts # 递归匹配
# 多选展开
cp src/{index,app}.ts ./backup/ # 复制 index.ts 和 app.ts
# PowerShell 递归
Get-ChildItem -Recurse -Include "*.ts","*.tsx"
三平台对照表
| 操作 | PowerShell | Linux Bash | Git Bash |
|---|---|---|---|
| 列出文件 | ls / Get-ChildItem |
ls -la |
ls -la |
| 显示隐藏文件 | ls -Force |
ls -a |
ls -a |
| 查看文件内容 | cat / Get-Content |
cat |
cat |
| 前 N 行 | Get-Content -Head 20 |
head -20 |
head -20 |
| 创建文件 | New-Item -ItemType File |
touch |
touch |
| 创建目录 | New-Item -ItemType Directory -Force |
mkdir -p |
mkdir -p |
| 复制 | Copy-Item |
cp / cp -r |
cp / cp -r |
| 移动/重命名 | Move-Item |
mv |
mv |
| 删除文件 | Remove-Item |
rm |
rm |
| 删除目录 | Remove-Item -LiteralPath 路径 -Recurse -Confirm |
rm -rI -- 路径 |
rm -rI -- 路径 |
| 查找文件 | Get-ChildItem -Recurse -Filter |
find / fd |
find |
| 文件大小 | (Get-Item f).Length |
stat -c%s f / ls -l |
ls -l |
| 当前路径 | pwd |
pwd |
pwd |
| 软链接 | New-Item -ItemType SymbolicLink |
ln -s |
ln -s(有坑) |
| 磁盘占用 | Measure-Object Length -Sum |
du -sh |
du -sh |