Guides
Guide

How to replace a file in multiple folders at once

You have a new logo.png, config.ini or template, and the old one sits in 40 different subfolders. File Explorer and Finder can't do this in one go, but a one-line command can. Here's how, on Windows, Mac and Linux, with a preview and a backup.

Before you run anything

Windows: PowerShell

Open PowerShell. First, list every copy:

Get-ChildItem -Path "C:\Projects" -Recurse -Filter "logo.png"

Back each one up as logo.png.bak, next to the original:

Get-ChildItem -Path "C:\Projects" -Recurse -Filter "logo.png" | ForEach-Object { Copy-Item $_.FullName "$($_.FullName).bak" }

Do a dry run. -WhatIf prints what would happen without changing anything:

Get-ChildItem -Path "C:\Projects" -Recurse -Filter "logo.png" | ForEach-Object { Copy-Item "C:\New\logo.png" $_.FullName -WhatIf }

If the list looks right, run it again without -WhatIf. Add -Force if some copies are read-only.

Windows: Command Prompt

for /r "C:\Projects" %f in (logo.png) do @if exist "%f" copy /y "C:\New\logo.png" "%f"

This walks every folder under C:\Projects and copies over logo.png wherever one exists. In a .bat file, write %%f instead of %f. There's no dry run, so back up first.

Mac and Linux

List every copy:

find ~/Projects -type f -name 'logo.png'

Back each one up as logo.png.bak:

find ~/Projects -type f -name 'logo.png' -exec cp {} {}.bak \;

Replace them all:

find ~/Projects -type f -name 'logo.png' -exec cp -v ~/New/logo.png {} \;

Use -iname instead of -name to match Logo.PNG and other capitalizations too.

Copying a file into every folder, where it isn't yet

If you want the file added to every subfolder, not just the ones that already have it, copy it into each folder instead. In PowerShell:

Get-ChildItem -Path "C:\Projects" -Directory -Recurse | ForEach-Object { Copy-Item "C:\New\readme.txt" $_.FullName }

On Mac and Linux:

find ~/Projects -type d -exec cp ~/New/readme.txt {} \;

A whole set of files, not just one

One file is easy. Fifty updated files, each in its own place in a big folder tree, is where these commands get messy. You have to loop over every name, avoid same-named files that shouldn't change, and decide what happens with files that don't exist yet.

If your new files are in the same folder layout as the destination, the command-line copy tools can update only files that already exist: robocopy with /XL on Windows, or rsync --existing on Mac and Linux. Both need the paths to line up exactly. BulkFileReplace vs robocopy and rsync compares them.

What can go wrong

With a preview and undo: BulkFileReplace

BulkFileReplace does this for one file or hundreds, on Windows and Linux:

  1. Drop your new files (loose, a folder or a ZIP) and choose the folder to update.
  2. It matches by path first, then by name, across every subfolder.
  3. When a name matches more than one place, it asks. Those files are never picked for you: tick the locations to update, then Done.
  4. Review the whole list (Replace, Add, No change) with previews of images and text, then Apply.
  5. Undo any run from History. Every original is backed up first.
A file found in three locations, waiting for a choice of where to update it

Two new files aimed at the same file (say, a settings.ini from a folder and a loose one) are flagged too, so you pick which one wins.

Questions

How do I replace a file in multiple folders in Windows?

In PowerShell: Get-ChildItem -Path "C:\Projects" -Recurse -Filter "logo.png" | ForEach-Object { Copy-Item "C:\New\logo.png" $_.FullName }. Run it with -WhatIf first to see what it would change.

How do I copy a file to multiple folders at once?

Use a command that loops over the folders: Get-ChildItem -Directory -Recurse with Copy-Item in PowerShell, or find -type d -exec cp on Mac and Linux.

Can I replace only some of the files with the same name?

Yes. Check the list first and leave out the copies that should stay. In BulkFileReplace, files with several matches wait for you to pick the locations to update.

Is there a way to undo it?

Only if you made backups first. BulkFileReplace backs up every original and can undo any run.

Bulk file replacement, done right.

Preview every change, automatic backups, undo any run. $19 once.

Keep reading

Replace files in many subfolders at once

Read more โ†’

Update the same files across many projects

Read more โ†’

BulkFileReplace vs robocopy and rsync

Read more โ†’