Synchronise one directory to another (one way mirroring) by performing the minimum amount of changes using Javascript, Typescript, or Php

Let's imagine that we have two directories which must contain both the same files. We know that one of the folders is up to date and we want to sync all its contents to the other, which may have partially the same data or not. So we basically want to "mirror" the first directory into the second one so at the end of the process we are sure that both of them contain exactly the same files, folders, and structure.

The simplest way to normally achieve this is by removing all the second directory contents and copying the whole first directory there. But when the source folder contains large amounts of data, the copy process may require lots of time. If both directories have partially the same information, it is better to scan the second one to remove the files that are not found on the first directory, copy only those that do not exist and leave untouched all the files that exist on both directories. By doing this, we may be saving lots of time and avoid hard drive overhead, cause only the necessary files and folders will be copied or deleted.

This process of one-way sync or directory mirroring is super easy to achieve with the turbodepot library. It contains a class called FilesManager which has a method named mirrorDirectory() that performs exactly this operation. Just as the documentation says:

This method performs a one-way sync process which consists of applying the minimum modifications to the destination path that will guarantee that it is an exact copy of the source path. Any files or folders that are identical on both provided paths will be left untouched

So let's see how to programmatically sync two directories with some of our favorite programming languages:

Perform a one-way synchronization between two directories in PHP

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\managers\FilesManager;

$filesManager = new FilesManager();
$filesManager->mirrorDirectory('path/to/source/directory', 'path/to/destination/directory');

And that's all. Just after the method is called, both directories will have exactly the same contents and the minimum copy and delete operations will have been performed to achieve the desired result.

Perform a one-way synchronization between two directories in Javascript (node js)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.mirrorDirectory('path/to/source/directory', 'path/to/destination/directory');

And that's all. Just after the method is called, both directories will have exactly the same contents and the minimum copy and delete operations will have been performed to achieve the desired result.

Perform a one-way synchronization between two directories in Typescript (TS)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.mirrorDirectory('path/to/source/directory', 'path/to/destination/directory');

And that's all. Just after the method is called, both directories will have exactly the same contents and the minimum copy and delete operations will have been performed to achieve the desired result.