文件系统
Windows 文件系统基础、NTFS 特性、磁盘操作及高级文件命令。
文件系统类型
| 类型 | 说明 | 适用场景 |
|---|---|---|
| NTFS | 支持权限、加密、压缩、日志、大文件 | Windows 系统盘、数据盘 |
| FAT32 | 兼容性好,单文件最大 4GB | U 盘、跨平台兼容 |
| exFAT | FAT32 的扩展,无 4GB 限制 | 大容量 U 盘、SD 卡 |
| ReFS | 弹性文件系统,适合大数据 | Windows Server、存储池 |
NTFS 核心特性
压缩
compact /c /s C:\logs :: 压缩目录
compact /u /s C:\logs :: 解压缩
compact /q C:\logs :: 查看压缩状态加密(EFS)
cipher /e /s:C:\secret :: 加密目录
cipher /d /s:C:\secret :: 解密目录
cipher /r:backup :: 备份 EFS 证书卷影副本(VSS)
vssadmin list shadows :: 列出快照
vssadmin create shadow /for=C: :: 创建快照
wbadmin start backup -backupTarget:E: -include:C: -allCritical :: 系统备份路径与目录
cd :: 显示当前目录
cd /d D:\projects :: 切换盘符+目录
pushd \\server\share :: 进入网络路径
popd :: 返回 pushd 之前目录
dir /a :: 显示所有文件(含隐藏和系统)
dir /a:h :: 仅显示隐藏文件
dir /a:d :: 仅显示目录
dir /s /b *.log :: 递归列出,仅路径
dir /o:s :: 按大小排序
dir /o:d :: 按修改时间排序文件复制
copy src.txt dst.txt :: 基础复制
xcopy src\ dst\ /e /i /y :: 递归复制目录
:: robocopy(推荐,功能最强)
robocopy src\ dst\ :: 基础同步
robocopy src\ dst\ /mir :: 完全镜像(含删除目标多余文件)
robocopy src\ dst\ /e /z /r:3 :: 递归+断点续传+失败重试 3 次
robocopy src\ dst\ /e /log:backup.log :: 记录日志
robocopy src\ dst\ *.log /s :: 仅复制 .log 文件
robocopy src\ dst\ /e /xf *.tmp /xd .git :: 排除文件/目录符号链接与硬链接
:: 符号链接(需管理员或开发者模式)
mklink link.txt target.txt :: 文件符号链接
mklink /d linkdir targetdir :: 目录符号链接
mklink /j junctiondir targetdir :: 目录交接点(Junction,无需管理员)
mklink /h hardlink.txt original.txt :: 硬链接
dir /a:l :: 列出目录中的链接New-Item -ItemType SymbolicLink -Name "link.txt" -Target "target.txt"
New-Item -ItemType Junction -Name "linkdir" -Target "targetdir"
New-Item -ItemType HardLink -Name "hard.txt" -Target "original.txt"文件属性
attrib +h +s file.txt :: 设置隐藏+系统属性
attrib -h -s file.txt :: 取消属性
attrib +r /s /d C:\important\ :: 递归设置只读
attrib :: 查看当前目录文件属性磁盘管理
diskpart(交互式)
diskpart
list disk 列出所有磁盘
select disk 1 选择磁盘
list partition 列出分区
select partition 1
format fs=ntfs quick 快速格式化
assign letter=E 分配盘符
exit
磁盘检查与修复
chkdsk C: :: 检查磁盘(不修复)
chkdsk C: /f :: 修复文件系统错误(重启后执行)
chkdsk C: /r :: 定位坏扇区并恢复数据
chkdsk C: /scan :: 在线扫描(不锁盘)
sfc /scannow :: 系统文件完整性检查
DISM /Online /Cleanup-Image /RestoreHealth :: 修复系统映像PowerShell 磁盘操作
Get-Disk # 列出物理磁盘
Get-Partition # 列出所有分区
Get-Volume # 列出所有卷
Format-Volume -DriveLetter E -FileSystem NTFS -Confirm:$false
Repair-Volume -DriveLetter C -Scan # 扫描
Repair-Volume -DriveLetter C -OfflineScanAndFix # 脱机修复文件查找与搜索
where python :: 在 PATH 中查找可执行文件
dir /s /b "*.conf" :: 递归查找文件名
findstr /s /i /n "error" C:\logs\*.log :: 递归搜索文本(忽略大小写)
findstr /r "^ERROR.*2024" *.log :: 正则搜索Get-ChildItem -Recurse -Filter "*.conf"
Select-String -Path "C:\logs\*.log" -Pattern "error" -CaseSensitive:$false
# 最近 7 天修改的文件
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-7)}
# 大于 100MB 的文件
Get-ChildItem -Recurse | Where-Object {$_.Length -gt 100MB} |
Select-Object FullName, @{N="MB";E={[math]::Round($_.Length/1MB,1)}} |
Sort-Object MB -Descending文件内容操作
Get-Content file.txt # 读取文件
Get-Content file.txt -Tail 20 # 最后 20 行
Get-Content file.txt -Wait # 实时跟踪(类似 tail -f)
Set-Content output.txt "内容" # 写入(覆盖)
Add-Content output.txt "追加行" # 追加
# 全文替换
(Get-Content big.txt) -replace "old","new" | Set-Content big.txt