Astuces

Best way to keep two folders in sync

Profil Picture

Guillaume Briday

1 minute

Sometime, you have to keep two folders in sync. They can be on external devices like SSDs or HDDs, on remote servers or on NAS for instance.

It could be really handy when you want to share files with friends and coworkers or deploy static or PHP websites.

You can manually do this action, but it takes a lot of times and it is an error-prone process.

Traditional drag and drop technique.
Traditional drag and drop technique.

How it works

To automate the process, we are going to use rsync.

Here is an example of how to use it in a real life example:

rsync -aP --delete "~/Movies/2023" "/Volumes/SSD Sandisk/Videos"

In this case, I want to copy the folder ~/Movies/2023 from my local machine to a folder named Videos on my external SSD.

Here is the detail of the -aP flag.

-P is really useful for big files like videos.

--delete will remove files from the destination that don't exist on the local machine. You cannot perform this when you use the Finder and it's one of the most important reasons to use rsync instead of the Finder.

  • -a: archive mode; same as -rlptgoD

    • -r: recurse into directories
    • -l: copy symlinks as symlinks
    • -p: preserve permissions
    • -t: preserve times
    • -g: preserve group
    • -o: preserve owner (super-user only)
    • -D: same as --devices --specials
      • --devises: preserve device files (super-user only)
      • --specials: preserve special files
  • -P: Same as --partial --progress

    • --partial: keep partially transferred files
    • --progress: show progress during transfer
  • --delete: delete extraneous files from destination dirs

On remote servers

Files can be be your local network or on the internet, it does not matter for rsync. Very cool if you want to transfer files on a Raspberry PI, a NAS or a remote server.

rsync -aP --delete "~/Movies/2023" user@host:~/Movies

Deploy a website (Static or PHP)

For instance with Gatsby JS:

gatsby build && rsync -avz --delete public/ user@host:~/www/

Exclude flag

Another flag that might be interesting to know is --exclude:

--exclude '*.log' to exclude all .log files.

--exclude={'*.log', '2022', '2023'} to exclude all .log files and 2022 and 2023 folders.

Conclusion

This is not a way to Backup your files. For that, you should use Time Machine or Carbon Copy Cloner.

Simplify your time tracking with Timecop

Timecop is a time tracking app that brings simplicity in your day to day life.

Timecop projects