Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. AWS
  3. Question
AWS

How to use aws s3 cp wildcards to copy group of files in aws cli ?

Asked by Fukuda Ashikaga Apr 20, 2021 35.2K views 3 answers
Share

About this question

I am unable to copy some files from a S3 bucket in AWS CLI

Adding * to the path is not helping:

aws s3 cp s3://personalfiles/file* 

Don’t know how to use aws s3 cp wildcard. Please help

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 28, 2025

Using aws s3 cp with wildcards in AWS CLI is an efficient way to copy groups of files between local systems and S3 buckets or within S3 itself. Here’s a breakdown of how to use it effectively:


1. Syntax for aws s3 cp

  • The basic syntax for copying files is:

  aws s3 cp   [options]

  • When dealing with multiple files, wildcards can help you specify patterns to match file names.

2. Using Wildcards with --recursive

Wildcard Support:

AWS S3 CLI supports shell-style wildcards like * to match multiple files.

Example:

  aws s3 cp s3://your-bucket-name/ local-folder/ --recursive --exclude "*" --include "*.txt"

  • --recursive: Copies all files matching the criteria in subdirectories.
  • --exclude and --include: Filters files using patterns (e.g., .txt, .jpg).

3. Copy Specific Groups of Files

To copy files with a specific extension:

  aws s3 cp s3://your-bucket-name/ local-folder/ --recursive --include "*.csv"

To copy files with a prefix:

  aws s3 cp s3://your-bucket-name/ local-folder/ --recursive --include "logs_*"

4. Copy Between S3 Locations

You can also use wildcards to copy files between two S3 buckets:

  aws s3 cp s3://source-bucket/ s3://destination-bucket/ --recursive --include "*.jpg"

5. Exclude Files

To exclude certain file types, use the --exclude flag:

  aws s3 cp s3://your-bucket-name/ local-folder/ --recursive --exclude "*.tmp"

6. Notes and Limitations

  • Wildcards apply to object keys, not directory paths.
  • Always use --recursive to handle folders and subfolders.
  • Run --dryrun before executing to preview the files:

  aws s3 cp s3://your-bucket-name/ local-folder/ --recursive --dryrun

By combining wildcards, --include, and --exclude, you can precisely control which files to copy. Let me know if you need help with a specific example!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 23, 2024

To copy a group of files in AWS CLI using wildcards with the aws s3 cp command, follow these steps:

Specify the source path with the wildcard pattern to match the group of files you want to copy.

Provide the destination S3 bucket and optionally specify the destination path.

If the source path includes subdirectories, use the --recursive flag.

Here's an example command:

aws s3 cp s3://source-bucket/path/to/files/* s3://destination-bucket/destination-path/ --recursive

s3://source-bucket/path/to/files/* specifies the source path with the wildcard * to match a group of files in the specified directory.

s3://destination-bucket/destination-path/ specifies the destination S3 bucket and path where the files will be copied.

The --recursive flag is used to copy files recursively if the source path includes subdirectories.

Adjust the source and destination paths according to your requirements. This command will copy all files matching the wildcard pattern from the source bucket to the destination bucket.

Was this helpful?

More AWS discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest AWS Blogs

Guides, tips and career advice on AWS from JanBask experts.