Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files from subdirectories directly to a folder using aws-cli

Tags:

aws-cli

I have a number of files that are contained within subfolders of /myfolder.

I would like to use aws cli to move them to /newfolder.

For example:

/myfolder/2017_01_01/file1.txt
/myfolder/2017_01_02/file2.txt
...

I want to copy the files out to a new folder:

/newfolder/file1.txt
/newfolder/file2.txt
...

When I use the cp command like below:

aws s3 cp s3://myfolder/ s3://newfolder/ --recursive --exclude '*' --include '*file*'

The correct files are copied, but the problem is that the files remain in their subfolders at the new location:

/newfolder/2017_01_01/file1.txt
/newfolder/2017_01_02/file2.txt
...

Is it possible to copy the files "out" of their subfolders so that they are directly put into /newfolder?

like image 558
Sam Gilbert Avatar asked Feb 22 '17 10:02

Sam Gilbert


People also ask

What is the command to copy files recursively in a folder to an S3 bucket?

s3://destination --recursive . That way, it is copying the contents of the current directory.

How do I transfer files from S3 to local using AWS CLI?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt . To know more about AWS S3 and its features in detail check this out!

Which command is used in CLI to upload a group of files on S3 bucket?

(Recommended) Upload the file using high-level (aws s3) commands. This example uses the command aws s3 cp, but other aws s3 commands that involve uploading objects into an S3 bucket (for example, aws s3 sync or aws s3 mv) also automatically perform a multipart upload when the object is large.


1 Answers

The easiest way to do this is a hybrid solution assuming you have linux shell: (make sure you create blank folder in your local computer and cd to it before doing the below steps)

1.aws s3 cp s3://yourBucket/myfolder . --recursive  --include "*.txt"
2.find . -name '*txt' -exec mv {} .  \;
3.aws s3 cp ./ s3://yourBucket/newfolder --exclude "*" --include "*.txt" --recursive
like image 128
grepit Avatar answered Jan 03 '23 12:01

grepit