You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在命令行替换多文件夹中含目标文件夹名的指定文件

解决方案

根据你的需求,完全可以通过命令行批量完成替换,以下分不同操作系统给出具体命令:

Linux/macOS(Bash)

假设你的新文件目录是~/new_data,原始文件目录是~/original_dir,执行以下命令:

cd ~/new_data
for file in data_*.csv; do
  # 从文件名中提取对应的文件夹名称(去掉"data_"和".csv")
  folder_name="${file#data_}"
  folder_name="${folder_name%.csv}"
  # 强制替换原始目录对应文件夹里的旧文件
  cp -f "$file" ~/original_dir/"$folder_name"/"$file"
done

Windows(PowerShell)

假设你的新文件目录是C:\new_data,原始文件目录是C:\original_dir,执行以下命令:

Get-ChildItem -Path C:\new_data -Filter data_*.csv | ForEach-Object {
    # 用正则提取文件名中的文件夹名称
    $folderName = $_.Name -replace '^data_(.*)\.csv$', '$1'
    # 拼接目标文件路径
    $targetPath = Join-Path C:\original_dir $folderName | Join-Path -ChildPath $_.Name
    # 强制覆盖旧文件
    Copy-Item -Path $_.FullName -Destination $targetPath -Force
}

注意事项

  • 务必先备份原始文件,避免操作失误导致数据丢失
  • 替换命令中的路径为你实际的目录路径
  • 可以先去掉-f(Bash)或-Force(PowerShell),添加echo(Bash)或Write-Output $targetPath(PowerShell),先预览要替换的文件路径,确认无误后再执行替换操作

内容的提问来源于stack exchange,提问作者Katerina Sym

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 06:30:47