Introduction

Two commands in the AWS S3 CLI that can be confusing to differentiate are cp and sync. These commands have similar descriptions, and many developers use them interchangeably without understanding the differences.

This article will list the key differences between the two commands.

cp

  • cp will copy all files even if they already exist in the destination folder.
  • cp doesn’t support the deletion of files in the destination folder, even if you deleted the files from the source folder.

sync

  • Sync will only copy new and updated files from the source directory to the destination. Sync will first look at the destination folder before copying files over.

  • --delete flag supports the deletion of files in the destination folder but no longer in the source folder. sync compares the size of the file and the last modified timestamp to check if a file needs updating. Sync doesn’t compare file contents.

When to use cp?

  • cp is a simpler and more performant operation than sync. Use it when you copy a few files and don’t care about keeping directories in sync.

When to use sync?

  • Use sync when you want to keep the structure of the source and destination folders in sync. E.g., maintaining a static website.
  • sync generally copies fewer data than cp since only new or updated files are eligible.